[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> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 22 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 23 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 24 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 26 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 27 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 28 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 29 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 30 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 31 | #include "base/macros.h" |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 32 | #include "base/memory/ptr_util.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" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 46 | #include "base/test/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" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 59 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 60 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 61 | #include "net/base/upload_bytes_element_reader.h" |
| 62 | #include "net/base/upload_data_stream.h" |
| 63 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 64 | #include "net/base/url_util.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 65 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 66 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 67 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 68 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 69 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 70 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 71 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 72 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 73 | #include "net/cookies/cookie_monster.h" |
| 74 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 75 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 76 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 77 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | #include "net/http/http_cache.h" |
| 79 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 80 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 81 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 82 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 83 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 84 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 85 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 86 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 87 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 88 | #include "net/log/test_net_log_entry.h" |
| 89 | #include "net/log/test_net_log_util.h" |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 90 | #include "net/net_features.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 91 | #include "net/nqe/external_estimate_provider.h" |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 92 | #include "net/proxy/proxy_server.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 93 | #include "net/proxy/proxy_service.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 94 | #include "net/quic/chromium/mock_crypto_client_stream_factory.h" |
| 95 | #include "net/quic/chromium/quic_server_info.h" |
| 96 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 97 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 98 | #include "net/ssl/channel_id_service.h" |
| 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" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 101 | #include "net/ssl/ssl_server_config.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 102 | #include "net/ssl/token_binding.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 103 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 104 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 105 | #include "net/test/embedded_test_server/http_request.h" |
| 106 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 107 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 108 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 109 | #include "net/test/test_data_directory.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 110 | #include "net/test/url_request/url_request_failed_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 111 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 112 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 113 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 114 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 115 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 116 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 117 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 118 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 119 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 120 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 121 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 122 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 123 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 124 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 125 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 127 | #if defined(OS_FUCHSIA) |
| 128 | #define USE_BUILTIN_CERT_VERIFIER |
| 129 | #endif |
| 130 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 131 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 132 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 133 | #include "net/url_request/file_protocol_handler.h" |
| 134 | #include "net/url_request/url_request_file_dir_job.h" |
| 135 | #endif |
| 136 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 137 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 138 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 139 | #include "net/url_request/ftp_protocol_handler.h" |
| 140 | #endif |
| 141 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 142 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 143 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 144 | #endif |
| 145 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 146 | #if BUILDFLAG(ENABLE_REPORTING) |
| 147 | #include "net/reporting/reporting_service.h" |
Julia Tuttle | 3f9acac | 2017-11-20 22:06:30 | [diff] [blame^] | 148 | #include "net/url_request/network_error_logging_delegate.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 149 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 150 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 151 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 152 | #include "net/cert/cert_net_fetcher.h" |
| 153 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 154 | #endif |
| 155 | |
| 156 | #if defined(USE_NSS_CERTS) |
| 157 | #include "net/cert_net/nss_ocsp.h" |
| 158 | #endif |
| 159 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 160 | using net::test::IsError; |
| 161 | using net::test::IsOk; |
| 162 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 163 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 164 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 165 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 166 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 167 | namespace net { |
| 168 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | namespace { |
| 170 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 171 | namespace test_default { |
| 172 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 175 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 176 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 177 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 178 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 179 | const base::FilePath::CharType kTestFilePath[] = |
| 180 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 181 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 182 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 183 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 184 | // Test file used in most FTP tests. |
| 185 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 186 | #endif |
| 187 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 188 | // Tests load timing information in the case a fresh connection was used, with |
| 189 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 190 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 191 | int connect_timing_flags) { |
| 192 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 193 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 194 | |
| 195 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 196 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 197 | |
| 198 | EXPECT_LE(load_timing_info.request_start, |
| 199 | load_timing_info.connect_timing.connect_start); |
| 200 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 201 | connect_timing_flags); |
| 202 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 203 | load_timing_info.send_start); |
| 204 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 205 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 206 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 207 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 208 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 209 | } |
| 210 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 211 | // Same as above, but with proxy times. |
| 212 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 213 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 214 | int connect_timing_flags) { |
| 215 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 216 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 217 | |
| 218 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 219 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 220 | |
| 221 | EXPECT_LE(load_timing_info.request_start, |
| 222 | load_timing_info.proxy_resolve_start); |
| 223 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 224 | load_timing_info.proxy_resolve_end); |
| 225 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 226 | load_timing_info.connect_timing.connect_start); |
| 227 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 228 | connect_timing_flags); |
| 229 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 230 | load_timing_info.send_start); |
| 231 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 232 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 233 | } |
| 234 | |
| 235 | // Same as above, but with a reused socket and proxy times. |
| 236 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 237 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 238 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 239 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 240 | |
| 241 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 242 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 243 | |
| 244 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 245 | |
| 246 | EXPECT_LE(load_timing_info.request_start, |
| 247 | load_timing_info.proxy_resolve_start); |
| 248 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 249 | load_timing_info.proxy_resolve_end); |
| 250 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 251 | load_timing_info.send_start); |
| 252 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 253 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 254 | } |
| 255 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 256 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 257 | // Tests load timing information in the case of a cache hit, when no cache |
| 258 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 259 | base::StringPiece TestNetResourceProvider(int key) { |
| 260 | return "header"; |
| 261 | } |
| 262 | |
| 263 | void FillBuffer(char* buffer, size_t len) { |
| 264 | static bool called = false; |
| 265 | if (!called) { |
| 266 | called = true; |
| 267 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 268 | srand(seed); |
| 269 | } |
| 270 | |
| 271 | for (size_t i = 0; i < len; i++) { |
| 272 | buffer[i] = static_cast<char>(rand()); |
| 273 | if (!buffer[i]) |
| 274 | buffer[i] = 'g'; |
| 275 | } |
| 276 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 277 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 278 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 279 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 280 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 281 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 282 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 283 | |
| 284 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 285 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 286 | |
| 287 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 288 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 289 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 290 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 291 | |
| 292 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 293 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 294 | } |
| 295 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 296 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 297 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 298 | // Tests load timing in the case that there is no HTTP response. This can be |
| 299 | // used to test in the case of errors or non-HTTP requests. |
| 300 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 301 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 302 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 303 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 304 | |
| 305 | // Only the request times should be non-null. |
| 306 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 307 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 308 | |
| 309 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 310 | |
| 311 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 312 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 313 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 314 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 315 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 316 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 317 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 318 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 319 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 320 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 321 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 322 | public: |
| 323 | TestPowerMonitorSource() {} |
| 324 | ~TestPowerMonitorSource() override {} |
| 325 | |
| 326 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 327 | |
| 328 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 329 | |
| 330 | bool IsOnBatteryPowerImpl() override { return false; } |
| 331 | |
| 332 | private: |
| 333 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 334 | }; |
| 335 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 336 | // Job that allows monitoring of its priority. |
| 337 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 338 | public: |
| 339 | // The latest priority of the job is always written to |request_priority_|. |
| 340 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 341 | NetworkDelegate* network_delegate, |
| 342 | RequestPriority* request_priority) |
| 343 | : URLRequestTestJob(request, network_delegate), |
| 344 | request_priority_(request_priority) { |
| 345 | *request_priority_ = DEFAULT_PRIORITY; |
| 346 | } |
| 347 | |
| 348 | void SetPriority(RequestPriority priority) override { |
| 349 | *request_priority_ = priority; |
| 350 | URLRequestTestJob::SetPriority(priority); |
| 351 | } |
| 352 | |
| 353 | private: |
| 354 | RequestPriority* const request_priority_; |
| 355 | }; |
| 356 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 357 | // Do a case-insensitive search through |haystack| for |needle|. |
| 358 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 359 | std::string::const_iterator it = std::search( |
| 360 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 361 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 362 | return it != haystack.end(); |
| 363 | } |
| 364 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 365 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 366 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 367 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 368 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 371 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 372 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 373 | // -1 means unknown. 0 means no encryption. |
| 374 | EXPECT_GT(ssl_info.security_bits, 0); |
| 375 | |
| 376 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 377 | uint16_t cipher_suite = |
| 378 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 379 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 382 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 383 | const GURL& host_url) { |
| 384 | std::string sent_value; |
| 385 | |
| 386 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 387 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 388 | |
| 389 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 390 | EXPECT_EQ("keep-alive", sent_value); |
| 391 | } |
| 392 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 393 | // A network delegate that allows the user to choose a subset of request stages |
| 394 | // to block in. When blocking, the delegate can do one of the following: |
| 395 | // * synchronously return a pre-specified error code, or |
| 396 | // * asynchronously return that value via an automatically called callback, |
| 397 | // or |
| 398 | // * block and wait for the user to do a callback. |
| 399 | // Additionally, the user may also specify a redirect URL -- then each request |
| 400 | // with the current URL different from the redirect target will be redirected |
| 401 | // to that target, in the on-before-URL-request stage, independent of whether |
| 402 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 403 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 404 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 405 | // Stages in which the delegate can block. |
| 406 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 407 | NOT_BLOCKED = 0, |
| 408 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 409 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 410 | ON_HEADERS_RECEIVED = 1 << 2, |
| 411 | ON_AUTH_REQUIRED = 1 << 3 |
| 412 | }; |
| 413 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 414 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 415 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 416 | enum BlockMode { |
| 417 | SYNCHRONOUS, // No callback, returns specified return values. |
| 418 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 419 | // specified return codes. |
| 420 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 421 | // |auth_retval_| are ignored. In every blocking stage the |
| 422 | // message loop is quit. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 423 | USER_NOTIFY, // User is notified by a provided callback of the |
| 424 | // blocking, and synchronously returns instructions |
| 425 | // for handling it. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 426 | }; |
| 427 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 428 | using NotificationCallback = |
| 429 | base::Callback<Error(const CompletionCallback&, const URLRequest*)>; |
| 430 | |
| 431 | using NotificationAuthCallback = |
| 432 | base::Callback<NetworkDelegate::AuthRequiredResponse(const AuthCallback&, |
| 433 | const URLRequest*)>; |
| 434 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 435 | // Creates a delegate which does not block at all. |
| 436 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 437 | |
| 438 | // For users to trigger a callback returning |response|. |
| 439 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 440 | // Only call if |block_mode_| == USER_CALLBACK. |
| 441 | void DoCallback(int response); |
| 442 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 443 | |
| 444 | // Setters. |
| 445 | void set_retval(int retval) { |
| 446 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 447 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 448 | ASSERT_NE(OK, retval); |
| 449 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 452 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 453 | // |auth_credentials_| will be passed with the response. |
| 454 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 455 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 456 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 457 | auth_retval_ = auth_retval; |
| 458 | } |
| 459 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 460 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 463 | void set_redirect_url(const GURL& url) { |
| 464 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 465 | } |
| 466 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 467 | void set_block_on(int block_on) { |
| 468 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 469 | } |
| 470 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 471 | // Only valid if |block_mode_| == USER_NOTIFY |
| 472 | void set_notification_callback( |
| 473 | const NotificationCallback& notification_callback) { |
| 474 | notification_callback_ = notification_callback; |
| 475 | } |
| 476 | |
| 477 | void set_notification_auth_callback( |
| 478 | const NotificationAuthCallback& notification_auth_callback) { |
| 479 | notification_auth_callback_ = notification_auth_callback; |
| 480 | } |
| 481 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 482 | // Allows the user to check in which state did we block. |
| 483 | Stage stage_blocked_for_callback() const { |
| 484 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 485 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 489 | void RunCallback(int response, const CompletionCallback& callback); |
| 490 | void RunAuthCallback(AuthRequiredResponse response, |
| 491 | const AuthCallback& callback); |
| 492 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 493 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 494 | int OnBeforeURLRequest(URLRequest* request, |
| 495 | const CompletionCallback& callback, |
| 496 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 497 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 498 | int OnBeforeStartTransaction(URLRequest* request, |
| 499 | const CompletionCallback& callback, |
| 500 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 501 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 502 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 503 | URLRequest* request, |
| 504 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 505 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 506 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 507 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 508 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 509 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 510 | URLRequest* request, |
| 511 | const AuthChallengeInfo& auth_info, |
| 512 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 513 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 514 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 515 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 516 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 517 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 518 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 519 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 520 | int MaybeBlockStage(Stage stage, |
| 521 | const URLRequest* request, |
| 522 | const CompletionCallback& callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 523 | |
| 524 | // Configuration parameters, can be adjusted by public methods: |
| 525 | const BlockMode block_mode_; |
| 526 | |
| 527 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 528 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 529 | int retval_; // To be returned in non-auth stages. |
| 530 | AuthRequiredResponse auth_retval_; |
| 531 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 532 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 533 | int block_on_; // Bit mask: in which stages to block. |
| 534 | |
| 535 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 536 | // callback. |
| 537 | AuthCredentials auth_credentials_; |
| 538 | AuthCredentials* target_auth_credentials_; |
| 539 | |
| 540 | // Internal variables, not set by not the user: |
| 541 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 542 | // USER_CALLBACK). |
| 543 | Stage stage_blocked_for_callback_; |
| 544 | |
| 545 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 546 | CompletionCallback callback_; |
| 547 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 548 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 549 | // Callback to request user instructions for blocking. |
| 550 | NotificationCallback notification_callback_; |
| 551 | NotificationAuthCallback notification_auth_callback_; |
| 552 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 553 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 554 | |
| 555 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 556 | }; |
| 557 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 558 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 559 | : block_mode_(block_mode), |
| 560 | retval_(OK), |
| 561 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 562 | block_on_(0), |
| 563 | target_auth_credentials_(NULL), |
| 564 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 565 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 569 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 570 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 571 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 572 | CompletionCallback callback = callback_; |
| 573 | Reset(); |
| 574 | RunCallback(response, callback); |
| 575 | } |
| 576 | |
| 577 | void BlockingNetworkDelegate::DoAuthCallback( |
| 578 | NetworkDelegate::AuthRequiredResponse response) { |
| 579 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 580 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 581 | AuthCallback auth_callback = auth_callback_; |
| 582 | Reset(); |
| 583 | RunAuthCallback(response, auth_callback); |
| 584 | } |
| 585 | |
| 586 | void BlockingNetworkDelegate::RunCallback(int response, |
| 587 | const CompletionCallback& callback) { |
| 588 | callback.Run(response); |
| 589 | } |
| 590 | |
| 591 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 592 | const AuthCallback& callback) { |
| 593 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 594 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 595 | *target_auth_credentials_ = auth_credentials_; |
| 596 | } |
| 597 | callback.Run(response); |
| 598 | } |
| 599 | |
| 600 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 601 | URLRequest* request, |
| 602 | const CompletionCallback& callback, |
| 603 | GURL* new_url) { |
| 604 | if (redirect_url_ == request->url()) |
| 605 | return OK; // We've already seen this request and redirected elsewhere. |
| 606 | |
| 607 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 608 | |
| 609 | if (!redirect_url_.is_empty()) |
| 610 | *new_url = redirect_url_; |
| 611 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 612 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 613 | } |
| 614 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 615 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 616 | URLRequest* request, |
| 617 | const CompletionCallback& callback, |
| 618 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 619 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 620 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 621 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 625 | URLRequest* request, |
| 626 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 627 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 628 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 629 | GURL* allowed_unsafe_redirect_url) { |
| 630 | TestNetworkDelegate::OnHeadersReceived(request, |
| 631 | callback, |
| 632 | original_response_headers, |
| 633 | override_response_headers, |
| 634 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 635 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 636 | return MaybeBlockStage(ON_HEADERS_RECEIVED, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 640 | URLRequest* request, |
| 641 | const AuthChallengeInfo& auth_info, |
| 642 | const AuthCallback& callback, |
| 643 | AuthCredentials* credentials) { |
| 644 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 645 | credentials); |
| 646 | // Check that the user has provided callback for the previous blocked stage. |
| 647 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 648 | |
| 649 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 650 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 651 | } |
| 652 | |
| 653 | target_auth_credentials_ = credentials; |
| 654 | |
| 655 | switch (block_mode_) { |
| 656 | case SYNCHRONOUS: |
| 657 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 658 | *target_auth_credentials_ = auth_credentials_; |
| 659 | return auth_retval_; |
| 660 | |
| 661 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 662 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 663 | FROM_HERE, |
| 664 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 665 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 666 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 667 | |
| 668 | case USER_CALLBACK: |
| 669 | auth_callback_ = callback; |
| 670 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 671 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 672 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 673 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 674 | |
| 675 | case USER_NOTIFY: |
| 676 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 677 | // responsibility for running the callback in the future. |
| 678 | return notification_auth_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 679 | } |
| 680 | NOTREACHED(); |
| 681 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 682 | } |
| 683 | |
| 684 | void BlockingNetworkDelegate::Reset() { |
| 685 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 686 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 687 | callback_.Reset(); |
| 688 | auth_callback_.Reset(); |
| 689 | } |
| 690 | |
| 691 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 692 | BlockingNetworkDelegate::Stage stage, |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 693 | const URLRequest* request, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 694 | const CompletionCallback& callback) { |
| 695 | // Check that the user has provided callback for the previous blocked stage. |
| 696 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 697 | |
| 698 | if ((block_on_ & stage) == 0) { |
| 699 | return OK; |
| 700 | } |
| 701 | |
| 702 | switch (block_mode_) { |
| 703 | case SYNCHRONOUS: |
| 704 | EXPECT_NE(OK, retval_); |
| 705 | return retval_; |
| 706 | |
| 707 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 708 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 709 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 710 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 711 | return ERR_IO_PENDING; |
| 712 | |
| 713 | case USER_CALLBACK: |
| 714 | callback_ = callback; |
| 715 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 716 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 717 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 718 | return ERR_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 719 | |
| 720 | case USER_NOTIFY: |
| 721 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 722 | // responsibility for running the callback in the future. |
| 723 | return notification_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 724 | } |
| 725 | NOTREACHED(); |
| 726 | return 0; |
| 727 | } |
| 728 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 729 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 730 | public: |
| 731 | // Does not own |delegate|. |
| 732 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 733 | NetworkDelegate* delegate) |
| 734 | : TestURLRequestContext(true) { |
| 735 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 736 | set_network_delegate(delegate); |
| 737 | Init(); |
| 738 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 739 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 740 | }; |
| 741 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 742 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 743 | // URI and report to be sent. |
| 744 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 745 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 746 | public: |
| 747 | MockCertificateReportSender() {} |
| 748 | ~MockCertificateReportSender() override {} |
| 749 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 750 | void Send(const GURL& report_uri, |
| 751 | base::StringPiece content_type, |
| 752 | base::StringPiece report, |
| 753 | const base::Callback<void()>& success_callback, |
| 754 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 755 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 756 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 757 | report.CopyToString(&latest_report_); |
| 758 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 759 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 760 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 761 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 762 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 763 | |
| 764 | private: |
| 765 | GURL latest_report_uri_; |
| 766 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 767 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 768 | }; |
| 769 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 770 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 771 | public: |
| 772 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 773 | }; |
| 774 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 775 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 776 | // This is needed because after the certificate failure, the URLRequest will |
| 777 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 778 | // The partial SSLInfo does not have the OCSP information filled out. |
| 779 | class OCSPErrorTestDelegate : public TestDelegate { |
| 780 | public: |
| 781 | void OnSSLCertificateError(URLRequest* request, |
| 782 | const SSLInfo& ssl_info, |
| 783 | bool fatal) override { |
| 784 | ssl_info_ = ssl_info; |
| 785 | on_ssl_certificate_error_called_ = true; |
| 786 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 787 | } |
| 788 | |
| 789 | bool on_ssl_certificate_error_called() { |
| 790 | return on_ssl_certificate_error_called_; |
| 791 | } |
| 792 | |
| 793 | SSLInfo ssl_info() { return ssl_info_; } |
| 794 | |
| 795 | private: |
| 796 | bool on_ssl_certificate_error_called_ = false; |
| 797 | SSLInfo ssl_info_; |
| 798 | }; |
| 799 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 800 | } // namespace |
| 801 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 802 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 803 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 804 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 805 | URLRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 806 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 807 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 808 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 809 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 810 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 811 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 812 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 813 | // URLRequestJobs may post clean-up tasks on destruction. |
| 814 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 815 | |
| 816 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 817 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 818 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 819 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 820 | SetUpFactory(); |
| 821 | default_context_.set_job_factory(job_factory_.get()); |
| 822 | default_context_.Init(); |
| 823 | PlatformTest::SetUp(); |
| 824 | } |
| 825 | |
| 826 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 827 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 828 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 829 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 830 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 831 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 832 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 833 | #endif |
| 834 | } |
| 835 | |
| 836 | TestNetworkDelegate* default_network_delegate() { |
| 837 | return &default_network_delegate_; |
| 838 | } |
| 839 | |
| 840 | const TestURLRequestContext& default_context() const { |
| 841 | return default_context_; |
| 842 | } |
| 843 | |
| 844 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 845 | // Adds the TestJobInterceptor to the default context. |
| 846 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 847 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 848 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 849 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 850 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 851 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 852 | } |
| 853 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 854 | // Creates a temp test file and writes |data| to the file. The file will be |
| 855 | // deleted after the test completes. |
| 856 | void CreateTestFile(const char* data, |
| 857 | size_t data_size, |
| 858 | base::FilePath* test_file) { |
| 859 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 860 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 861 | // now, Mac and Android bots return a path with a symbolic link. |
| 862 | base::FilePath absolute_temp_dir = |
| 863 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 864 | |
| 865 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 866 | ASSERT_EQ(static_cast<int>(data_size), |
| 867 | base::WriteFile(*test_file, data, data_size)); |
| 868 | } |
| 869 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 870 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 871 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 872 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 873 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 874 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 875 | TestURLRequestContext default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 876 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 877 | }; |
| 878 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 879 | // This NetworkDelegate is picky about what files are accessible. Only |
| 880 | // whitelisted files are allowed. |
| 881 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 882 | public: |
| 883 | CookieBlockingNetworkDelegate(){}; |
| 884 | |
| 885 | // Adds |directory| to the access white list. |
| 886 | void AddToWhitelist(const base::FilePath& directory) { |
| 887 | whitelist_.insert(directory); |
| 888 | } |
| 889 | |
| 890 | private: |
| 891 | // Returns true if |path| matches the white list. |
| 892 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 893 | for (const auto& directory : whitelist_) { |
| 894 | if (directory == path || directory.IsParent(path)) |
| 895 | return true; |
| 896 | } |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | // Returns true only if both |original_path| and |absolute_path| match the |
| 901 | // white list. |
| 902 | bool OnCanAccessFile(const URLRequest& request, |
| 903 | const base::FilePath& original_path, |
| 904 | const base::FilePath& absolute_path) const override { |
| 905 | return (OnCanAccessFileInternal(original_path) && |
| 906 | OnCanAccessFileInternal(absolute_path)); |
| 907 | } |
| 908 | |
| 909 | std::set<base::FilePath> whitelist_; |
| 910 | |
| 911 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 912 | }; |
| 913 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 914 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 915 | TestDelegate d; |
| 916 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 917 | std::unique_ptr<URLRequest> r( |
| 918 | default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 919 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 920 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 921 | r->Start(); |
| 922 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 923 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 924 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 926 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 927 | EXPECT_FALSE(d.received_data_before_response()); |
| 928 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 929 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 930 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 931 | |
| 932 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 933 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 938 | TestDelegate d; |
| 939 | { |
| 940 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 941 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 942 | GURL("data:image/png;base64," |
| 943 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 944 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 945 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 946 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 947 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 948 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 949 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 950 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 951 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 952 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 953 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 954 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 955 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 956 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 957 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 958 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 959 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 960 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 961 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 962 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 963 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 964 | r->Start(); |
| 965 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 966 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 967 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 969 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 970 | EXPECT_FALSE(d.received_data_before_response()); |
| 971 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 972 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 973 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 974 | |
| 975 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 976 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 980 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 981 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 982 | const char kTestFileContent[] = "Hello"; |
| 983 | base::FilePath test_file; |
| 984 | ASSERT_NO_FATAL_FAILURE( |
| 985 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 986 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 987 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 988 | |
| 989 | TestDelegate d; |
| 990 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 991 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 992 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 993 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 994 | r->Start(); |
| 995 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 996 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 997 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 998 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 999 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1000 | EXPECT_EQ(1, d.response_started_count()); |
| 1001 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1002 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1003 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 1004 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1005 | |
| 1006 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1007 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1011 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1012 | const char kTestFileContent[] = "Hello"; |
| 1013 | base::FilePath test_file; |
| 1014 | ASSERT_NO_FATAL_FAILURE( |
| 1015 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1016 | |
| 1017 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1018 | |
| 1019 | TestDelegate d; |
| 1020 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1021 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1022 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1023 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1024 | r->Start(); |
| 1025 | EXPECT_TRUE(r->is_pending()); |
| 1026 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1027 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1028 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1029 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1030 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1031 | } |
| 1032 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1033 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1034 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1035 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1036 | FillBuffer(buffer.get(), buffer_size); |
| 1037 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1038 | base::FilePath test_file; |
| 1039 | ASSERT_NO_FATAL_FAILURE( |
| 1040 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1041 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1042 | |
| 1043 | const size_t first_byte_position = 500; |
| 1044 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1045 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1046 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1047 | buffer.get() + last_byte_position + 1); |
| 1048 | |
| 1049 | TestDelegate d; |
| 1050 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1051 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1052 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1053 | |
| 1054 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1055 | headers.SetHeader( |
| 1056 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1057 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1058 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1059 | r->SetExtraRequestHeaders(headers); |
| 1060 | r->Start(); |
| 1061 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1062 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1063 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1064 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1065 | EXPECT_EQ(1, d.response_started_count()); |
| 1066 | EXPECT_FALSE(d.received_data_before_response()); |
| 1067 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1068 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1069 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1070 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1074 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1075 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1076 | FillBuffer(buffer.get(), buffer_size); |
| 1077 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1078 | base::FilePath test_file; |
| 1079 | ASSERT_NO_FATAL_FAILURE( |
| 1080 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1081 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1082 | |
| 1083 | const size_t first_byte_position = 500; |
| 1084 | const size_t last_byte_position = buffer_size - 1; |
| 1085 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1086 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1087 | buffer.get() + last_byte_position + 1); |
| 1088 | |
| 1089 | TestDelegate d; |
| 1090 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1091 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1092 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1093 | |
| 1094 | HttpRequestHeaders headers; |
| 1095 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1096 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1097 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1098 | r->SetExtraRequestHeaders(headers); |
| 1099 | r->Start(); |
| 1100 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1101 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1102 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1103 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1104 | EXPECT_EQ(1, d.response_started_count()); |
| 1105 | EXPECT_FALSE(d.received_data_before_response()); |
| 1106 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1107 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1108 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1109 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1113 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1114 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1115 | FillBuffer(buffer.get(), buffer_size); |
| 1116 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1117 | base::FilePath test_file; |
| 1118 | ASSERT_NO_FATAL_FAILURE( |
| 1119 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1120 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | |
| 1122 | TestDelegate d; |
| 1123 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1124 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1125 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1126 | |
| 1127 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1128 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1129 | r->SetExtraRequestHeaders(headers); |
| 1130 | r->Start(); |
| 1131 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1132 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1133 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1134 | EXPECT_TRUE(d.request_failed()); |
| 1135 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1136 | } |
| 1137 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1138 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1139 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1140 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1141 | ASSERT_NO_FATAL_FAILURE( |
| 1142 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1143 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1144 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1145 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1146 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1147 | // Hence the test should use the directory name of |test_file|, rather than |
| 1148 | // |absolute_temp_dir|, for whitelisting. |
| 1149 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1150 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1151 | { |
| 1152 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1153 | CookieBlockingNetworkDelegate network_delegate; |
| 1154 | network_delegate.AddToWhitelist(real_temp_dir); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1155 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1156 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1157 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1158 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1159 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1160 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1161 | EXPECT_FALSE(d.request_failed()); |
| 1162 | EXPECT_EQ(test_data, d.data_received()); |
| 1163 | } |
| 1164 | |
| 1165 | { |
| 1166 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1167 | CookieBlockingNetworkDelegate network_delegate; |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1168 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1169 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1170 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1171 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1172 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1173 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1174 | EXPECT_TRUE(d.request_failed()); |
| 1175 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1176 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1180 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1181 | |
| 1182 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1183 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1184 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1185 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1186 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1187 | |
| 1188 | // Create a good directory (will be whitelisted) and a good file. |
| 1189 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1190 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1191 | base::FilePath good_file; |
| 1192 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1193 | std::string good_data("good"); |
| 1194 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1195 | // See the comment in AllowFileURLs() for why this is done. |
| 1196 | base::FilePath real_good_dir = good_file.DirName(); |
| 1197 | |
| 1198 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1199 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1200 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1201 | base::FilePath bad_file; |
| 1202 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1203 | std::string bad_data("bad"); |
| 1204 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1205 | |
| 1206 | // This symlink will point to the good file. Access to the symlink will be |
| 1207 | // allowed as both the symlink and the destination file are in the same |
| 1208 | // good directory. |
| 1209 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1210 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1211 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1212 | // This symlink will point to the bad file. Even though the symlink is in |
| 1213 | // the good directory, access to the symlink will be rejected since it |
| 1214 | // points to the bad file. |
| 1215 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1216 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1217 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1218 | |
| 1219 | CookieBlockingNetworkDelegate network_delegate; |
| 1220 | network_delegate.AddToWhitelist(real_good_dir); |
| 1221 | { |
| 1222 | TestDelegate d; |
| 1223 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1224 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1225 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1226 | r->Start(); |
| 1227 | base::RunLoop().Run(); |
| 1228 | // good_file_url should be allowed. |
| 1229 | EXPECT_FALSE(d.request_failed()); |
| 1230 | EXPECT_EQ(good_data, d.data_received()); |
| 1231 | } |
| 1232 | |
| 1233 | { |
| 1234 | TestDelegate d; |
| 1235 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1236 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1237 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1238 | r->Start(); |
| 1239 | base::RunLoop().Run(); |
| 1240 | // bad_file_url should be rejected. |
| 1241 | EXPECT_TRUE(d.request_failed()); |
| 1242 | EXPECT_EQ("", d.data_received()); |
| 1243 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1248 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1249 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1250 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1251 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1252 | |
| 1253 | // Create a good directory (will be whitelisted). |
| 1254 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1255 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1256 | |
| 1257 | // Create a bad directory (will not be whitelisted). |
| 1258 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1259 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1260 | |
| 1261 | // This symlink will point to the good directory. Access to the symlink |
| 1262 | // will be allowed as the symlink is in the good dir that'll be white |
| 1263 | // listed. |
| 1264 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1265 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1266 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1267 | // This symlink will point to the bad directory. Even though the symlink is |
| 1268 | // in the good directory, access to the symlink will be rejected since it |
| 1269 | // points to the bad directory. |
| 1270 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1271 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1272 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1273 | |
| 1274 | CookieBlockingNetworkDelegate network_delegate; |
| 1275 | network_delegate.AddToWhitelist(good_dir); |
| 1276 | { |
| 1277 | TestDelegate d; |
| 1278 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1279 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1280 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1281 | r->Start(); |
| 1282 | base::RunLoop().Run(); |
| 1283 | // good_file_url should be allowed. |
| 1284 | EXPECT_FALSE(d.request_failed()); |
| 1285 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1286 | } |
| 1287 | |
| 1288 | { |
| 1289 | TestDelegate d; |
| 1290 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1291 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1292 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1293 | r->Start(); |
| 1294 | base::RunLoop().Run(); |
| 1295 | // bad_file_url should be rejected. |
| 1296 | EXPECT_TRUE(d.request_failed()); |
| 1297 | EXPECT_EQ("", d.data_received()); |
| 1298 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1302 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1303 | |
| 1304 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1305 | // Put in mock resource provider. |
| 1306 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1307 | |
| 1308 | TestDelegate d; |
| 1309 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1310 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1311 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1312 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1313 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1314 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1315 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1316 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1317 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1318 | req->Start(); |
| 1319 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1320 | |
| 1321 | d.set_cancel_in_received_data_pending(true); |
| 1322 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1323 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | // Take out mock resource provider. |
| 1327 | NetModule::SetResourceProvider(NULL); |
| 1328 | } |
| 1329 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1330 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1331 | // Verify the general sanity of the the output of the file: |
| 1332 | // directory lister by checking for the output of a known existing |
| 1333 | // file. |
| 1334 | const char sentinel_name[] = "filedir-sentinel"; |
| 1335 | |
| 1336 | base::FilePath path; |
| 1337 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1338 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1339 | |
| 1340 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1341 | std::unique_ptr<URLRequest> req( |
| 1342 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1343 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1344 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1345 | base::RunLoop().Run(); |
| 1346 | |
| 1347 | // Generate entry for the sentinel file. |
| 1348 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1349 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1350 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1351 | EXPECT_GT(info.size, 0); |
| 1352 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1353 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1354 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1355 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1356 | info.last_modified); |
| 1357 | |
| 1358 | ASSERT_LT(0, d.bytes_received()); |
| 1359 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1360 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1361 | // Check for the entry generated for the "sentinel" file. |
| 1362 | const std::string& data = d.data_received(); |
| 1363 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1364 | } |
| 1365 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1366 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1367 | // There is an implicit redirect when loading a file path that matches a |
| 1368 | // directory and does not end with a slash. Ensure that following such |
| 1369 | // redirects does not crash. See http://crbug.com/18686. |
| 1370 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1371 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1372 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1373 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1374 | |
| 1375 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1376 | std::unique_ptr<URLRequest> req( |
| 1377 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1378 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1379 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1380 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1381 | |
| 1382 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1383 | ASSERT_LT(0, d.bytes_received()); |
| 1384 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1385 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | #if defined(OS_WIN) |
| 1389 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1390 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1391 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1392 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1393 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1394 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1395 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1396 | |
| 1397 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1398 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1399 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1400 | #endif // defined(OS_WIN) |
| 1401 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1402 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1403 | |
| 1404 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1405 | TestDelegate d; |
| 1406 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1407 | std::unique_ptr<URLRequest> r( |
| 1408 | default_context_.CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1409 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1410 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1411 | r->Start(); |
| 1412 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1413 | |
| 1414 | base::RunLoop().Run(); |
| 1415 | EXPECT_TRUE(d.request_failed()); |
| 1416 | } |
| 1417 | } |
| 1418 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1419 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1420 | TestURLRequestContext context; |
| 1421 | TestNetworkDelegate network_delegate; |
| 1422 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1423 | context.set_network_delegate(&network_delegate); |
| 1424 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1425 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1426 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1427 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1428 | req->SetReferrer("https://somewhere.com/"); |
| 1429 | |
| 1430 | req->Start(); |
| 1431 | base::RunLoop().Run(); |
| 1432 | EXPECT_TRUE(d.request_failed()); |
| 1433 | } |
| 1434 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1435 | #if defined(OS_WIN) |
| 1436 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1437 | base::FilePath app_path; |
| 1438 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1439 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1440 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1441 | |
| 1442 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1443 | |
| 1444 | base::win::ScopedCOMInitializer com_initializer; |
| 1445 | |
| 1446 | // Temporarily create a shortcut for test |
| 1447 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1448 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1449 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1450 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1451 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1452 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1453 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1454 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1455 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1456 | } |
| 1457 | |
| 1458 | TestDelegate d; |
| 1459 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1460 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1461 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1462 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1463 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1464 | r->Start(); |
| 1465 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1466 | |
| 1467 | base::RunLoop().Run(); |
| 1468 | |
| 1469 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1470 | GetFileAttributesEx(app_path.value().c_str(), |
| 1471 | GetFileExInfoStandard, &data); |
| 1472 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1473 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1474 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1475 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1476 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1477 | DWORD read_size; |
| 1478 | BOOL result; |
| 1479 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1480 | &read_size, NULL); |
| 1481 | std::string content(buffer.get(), read_size); |
| 1482 | CloseHandle(file); |
| 1483 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1484 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1485 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1486 | EXPECT_EQ(content, d.data_received()); |
| 1487 | } |
| 1488 | |
| 1489 | // Clean the shortcut |
| 1490 | DeleteFile(lnk_path.c_str()); |
| 1491 | } |
| 1492 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1493 | |
| 1494 | // Custom URLRequestJobs for use with interceptor tests |
| 1495 | class RestartTestJob : public URLRequestTestJob { |
| 1496 | public: |
| 1497 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1498 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1499 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1500 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1501 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1502 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1503 | }; |
| 1504 | |
| 1505 | class CancelTestJob : public URLRequestTestJob { |
| 1506 | public: |
| 1507 | explicit CancelTestJob(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 { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1511 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1512 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1513 | }; |
| 1514 | |
| 1515 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1516 | public: |
| 1517 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1518 | NetworkDelegate* network_delegate) |
| 1519 | : URLRequestTestJob(request, network_delegate, true) { |
| 1520 | } |
| 1521 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1522 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1523 | request_->Cancel(); |
| 1524 | this->NotifyRestartRequired(); |
| 1525 | } |
| 1526 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1527 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1528 | }; |
| 1529 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1530 | // An Interceptor for use with interceptor tests. |
| 1531 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1532 | public: |
| 1533 | // Static getters for canned response header and data strings. |
| 1534 | static std::string ok_data() { |
| 1535 | return URLRequestTestJob::test_data_1(); |
| 1536 | } |
| 1537 | |
| 1538 | static std::string ok_headers() { |
| 1539 | return URLRequestTestJob::test_headers(); |
| 1540 | } |
| 1541 | |
| 1542 | static std::string redirect_data() { |
| 1543 | return std::string(); |
| 1544 | } |
| 1545 | |
| 1546 | static std::string redirect_headers() { |
| 1547 | return URLRequestTestJob::test_redirect_headers(); |
| 1548 | } |
| 1549 | |
| 1550 | static std::string error_data() { |
| 1551 | return std::string("ohhh nooooo mr. bill!"); |
| 1552 | } |
| 1553 | |
| 1554 | static std::string error_headers() { |
| 1555 | return URLRequestTestJob::test_error_headers(); |
| 1556 | } |
| 1557 | |
| 1558 | MockURLRequestInterceptor() |
| 1559 | : intercept_main_request_(false), restart_main_request_(false), |
| 1560 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1561 | simulate_main_network_error_(false), |
| 1562 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1563 | intercept_final_response_(false), cancel_final_request_(false), |
| 1564 | use_url_request_http_job_(false), |
| 1565 | did_intercept_main_(false), did_restart_main_(false), |
| 1566 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1567 | did_simulate_error_main_(false), |
| 1568 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1569 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1570 | } |
| 1571 | |
| 1572 | ~MockURLRequestInterceptor() override { |
| 1573 | } |
| 1574 | |
| 1575 | // URLRequestInterceptor implementation: |
| 1576 | URLRequestJob* MaybeInterceptRequest( |
| 1577 | URLRequest* request, |
| 1578 | NetworkDelegate* network_delegate) const override { |
| 1579 | if (restart_main_request_) { |
| 1580 | restart_main_request_ = false; |
| 1581 | did_restart_main_ = true; |
| 1582 | return new RestartTestJob(request, network_delegate); |
| 1583 | } |
| 1584 | if (cancel_main_request_) { |
| 1585 | cancel_main_request_ = false; |
| 1586 | did_cancel_main_ = true; |
| 1587 | return new CancelTestJob(request, network_delegate); |
| 1588 | } |
| 1589 | if (cancel_then_restart_main_request_) { |
| 1590 | cancel_then_restart_main_request_ = false; |
| 1591 | did_cancel_then_restart_main_ = true; |
| 1592 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1593 | } |
| 1594 | if (simulate_main_network_error_) { |
| 1595 | simulate_main_network_error_ = false; |
| 1596 | did_simulate_error_main_ = true; |
| 1597 | if (use_url_request_http_job_) { |
| 1598 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1599 | } |
| 1600 | // This job will result in error since the requested URL is not one of the |
| 1601 | // URLs supported by these tests. |
| 1602 | return new URLRequestTestJob(request, network_delegate, true); |
| 1603 | } |
| 1604 | if (!intercept_main_request_) |
| 1605 | return nullptr; |
| 1606 | intercept_main_request_ = false; |
| 1607 | did_intercept_main_ = true; |
| 1608 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1609 | network_delegate, |
| 1610 | main_headers_, |
| 1611 | main_data_, |
| 1612 | true); |
| 1613 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1614 | return job; |
| 1615 | } |
| 1616 | |
| 1617 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1618 | NetworkDelegate* network_delegate, |
| 1619 | const GURL& location) const override { |
| 1620 | if (cancel_redirect_request_) { |
| 1621 | cancel_redirect_request_ = false; |
| 1622 | did_cancel_redirect_ = true; |
| 1623 | return new CancelTestJob(request, network_delegate); |
| 1624 | } |
| 1625 | if (!intercept_redirect_) |
| 1626 | return nullptr; |
| 1627 | intercept_redirect_ = false; |
| 1628 | did_intercept_redirect_ = true; |
| 1629 | if (use_url_request_http_job_) { |
| 1630 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1631 | } |
| 1632 | return new URLRequestTestJob(request, |
| 1633 | network_delegate, |
| 1634 | redirect_headers_, |
| 1635 | redirect_data_, |
| 1636 | true); |
| 1637 | } |
| 1638 | |
| 1639 | URLRequestJob* MaybeInterceptResponse( |
| 1640 | URLRequest* request, |
| 1641 | NetworkDelegate* network_delegate) const override { |
| 1642 | if (cancel_final_request_) { |
| 1643 | cancel_final_request_ = false; |
| 1644 | did_cancel_final_ = true; |
| 1645 | return new CancelTestJob(request, network_delegate); |
| 1646 | } |
| 1647 | if (!intercept_final_response_) |
| 1648 | return nullptr; |
| 1649 | intercept_final_response_ = false; |
| 1650 | did_intercept_final_ = true; |
| 1651 | if (use_url_request_http_job_) { |
| 1652 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1653 | } |
| 1654 | return new URLRequestTestJob(request, |
| 1655 | network_delegate, |
| 1656 | final_headers_, |
| 1657 | final_data_, |
| 1658 | true); |
| 1659 | } |
| 1660 | |
| 1661 | void set_intercept_main_request(bool intercept_main_request) { |
| 1662 | intercept_main_request_ = intercept_main_request; |
| 1663 | } |
| 1664 | |
| 1665 | void set_main_headers(const std::string& main_headers) { |
| 1666 | main_headers_ = main_headers; |
| 1667 | } |
| 1668 | |
| 1669 | void set_main_data(const std::string& main_data) { |
| 1670 | main_data_ = main_data; |
| 1671 | } |
| 1672 | |
| 1673 | void set_main_request_load_timing_info( |
| 1674 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1675 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1676 | } |
| 1677 | |
| 1678 | void set_restart_main_request(bool restart_main_request) { |
| 1679 | restart_main_request_ = restart_main_request; |
| 1680 | } |
| 1681 | |
| 1682 | void set_cancel_main_request(bool cancel_main_request) { |
| 1683 | cancel_main_request_ = cancel_main_request; |
| 1684 | } |
| 1685 | |
| 1686 | void set_cancel_then_restart_main_request( |
| 1687 | bool cancel_then_restart_main_request) { |
| 1688 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1689 | } |
| 1690 | |
| 1691 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1692 | simulate_main_network_error_ = simulate_main_network_error; |
| 1693 | } |
| 1694 | |
| 1695 | void set_intercept_redirect(bool intercept_redirect) { |
| 1696 | intercept_redirect_ = intercept_redirect; |
| 1697 | } |
| 1698 | |
| 1699 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1700 | redirect_headers_ = redirect_headers; |
| 1701 | } |
| 1702 | |
| 1703 | void set_redirect_data(const std::string& redirect_data) { |
| 1704 | redirect_data_ = redirect_data; |
| 1705 | } |
| 1706 | |
| 1707 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1708 | cancel_redirect_request_ = cancel_redirect_request; |
| 1709 | } |
| 1710 | |
| 1711 | void set_intercept_final_response(bool intercept_final_response) { |
| 1712 | intercept_final_response_ = intercept_final_response; |
| 1713 | } |
| 1714 | |
| 1715 | void set_final_headers(const std::string& final_headers) { |
| 1716 | final_headers_ = final_headers; |
| 1717 | } |
| 1718 | |
| 1719 | void set_final_data(const std::string& final_data) { |
| 1720 | final_data_ = final_data; |
| 1721 | } |
| 1722 | |
| 1723 | void set_cancel_final_request(bool cancel_final_request) { |
| 1724 | cancel_final_request_ = cancel_final_request; |
| 1725 | } |
| 1726 | |
| 1727 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1728 | use_url_request_http_job_ = use_url_request_http_job; |
| 1729 | } |
| 1730 | |
| 1731 | bool did_intercept_main() const { |
| 1732 | return did_intercept_main_; |
| 1733 | } |
| 1734 | |
| 1735 | bool did_restart_main() const { |
| 1736 | return did_restart_main_; |
| 1737 | } |
| 1738 | |
| 1739 | bool did_cancel_main() const { |
| 1740 | return did_cancel_main_; |
| 1741 | } |
| 1742 | |
| 1743 | bool did_cancel_then_restart_main() const { |
| 1744 | return did_cancel_then_restart_main_; |
| 1745 | } |
| 1746 | |
| 1747 | bool did_simulate_error_main() const { |
| 1748 | return did_simulate_error_main_; |
| 1749 | } |
| 1750 | |
| 1751 | bool did_intercept_redirect() const { |
| 1752 | return did_intercept_redirect_; |
| 1753 | } |
| 1754 | |
| 1755 | bool did_cancel_redirect() const { |
| 1756 | return did_cancel_redirect_; |
| 1757 | } |
| 1758 | |
| 1759 | bool did_intercept_final() const { |
| 1760 | return did_intercept_final_; |
| 1761 | } |
| 1762 | |
| 1763 | bool did_cancel_final() const { |
| 1764 | return did_cancel_final_; |
| 1765 | } |
| 1766 | |
| 1767 | private: |
| 1768 | // Indicate whether to intercept the main request, and if so specify the |
| 1769 | // response to return and the LoadTimingInfo to use. |
| 1770 | mutable bool intercept_main_request_; |
| 1771 | mutable std::string main_headers_; |
| 1772 | mutable std::string main_data_; |
| 1773 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1774 | |
| 1775 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1776 | mutable bool restart_main_request_; |
| 1777 | mutable bool cancel_main_request_; |
| 1778 | mutable bool cancel_then_restart_main_request_; |
| 1779 | mutable bool simulate_main_network_error_; |
| 1780 | |
| 1781 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1782 | // return. |
| 1783 | mutable bool intercept_redirect_; |
| 1784 | mutable std::string redirect_headers_; |
| 1785 | mutable std::string redirect_data_; |
| 1786 | |
| 1787 | // Cancel the request within MaybeInterceptRedirect. |
| 1788 | mutable bool cancel_redirect_request_; |
| 1789 | |
| 1790 | // Indicate whether to intercept the final response, and if so specify the |
| 1791 | // response to return. |
| 1792 | mutable bool intercept_final_response_; |
| 1793 | mutable std::string final_headers_; |
| 1794 | mutable std::string final_data_; |
| 1795 | |
| 1796 | // Cancel the final request within MaybeInterceptResponse. |
| 1797 | mutable bool cancel_final_request_; |
| 1798 | |
| 1799 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1800 | mutable bool use_url_request_http_job_; |
| 1801 | |
| 1802 | // These indicate if the interceptor did something or not. |
| 1803 | mutable bool did_intercept_main_; |
| 1804 | mutable bool did_restart_main_; |
| 1805 | mutable bool did_cancel_main_; |
| 1806 | mutable bool did_cancel_then_restart_main_; |
| 1807 | mutable bool did_simulate_error_main_; |
| 1808 | mutable bool did_intercept_redirect_; |
| 1809 | mutable bool did_cancel_redirect_; |
| 1810 | mutable bool did_intercept_final_; |
| 1811 | mutable bool did_cancel_final_; |
| 1812 | }; |
| 1813 | |
| 1814 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1815 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1816 | public: |
| 1817 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1818 | } |
| 1819 | |
| 1820 | ~URLRequestInterceptorTest() override { |
| 1821 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1822 | base::RunLoop().RunUntilIdle(); |
| 1823 | } |
| 1824 | |
| 1825 | void SetUpFactory() override { |
| 1826 | interceptor_ = new MockURLRequestInterceptor(); |
| 1827 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1828 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | MockURLRequestInterceptor* interceptor() const { |
| 1832 | return interceptor_; |
| 1833 | } |
| 1834 | |
| 1835 | private: |
| 1836 | MockURLRequestInterceptor* interceptor_; |
| 1837 | }; |
| 1838 | |
| 1839 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1840 | // Intercept the main request and respond with a simple response. |
| 1841 | interceptor()->set_intercept_main_request(true); |
| 1842 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1843 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1844 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1845 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1846 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1847 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1848 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1849 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1850 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1851 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1852 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1853 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1854 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1855 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1856 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1857 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1858 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1859 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1860 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1861 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1862 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1863 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1864 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1865 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1866 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1867 | EXPECT_EQ(1, d.response_started_count()); |
| 1868 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1869 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1870 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1871 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1872 | // Intercept the main request and respond with a redirect. |
| 1873 | interceptor()->set_intercept_main_request(true); |
| 1874 | interceptor()->set_main_headers( |
| 1875 | MockURLRequestInterceptor::redirect_headers()); |
| 1876 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1877 | |
| 1878 | // Intercept that redirect and respond with a final OK response. |
| 1879 | interceptor()->set_intercept_redirect(true); |
| 1880 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1881 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1882 | |
| 1883 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1884 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1885 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1886 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1887 | req->set_method("GET"); |
| 1888 | req->Start(); |
| 1889 | base::RunLoop().Run(); |
| 1890 | |
| 1891 | // Check that the interceptor got called as expected. |
| 1892 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1893 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1894 | |
| 1895 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1896 | int status = d.request_status(); |
| 1897 | EXPECT_EQ(OK, status); |
| 1898 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1899 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1900 | |
| 1901 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1902 | EXPECT_EQ(1, d.response_started_count()); |
| 1903 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1904 | } |
| 1905 | |
| 1906 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1907 | // Intercept the main request to generate a server error response. |
| 1908 | interceptor()->set_intercept_main_request(true); |
| 1909 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1910 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1911 | |
| 1912 | // Intercept that error and respond with an OK response. |
| 1913 | interceptor()->set_intercept_final_response(true); |
| 1914 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1915 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1916 | |
| 1917 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1918 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1919 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1920 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1921 | req->set_method("GET"); |
| 1922 | req->Start(); |
| 1923 | base::RunLoop().Run(); |
| 1924 | |
| 1925 | // Check that the interceptor got called as expected. |
| 1926 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1927 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1928 | |
| 1929 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1930 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1931 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1932 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1933 | EXPECT_EQ(1, d.response_started_count()); |
| 1934 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1935 | } |
| 1936 | |
| 1937 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1938 | // Intercept the main request to simulate a network error. |
| 1939 | interceptor()->set_simulate_main_network_error(true); |
| 1940 | |
| 1941 | // Intercept that error and respond with an OK response. |
| 1942 | interceptor()->set_intercept_final_response(true); |
| 1943 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1944 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1945 | |
| 1946 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1947 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1948 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1949 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1950 | req->set_method("GET"); |
| 1951 | req->Start(); |
| 1952 | base::RunLoop().Run(); |
| 1953 | |
| 1954 | // Check that the interceptor got called as expected. |
| 1955 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1956 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1957 | |
| 1958 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1959 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1960 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1961 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1962 | EXPECT_EQ(1, d.response_started_count()); |
| 1963 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1964 | } |
| 1965 | |
| 1966 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1967 | // Restart the main request. |
| 1968 | interceptor()->set_restart_main_request(true); |
| 1969 | |
| 1970 | // then intercept the new main request and respond with an OK response |
| 1971 | interceptor()->set_intercept_main_request(true); |
| 1972 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1973 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1974 | |
| 1975 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1976 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1977 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1978 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1979 | req->set_method("GET"); |
| 1980 | req->Start(); |
| 1981 | base::RunLoop().Run(); |
| 1982 | |
| 1983 | // Check that the interceptor got called as expected. |
| 1984 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1985 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1986 | |
| 1987 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1988 | int status = d.request_status(); |
| 1989 | EXPECT_EQ(OK, status); |
| 1990 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1991 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1992 | |
| 1993 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1994 | EXPECT_EQ(1, d.response_started_count()); |
| 1995 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1996 | } |
| 1997 | |
| 1998 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1999 | // Intercept the main request and cancel from within the restarted job. |
| 2000 | interceptor()->set_cancel_main_request(true); |
| 2001 | |
| 2002 | // Set up to intercept the final response and override it with an OK response. |
| 2003 | interceptor()->set_intercept_final_response(true); |
| 2004 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2005 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2006 | |
| 2007 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2008 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2009 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2010 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2011 | req->set_method("GET"); |
| 2012 | req->Start(); |
| 2013 | base::RunLoop().Run(); |
| 2014 | |
| 2015 | // Check that the interceptor got called as expected. |
| 2016 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2017 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2018 | |
| 2019 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2020 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2024 | // Intercept the main request and respond with a redirect. |
| 2025 | interceptor()->set_intercept_main_request(true); |
| 2026 | interceptor()->set_main_headers( |
| 2027 | MockURLRequestInterceptor::redirect_headers()); |
| 2028 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2029 | |
| 2030 | // Intercept the redirect and cancel from within that job. |
| 2031 | interceptor()->set_cancel_redirect_request(true); |
| 2032 | |
| 2033 | // Set up to intercept the final response and override it with an OK response. |
| 2034 | interceptor()->set_intercept_final_response(true); |
| 2035 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2036 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2037 | |
| 2038 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2039 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2040 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2041 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2042 | req->set_method("GET"); |
| 2043 | req->Start(); |
| 2044 | base::RunLoop().Run(); |
| 2045 | |
| 2046 | // Check that the interceptor got called as expected. |
| 2047 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2048 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2049 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2050 | |
| 2051 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2052 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2056 | // Intercept the main request to simulate a network error. |
| 2057 | interceptor()->set_simulate_main_network_error(true); |
| 2058 | |
| 2059 | // Set up to intercept final the response and cancel from within that job. |
| 2060 | interceptor()->set_cancel_final_request(true); |
| 2061 | |
| 2062 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2063 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2064 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2065 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2066 | req->set_method("GET"); |
| 2067 | req->Start(); |
| 2068 | base::RunLoop().Run(); |
| 2069 | |
| 2070 | // Check that the interceptor got called as expected. |
| 2071 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2072 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2073 | |
| 2074 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2075 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2079 | // Intercept the main request and cancel then restart from within that job. |
| 2080 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2081 | |
| 2082 | // Set up to intercept the final response and override it with an OK response. |
| 2083 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2084 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2085 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2086 | |
| 2087 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2088 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2089 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2090 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2091 | req->set_method("GET"); |
| 2092 | req->Start(); |
| 2093 | base::RunLoop().Run(); |
| 2094 | |
| 2095 | // Check that the interceptor got called as expected. |
| 2096 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2097 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2098 | |
| 2099 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2100 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2104 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2105 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2106 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2107 | int connect_time_flags, |
| 2108 | bool used_proxy) { |
| 2109 | LoadTimingInfo load_timing; |
| 2110 | load_timing.socket_log_id = 1; |
| 2111 | |
| 2112 | if (used_proxy) { |
| 2113 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2114 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2115 | } |
| 2116 | |
| 2117 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2118 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2119 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2120 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2121 | } |
| 2122 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2123 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2124 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2125 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2126 | } |
| 2127 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2128 | |
| 2129 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2130 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2131 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2132 | return load_timing; |
| 2133 | } |
| 2134 | |
| 2135 | // Same as above, but in the case of a reused socket. |
| 2136 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2137 | bool used_proxy) { |
| 2138 | LoadTimingInfo load_timing; |
| 2139 | load_timing.socket_log_id = 1; |
| 2140 | load_timing.socket_reused = true; |
| 2141 | |
| 2142 | if (used_proxy) { |
| 2143 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2144 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2145 | } |
| 2146 | |
| 2147 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2148 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2149 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2150 | return load_timing; |
| 2151 | } |
| 2152 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2153 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2154 | const LoadTimingInfo& job_load_timing, |
| 2155 | const URLRequestContext& context, |
| 2156 | MockURLRequestInterceptor* interceptor) { |
| 2157 | interceptor->set_intercept_main_request(true); |
| 2158 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2159 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2160 | std::unique_ptr<URLRequest> req( |
| 2161 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2162 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2163 | req->Start(); |
| 2164 | base::RunLoop().Run(); |
| 2165 | |
| 2166 | LoadTimingInfo resulting_load_timing; |
| 2167 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2168 | |
| 2169 | // None of these should be modified by the URLRequest. |
| 2170 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2171 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2172 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2173 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 2174 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2175 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2176 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2177 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2178 | |
| 2179 | return resulting_load_timing; |
| 2180 | } |
| 2181 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2182 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2183 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2184 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2185 | LoadTimingInfo job_load_timing = |
| 2186 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2187 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2188 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2189 | RunURLRequestInterceptorLoadTimingTest( |
| 2190 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2191 | |
| 2192 | // Nothing should have been changed by the URLRequest. |
| 2193 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2194 | load_timing_result.proxy_resolve_start); |
| 2195 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2196 | load_timing_result.proxy_resolve_end); |
| 2197 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2198 | load_timing_result.connect_timing.dns_start); |
| 2199 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2200 | load_timing_result.connect_timing.dns_end); |
| 2201 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2202 | load_timing_result.connect_timing.connect_start); |
| 2203 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2204 | load_timing_result.connect_timing.connect_end); |
| 2205 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2206 | load_timing_result.connect_timing.ssl_start); |
| 2207 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2208 | load_timing_result.connect_timing.ssl_end); |
| 2209 | |
| 2210 | // Redundant sanity check. |
| 2211 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2212 | } |
| 2213 | |
| 2214 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2215 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2216 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2217 | LoadTimingInfo job_load_timing = |
| 2218 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2219 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2220 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2221 | RunURLRequestInterceptorLoadTimingTest( |
| 2222 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2223 | |
| 2224 | // Nothing should have been changed by the URLRequest. |
| 2225 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2226 | load_timing_result.proxy_resolve_start); |
| 2227 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2228 | load_timing_result.proxy_resolve_end); |
| 2229 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2230 | load_timing_result.connect_timing.dns_start); |
| 2231 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2232 | load_timing_result.connect_timing.dns_end); |
| 2233 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2234 | load_timing_result.connect_timing.connect_start); |
| 2235 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2236 | load_timing_result.connect_timing.connect_end); |
| 2237 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2238 | load_timing_result.connect_timing.ssl_start); |
| 2239 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2240 | load_timing_result.connect_timing.ssl_end); |
| 2241 | |
| 2242 | // Redundant sanity check. |
| 2243 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2244 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2245 | } |
| 2246 | |
| 2247 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2248 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2249 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2250 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2251 | // |
| 2252 | // 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] | 2253 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2254 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2255 | LoadTimingInfo job_load_timing = |
| 2256 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2257 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2258 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2259 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2260 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2261 | job_load_timing.connect_timing.connect_start = |
| 2262 | now - base::TimeDelta::FromDays(2); |
| 2263 | job_load_timing.connect_timing.connect_end = |
| 2264 | now - base::TimeDelta::FromDays(1); |
| 2265 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2266 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2267 | RunURLRequestInterceptorLoadTimingTest( |
| 2268 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2269 | |
| 2270 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2271 | // request_start. |
| 2272 | EXPECT_EQ(load_timing_result.request_start, |
| 2273 | load_timing_result.proxy_resolve_start); |
| 2274 | EXPECT_EQ(load_timing_result.request_start, |
| 2275 | load_timing_result.proxy_resolve_end); |
| 2276 | EXPECT_EQ(load_timing_result.request_start, |
| 2277 | load_timing_result.connect_timing.dns_start); |
| 2278 | EXPECT_EQ(load_timing_result.request_start, |
| 2279 | load_timing_result.connect_timing.dns_end); |
| 2280 | EXPECT_EQ(load_timing_result.request_start, |
| 2281 | load_timing_result.connect_timing.connect_start); |
| 2282 | EXPECT_EQ(load_timing_result.request_start, |
| 2283 | load_timing_result.connect_timing.connect_end); |
| 2284 | |
| 2285 | // Other times should have been left null. |
| 2286 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2287 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2288 | } |
| 2289 | |
| 2290 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2291 | TEST_F(URLRequestInterceptorTest, |
| 2292 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2293 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2294 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2295 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2296 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2297 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2298 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2299 | RunURLRequestInterceptorLoadTimingTest( |
| 2300 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2301 | |
| 2302 | // Proxy times and connect times should all be replaced with request_start. |
| 2303 | EXPECT_EQ(load_timing_result.request_start, |
| 2304 | load_timing_result.proxy_resolve_start); |
| 2305 | EXPECT_EQ(load_timing_result.request_start, |
| 2306 | load_timing_result.proxy_resolve_end); |
| 2307 | |
| 2308 | // Other times should have been left null. |
| 2309 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2310 | } |
| 2311 | |
| 2312 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2313 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2314 | // not considered reused in this test (May be a preconnect). |
| 2315 | // |
| 2316 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2317 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2318 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2319 | LoadTimingInfo job_load_timing = |
| 2320 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2321 | job_load_timing.connect_timing.connect_start = |
| 2322 | now - base::TimeDelta::FromDays(1); |
| 2323 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2324 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2325 | job_load_timing.connect_timing.connect_end = |
| 2326 | now - base::TimeDelta::FromDays(4); |
| 2327 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2328 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2329 | RunURLRequestInterceptorLoadTimingTest( |
| 2330 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2331 | |
| 2332 | // Connect times, and SSL times should be replaced with request_start. |
| 2333 | EXPECT_EQ(load_timing_result.request_start, |
| 2334 | load_timing_result.connect_timing.connect_start); |
| 2335 | EXPECT_EQ(load_timing_result.request_start, |
| 2336 | load_timing_result.connect_timing.ssl_start); |
| 2337 | EXPECT_EQ(load_timing_result.request_start, |
| 2338 | load_timing_result.connect_timing.ssl_end); |
| 2339 | EXPECT_EQ(load_timing_result.request_start, |
| 2340 | load_timing_result.connect_timing.connect_end); |
| 2341 | |
| 2342 | // Other times should have been left null. |
| 2343 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2344 | } |
| 2345 | |
| 2346 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2347 | // |request_start|, due to reusing a connected socket in the case that there |
| 2348 | // are also proxy times. The connected socket is not considered reused in this |
| 2349 | // test (May be a preconnect). |
| 2350 | // |
| 2351 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2352 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2353 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2354 | LoadTimingInfo job_load_timing = |
| 2355 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2356 | job_load_timing.connect_timing.connect_start = |
| 2357 | now - base::TimeDelta::FromDays(1); |
| 2358 | job_load_timing.connect_timing.connect_end = |
| 2359 | now - base::TimeDelta::FromDays(2); |
| 2360 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2361 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2362 | RunURLRequestInterceptorLoadTimingTest( |
| 2363 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2364 | |
| 2365 | // Connect times should be replaced with proxy_resolve_end. |
| 2366 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2367 | load_timing_result.connect_timing.connect_start); |
| 2368 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2369 | load_timing_result.connect_timing.connect_end); |
| 2370 | |
| 2371 | // Other times should have been left null. |
| 2372 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2373 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2374 | } |
| 2375 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2376 | // Check that two different URL requests have different identifiers. |
| 2377 | TEST_F(URLRequestTest, Identifiers) { |
| 2378 | TestDelegate d; |
| 2379 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2380 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2381 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2382 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2383 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2384 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2385 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2386 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2387 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2388 | } |
| 2389 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2390 | #if defined(OS_IOS) |
| 2391 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2392 | // the network delegate. crbug.com/496743 |
| 2393 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2394 | #else |
| 2395 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2396 | #endif |
| 2397 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2398 | MockHostResolver host_resolver; |
| 2399 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2400 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2401 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2402 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2403 | |
| 2404 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2405 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2406 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2407 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2408 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2409 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2410 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2411 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2412 | |
| 2413 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2414 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2415 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2416 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2417 | |
| 2418 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2419 | EXPECT_THAT(network_delegate.last_error(), |
| 2420 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2421 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2422 | } |
| 2423 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2424 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2425 | // content is empty. |
| 2426 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2427 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2428 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2429 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2430 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2431 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2432 | EXPECT_EQ("", d.data_received()); |
| 2433 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2434 | } |
| 2435 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2436 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2437 | // correctly, both before and after start. |
| 2438 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2439 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2440 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2441 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2442 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2443 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2444 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | req->SetPriority(LOW); |
| 2446 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2447 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2448 | req->Start(); |
| 2449 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2450 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2451 | req->SetPriority(MEDIUM); |
| 2452 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2456 | // Start on it. |
| 2457 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2458 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2459 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2460 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2461 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2462 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2463 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2464 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2465 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2466 | req.get(), &default_network_delegate_, &job_priority)); |
| 2467 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2468 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2469 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2470 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2471 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2472 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2473 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | // Make sure that URLRequest passes on its priority updates to its |
| 2477 | // job. |
| 2478 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2479 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2480 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2481 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2482 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2483 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2484 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2485 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2486 | req.get(), &default_network_delegate_, &job_priority)); |
| 2487 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2488 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2489 | req->SetPriority(LOW); |
| 2490 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2491 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2492 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2493 | req->SetPriority(MEDIUM); |
| 2494 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2495 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2496 | } |
| 2497 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2498 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2499 | // is MAXIMUM_PRIORITY. |
| 2500 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2501 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2502 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2503 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2504 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2505 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2506 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2507 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2508 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2509 | req.get(), &default_network_delegate_, &job_priority)); |
| 2510 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2511 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2512 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2513 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2514 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2515 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2516 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2517 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2518 | req->Start(); |
| 2519 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2520 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2521 | } |
| 2522 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2523 | namespace { |
| 2524 | |
| 2525 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2526 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2527 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2528 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2529 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2530 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2531 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2532 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2533 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2534 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2535 | } // namespace |
| 2536 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2537 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2538 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2539 | ASSERT_TRUE(test_server.Start()); |
| 2540 | |
| 2541 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2542 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2543 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2544 | |
| 2545 | // Set up a cookie. |
| 2546 | { |
| 2547 | TestNetworkDelegate network_delegate; |
| 2548 | context.set_network_delegate(&network_delegate); |
| 2549 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2550 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2551 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2552 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2553 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2554 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2555 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2556 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2557 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2558 | } |
| 2559 | |
| 2560 | // Verify that the cookie is set. |
| 2561 | { |
| 2562 | TestNetworkDelegate network_delegate; |
| 2563 | context.set_network_delegate(&network_delegate); |
| 2564 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2565 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2566 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2567 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2568 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2569 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2570 | |
| 2571 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2572 | != std::string::npos); |
| 2573 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2574 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2575 | } |
| 2576 | } |
| 2577 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2578 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2579 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2580 | ASSERT_TRUE(test_server.Start()); |
| 2581 | |
| 2582 | // Set up a cookie. |
| 2583 | { |
| 2584 | TestNetworkDelegate network_delegate; |
| 2585 | default_context_.set_network_delegate(&network_delegate); |
| 2586 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2587 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2588 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2589 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2590 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2591 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2592 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2593 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2594 | } |
| 2595 | |
| 2596 | // Verify that the cookie is set. |
| 2597 | { |
| 2598 | TestNetworkDelegate network_delegate; |
| 2599 | default_context_.set_network_delegate(&network_delegate); |
| 2600 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2601 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2602 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2603 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2604 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2605 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2606 | |
| 2607 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2608 | != std::string::npos); |
| 2609 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2610 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2611 | } |
| 2612 | |
| 2613 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2614 | { |
| 2615 | TestNetworkDelegate network_delegate; |
| 2616 | default_context_.set_network_delegate(&network_delegate); |
| 2617 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2618 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2619 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2620 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2621 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2622 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2623 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2624 | |
| 2625 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2626 | == std::string::npos); |
| 2627 | |
| 2628 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2629 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2630 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2635 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2636 | ASSERT_TRUE(test_server.Start()); |
| 2637 | |
| 2638 | // Set up a cookie. |
| 2639 | { |
| 2640 | TestNetworkDelegate network_delegate; |
| 2641 | default_context_.set_network_delegate(&network_delegate); |
| 2642 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2643 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2644 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2645 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2646 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2647 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2648 | |
| 2649 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2650 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2651 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2652 | } |
| 2653 | |
| 2654 | // Try to set-up another cookie and update the previous cookie. |
| 2655 | { |
| 2656 | TestNetworkDelegate network_delegate; |
| 2657 | default_context_.set_network_delegate(&network_delegate); |
| 2658 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2659 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2660 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2661 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2662 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2663 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2664 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2665 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2666 | |
| 2667 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2668 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2669 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2670 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2671 | } |
| 2672 | |
| 2673 | // Verify the cookies weren't saved or updated. |
| 2674 | { |
| 2675 | TestNetworkDelegate network_delegate; |
| 2676 | default_context_.set_network_delegate(&network_delegate); |
| 2677 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2678 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2679 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2680 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2681 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2682 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2683 | |
| 2684 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2685 | == std::string::npos); |
| 2686 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2687 | != std::string::npos); |
| 2688 | |
| 2689 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2690 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2691 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2696 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2697 | ASSERT_TRUE(test_server.Start()); |
| 2698 | |
| 2699 | // Set up a cookie. |
| 2700 | { |
| 2701 | TestNetworkDelegate network_delegate; |
| 2702 | default_context_.set_network_delegate(&network_delegate); |
| 2703 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2704 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2705 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2706 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2707 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2708 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2709 | |
| 2710 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2711 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2712 | } |
| 2713 | |
| 2714 | // Verify that the cookie is set. |
| 2715 | { |
| 2716 | TestNetworkDelegate network_delegate; |
| 2717 | default_context_.set_network_delegate(&network_delegate); |
| 2718 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2719 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2720 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2721 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2722 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2723 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2724 | |
| 2725 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2726 | != std::string::npos); |
| 2727 | |
| 2728 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2729 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2730 | } |
| 2731 | |
| 2732 | // Verify that the cookie isn't sent. |
| 2733 | { |
| 2734 | TestNetworkDelegate network_delegate; |
| 2735 | default_context_.set_network_delegate(&network_delegate); |
| 2736 | TestDelegate d; |
| 2737 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2738 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2739 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2740 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2741 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2742 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2743 | |
| 2744 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2745 | == std::string::npos); |
| 2746 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2747 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2748 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2749 | } |
| 2750 | } |
| 2751 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2752 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2753 | #if defined(OS_IOS) |
| 2754 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2755 | #else |
| 2756 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2757 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2758 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2759 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2760 | ASSERT_TRUE(test_server.Start()); |
| 2761 | |
| 2762 | // Set up a cookie. |
| 2763 | { |
| 2764 | TestNetworkDelegate network_delegate; |
| 2765 | default_context_.set_network_delegate(&network_delegate); |
| 2766 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2767 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2768 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2769 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2770 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2771 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2772 | |
| 2773 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2774 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2775 | } |
| 2776 | |
| 2777 | // Try to set-up another cookie and update the previous cookie. |
| 2778 | { |
| 2779 | TestNetworkDelegate network_delegate; |
| 2780 | default_context_.set_network_delegate(&network_delegate); |
| 2781 | TestDelegate d; |
| 2782 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2783 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2784 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2785 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2786 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2787 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2788 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2789 | |
| 2790 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2791 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2792 | } |
| 2793 | |
| 2794 | // Verify the cookies weren't saved or updated. |
| 2795 | { |
| 2796 | TestNetworkDelegate network_delegate; |
| 2797 | default_context_.set_network_delegate(&network_delegate); |
| 2798 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2799 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2800 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2801 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2802 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2803 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2804 | |
| 2805 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2806 | == std::string::npos); |
| 2807 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2808 | != std::string::npos); |
| 2809 | |
| 2810 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2811 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2816 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2817 | ASSERT_TRUE(test_server.Start()); |
| 2818 | |
| 2819 | // Set up an empty cookie. |
| 2820 | { |
| 2821 | TestNetworkDelegate network_delegate; |
| 2822 | default_context_.set_network_delegate(&network_delegate); |
| 2823 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2824 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2825 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2826 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2827 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2828 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2829 | |
| 2830 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2831 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2832 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2837 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2838 | ASSERT_TRUE(test_server.Start()); |
| 2839 | |
| 2840 | // Set up a cookie. |
| 2841 | { |
| 2842 | TestNetworkDelegate network_delegate; |
| 2843 | default_context_.set_network_delegate(&network_delegate); |
| 2844 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2845 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2846 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2847 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2848 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2849 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2850 | |
| 2851 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2852 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2853 | } |
| 2854 | |
| 2855 | // Verify that the cookie is set. |
| 2856 | { |
| 2857 | TestNetworkDelegate network_delegate; |
| 2858 | default_context_.set_network_delegate(&network_delegate); |
| 2859 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2860 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2861 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2862 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2863 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2864 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2865 | |
| 2866 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2867 | != std::string::npos); |
| 2868 | |
| 2869 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2870 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2871 | } |
| 2872 | |
| 2873 | // Verify that the cookie isn't sent. |
| 2874 | { |
| 2875 | TestNetworkDelegate network_delegate; |
| 2876 | default_context_.set_network_delegate(&network_delegate); |
| 2877 | TestDelegate d; |
| 2878 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2879 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2880 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2881 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2882 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2883 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2884 | |
| 2885 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2886 | == std::string::npos); |
| 2887 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2888 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2889 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2894 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2895 | ASSERT_TRUE(test_server.Start()); |
| 2896 | |
| 2897 | // Set up a cookie. |
| 2898 | { |
| 2899 | TestNetworkDelegate network_delegate; |
| 2900 | default_context_.set_network_delegate(&network_delegate); |
| 2901 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2902 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2903 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2904 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2905 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2906 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2907 | |
| 2908 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2909 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2910 | } |
| 2911 | |
| 2912 | // Try to set-up another cookie and update the previous cookie. |
| 2913 | { |
| 2914 | TestNetworkDelegate network_delegate; |
| 2915 | default_context_.set_network_delegate(&network_delegate); |
| 2916 | TestDelegate d; |
| 2917 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2918 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2919 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2920 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2921 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2922 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2923 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2924 | |
| 2925 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2926 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2927 | } |
| 2928 | |
| 2929 | // Verify the cookies weren't saved or updated. |
| 2930 | { |
| 2931 | TestNetworkDelegate network_delegate; |
| 2932 | default_context_.set_network_delegate(&network_delegate); |
| 2933 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2934 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2935 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2936 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2937 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2938 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2939 | |
| 2940 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2941 | == std::string::npos); |
| 2942 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2943 | != std::string::npos); |
| 2944 | |
| 2945 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2946 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2947 | } |
| 2948 | } |
| 2949 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2950 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2951 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2952 | ASSERT_TRUE(test_server.Start()); |
| 2953 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2954 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2955 | default_context_.set_network_delegate(&network_delegate); |
| 2956 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2957 | const std::string kHost = "example.test"; |
| 2958 | const std::string kSubHost = "subdomain.example.test"; |
| 2959 | const std::string kCrossHost = "cross-origin.test"; |
| 2960 | |
| 2961 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2962 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2963 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2964 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2965 | test_server.GetURL(kHost, |
| 2966 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2967 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2968 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2969 | req->Start(); |
| 2970 | base::RunLoop().Run(); |
| 2971 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2972 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2973 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2974 | } |
| 2975 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2976 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2977 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2978 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2979 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2980 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2981 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2982 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 2983 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2984 | req->Start(); |
| 2985 | base::RunLoop().Run(); |
| 2986 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2987 | EXPECT_NE(std::string::npos, |
| 2988 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2989 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2990 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2991 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2992 | } |
| 2993 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2994 | // Verify that both cookies are sent when the request has no initiator (can |
| 2995 | // happen for main frame browser-initiated navigations). |
| 2996 | { |
| 2997 | TestDelegate d; |
| 2998 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2999 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3001 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3002 | req->Start(); |
| 3003 | base::RunLoop().Run(); |
| 3004 | |
| 3005 | EXPECT_NE(std::string::npos, |
| 3006 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3007 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3008 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3009 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3010 | } |
| 3011 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3012 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3013 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3014 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3015 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3016 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3017 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3018 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3019 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3020 | req->Start(); |
| 3021 | base::RunLoop().Run(); |
| 3022 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3023 | EXPECT_NE(std::string::npos, |
| 3024 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3025 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3026 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3027 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3028 | } |
| 3029 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3030 | // Verify that neither cookie is not sent for cross-site requests. |
| 3031 | { |
| 3032 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3033 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3034 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3035 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3036 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3037 | req->set_initiator( |
| 3038 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3039 | req->Start(); |
| 3040 | base::RunLoop().Run(); |
| 3041 | |
| 3042 | EXPECT_EQ(std::string::npos, |
| 3043 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3044 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3045 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3046 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3047 | } |
| 3048 | |
| 3049 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3050 | // method is "safe". |
| 3051 | { |
| 3052 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3053 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3054 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3055 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3056 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3057 | req->set_initiator( |
| 3058 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3059 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3060 | req->Start(); |
| 3061 | base::RunLoop().Run(); |
| 3062 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3063 | EXPECT_EQ(std::string::npos, |
| 3064 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3065 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3066 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3067 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3068 | } |
| 3069 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3070 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3071 | // method is unsafe (e.g. POST). |
| 3072 | { |
| 3073 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3074 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3075 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3076 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3077 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3078 | req->set_initiator( |
| 3079 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3080 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3081 | req->Start(); |
| 3082 | base::RunLoop().Run(); |
| 3083 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3084 | EXPECT_EQ(std::string::npos, |
| 3085 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3086 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3087 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3088 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3089 | } |
| 3090 | } |
| 3091 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3092 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3093 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3094 | EmbeddedTestServer http_server; |
| 3095 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3096 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3097 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3098 | https_server.AddDefaultHandlers( |
| 3099 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3100 | ASSERT_TRUE(http_server.Start()); |
| 3101 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3102 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3103 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3104 | TestURLRequestContext context(true); |
| 3105 | context.set_network_delegate(&network_delegate); |
| 3106 | context.Init(); |
| 3107 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3108 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3109 | { |
| 3110 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3111 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3112 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3113 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3114 | req->Start(); |
| 3115 | base::RunLoop().Run(); |
| 3116 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3117 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3118 | } |
| 3119 | |
| 3120 | // Verify that the cookie is not set. |
| 3121 | { |
| 3122 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3123 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3124 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3125 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3126 | req->Start(); |
| 3127 | base::RunLoop().Run(); |
| 3128 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3129 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3130 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3131 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3132 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3133 | } |
| 3134 | } |
| 3135 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3136 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3137 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3138 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3139 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3140 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3141 | |
| 3142 | TestNetworkDelegate network_delegate; |
| 3143 | TestURLRequestContext context(true); |
| 3144 | context.set_network_delegate(&network_delegate); |
| 3145 | context.Init(); |
| 3146 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3147 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3148 | { |
| 3149 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3150 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3151 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3152 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3153 | req->Start(); |
| 3154 | base::RunLoop().Run(); |
| 3155 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3156 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3157 | } |
| 3158 | |
| 3159 | // Verify that the cookie is not set. |
| 3160 | { |
| 3161 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3162 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3163 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3164 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3165 | req->Start(); |
| 3166 | base::RunLoop().Run(); |
| 3167 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3168 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3169 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3170 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3171 | } |
| 3172 | } |
| 3173 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3174 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3175 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3176 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3177 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3178 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3179 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3180 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3181 | TestURLRequestContext context(true); |
| 3182 | context.set_network_delegate(&network_delegate); |
| 3183 | context.Init(); |
| 3184 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3185 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3186 | { |
| 3187 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3188 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3189 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3190 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3191 | req->Start(); |
| 3192 | base::RunLoop().Run(); |
| 3193 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3194 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3195 | } |
| 3196 | |
| 3197 | // Verify that the cookie is set. |
| 3198 | { |
| 3199 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3200 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3201 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3202 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3203 | req->Start(); |
| 3204 | base::RunLoop().Run(); |
| 3205 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3206 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3207 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3208 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3213 | // cookies are enabled. |
| 3214 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3215 | EmbeddedTestServer http_server; |
| 3216 | http_server.AddDefaultHandlers( |
| 3217 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3218 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3219 | https_server.AddDefaultHandlers( |
| 3220 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3221 | ASSERT_TRUE(http_server.Start()); |
| 3222 | ASSERT_TRUE(https_server.Start()); |
| 3223 | |
| 3224 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3225 | TestURLRequestContext context(true); |
| 3226 | context.set_network_delegate(&network_delegate); |
| 3227 | context.Init(); |
| 3228 | |
| 3229 | // Try to set a Secure cookie, with experimental features enabled. |
| 3230 | { |
| 3231 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3232 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3233 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3234 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3235 | req->Start(); |
| 3236 | base::RunLoop().Run(); |
| 3237 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3238 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3239 | } |
| 3240 | |
| 3241 | // Verify that the cookie is not set. |
| 3242 | { |
| 3243 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3244 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3245 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3247 | req->Start(); |
| 3248 | base::RunLoop().Run(); |
| 3249 | |
| 3250 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3251 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3252 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 3257 | // cookies are enabled. |
| 3258 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 3259 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3260 | https_server.AddDefaultHandlers( |
| 3261 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3262 | ASSERT_TRUE(https_server.Start()); |
| 3263 | |
| 3264 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3265 | TestURLRequestContext context(true); |
| 3266 | context.set_network_delegate(&network_delegate); |
| 3267 | context.Init(); |
| 3268 | |
| 3269 | // Try to set a Secure cookie, with experimental features enabled. |
| 3270 | { |
| 3271 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3272 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3273 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3274 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3275 | req->Start(); |
| 3276 | base::RunLoop().Run(); |
| 3277 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3278 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3279 | } |
| 3280 | |
| 3281 | // Verify that the cookie is not set. |
| 3282 | { |
| 3283 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3284 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3285 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3286 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3287 | req->Start(); |
| 3288 | base::RunLoop().Run(); |
| 3289 | |
| 3290 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3291 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3292 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3293 | } |
| 3294 | } |
| 3295 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3296 | TEST_F(URLRequestTest, CookieAgeMetrics) { |
| 3297 | EmbeddedTestServer http_server; |
| 3298 | http_server.AddDefaultHandlers( |
| 3299 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3300 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3301 | https_server.AddDefaultHandlers( |
| 3302 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3303 | ASSERT_TRUE(http_server.Start()); |
| 3304 | ASSERT_TRUE(https_server.Start()); |
| 3305 | |
| 3306 | TestNetworkDelegate network_delegate; |
| 3307 | default_context_.set_network_delegate(&network_delegate); |
| 3308 | base::HistogramTester histograms; |
| 3309 | |
| 3310 | const std::string kHost = "example.test"; |
| 3311 | const std::string kCrossHost = "cross-origin.test"; |
| 3312 | |
| 3313 | // Set a test cookie. |
| 3314 | { |
| 3315 | TestDelegate d; |
| 3316 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3317 | http_server.GetURL(kHost, "/set-cookie?cookie=value"), DEFAULT_PRIORITY, |
| 3318 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3319 | req->Start(); |
| 3320 | base::RunLoop().Run(); |
| 3321 | ASSERT_EQ(1, network_delegate.set_cookie_count()); |
| 3322 | } |
| 3323 | |
| 3324 | // Make a secure request to `example.test`: we shouldn't record data. |
| 3325 | { |
| 3326 | TestDelegate d; |
| 3327 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3328 | https_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3329 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3330 | req->Start(); |
| 3331 | base::RunLoop().Run(); |
| 3332 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3333 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3334 | } |
| 3335 | |
| 3336 | // Make a non-secure same-site request. |
| 3337 | { |
| 3338 | TestDelegate d; |
| 3339 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3340 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3341 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3342 | req->set_site_for_cookies(http_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3343 | req->set_initiator(url::Origin::Create(http_server.GetURL(kHost, "/"))); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3344 | req->Start(); |
| 3345 | base::RunLoop().Run(); |
| 3346 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3347 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3348 | } |
| 3349 | |
| 3350 | // Make a non-secure cross-site request. |
| 3351 | { |
| 3352 | TestDelegate d; |
| 3353 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3354 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3355 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3356 | req->set_site_for_cookies(http_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3357 | req->set_initiator( |
| 3358 | url::Origin::Create(http_server.GetURL(kCrossHost, "/"))); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3359 | req->Start(); |
| 3360 | base::RunLoop().Run(); |
| 3361 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 1); |
| 3362 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3363 | } |
| 3364 | } |
| 3365 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3366 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3367 | // rather than a spawned test server because the connection used to talk to |
| 3368 | // the test server is affected by entering suspend mode on Android. |
| 3369 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3370 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3371 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3372 | |
| 3373 | URLRequestFailedJob::AddUrlHandler(); |
| 3374 | |
| 3375 | TestDelegate d; |
| 3376 | // Request that just hangs. |
| 3377 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3378 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3379 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3380 | r->Start(); |
| 3381 | |
| 3382 | power_monitor_source->Suspend(); |
| 3383 | // Wait for the suspend notification to cause the request to fail. |
| 3384 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3385 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3386 | EXPECT_TRUE(d.request_failed()); |
| 3387 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3388 | |
| 3389 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3390 | |
| 3391 | // Shouldn't be needed, but just in case. |
| 3392 | power_monitor_source->Resume(); |
| 3393 | } |
| 3394 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3395 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3396 | // value for the |fixed_date| argument given to the constructor. |
| 3397 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3398 | public: |
| 3399 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3400 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3401 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3402 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3403 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3404 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3405 | URLRequest* request, |
| 3406 | const CompletionCallback& callback, |
| 3407 | const HttpResponseHeaders* original_response_headers, |
| 3408 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3409 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3410 | |
| 3411 | private: |
| 3412 | std::string fixed_date_; |
| 3413 | |
| 3414 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3415 | }; |
| 3416 | |
| 3417 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3418 | URLRequest* request, |
| 3419 | const CompletionCallback& callback, |
| 3420 | const HttpResponseHeaders* original_response_headers, |
| 3421 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3422 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3423 | HttpResponseHeaders* new_response_headers = |
| 3424 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3425 | |
| 3426 | new_response_headers->RemoveHeader("Date"); |
| 3427 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3428 | |
| 3429 | *override_response_headers = new_response_headers; |
| 3430 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3431 | callback, |
| 3432 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3433 | override_response_headers, |
| 3434 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | // Test that cookie expiration times are adjusted for server/client clock |
| 3438 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3439 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3440 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3441 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3442 | ASSERT_TRUE(test_server.Start()); |
| 3443 | |
| 3444 | // Set up an expired cookie. |
| 3445 | { |
| 3446 | TestNetworkDelegate network_delegate; |
| 3447 | default_context_.set_network_delegate(&network_delegate); |
| 3448 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3449 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3450 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3451 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3452 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3453 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3454 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3455 | } |
| 3456 | // Verify that the cookie is not set. |
| 3457 | { |
| 3458 | TestNetworkDelegate network_delegate; |
| 3459 | default_context_.set_network_delegate(&network_delegate); |
| 3460 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3461 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3462 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3463 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3464 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3465 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3466 | |
| 3467 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3468 | } |
| 3469 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3470 | { |
| 3471 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3472 | default_context_.set_network_delegate(&network_delegate); |
| 3473 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3474 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3475 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3476 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3477 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3478 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3479 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3480 | } |
| 3481 | // Verify that the cookie is set. |
| 3482 | { |
| 3483 | TestNetworkDelegate network_delegate; |
| 3484 | default_context_.set_network_delegate(&network_delegate); |
| 3485 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3486 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3487 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3488 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3489 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3490 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3491 | |
| 3492 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3497 | // Check that it is impossible to change the referrer in the extra headers of |
| 3498 | // an URLRequest. |
| 3499 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3500 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3501 | ASSERT_TRUE(test_server.Start()); |
| 3502 | |
| 3503 | // If extra headers contain referer and the request contains a referer, |
| 3504 | // only the latter shall be respected. |
| 3505 | { |
| 3506 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3507 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3508 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3509 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3510 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3511 | |
| 3512 | HttpRequestHeaders headers; |
| 3513 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3514 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3515 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3516 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3517 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3518 | |
| 3519 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3520 | } |
| 3521 | |
| 3522 | // If extra headers contain a referer but the request does not, no referer |
| 3523 | // shall be sent in the header. |
| 3524 | { |
| 3525 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3526 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3527 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3528 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3529 | |
| 3530 | HttpRequestHeaders headers; |
| 3531 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3532 | req->SetExtraRequestHeaders(headers); |
| 3533 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3534 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3535 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3536 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3537 | |
| 3538 | EXPECT_EQ("None", d.data_received()); |
| 3539 | } |
| 3540 | } |
| 3541 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3542 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3543 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3544 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3545 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3546 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3547 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3548 | // |request_method| is the method to use for the initial request. |
| 3549 | // |redirect_method| is the method that is expected to be used for the second |
| 3550 | // request, after redirection. |
| 3551 | // If |include_data| is true, data is uploaded with the request. The |
| 3552 | // response body is expected to match it exactly, if and only if |
| 3553 | // |request_method| == |redirect_method|. |
| 3554 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3555 | const std::string& request_method, |
| 3556 | const std::string& redirect_method, |
| 3557 | bool include_data) { |
| 3558 | static const char kData[] = "hello world"; |
| 3559 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3560 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3561 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3562 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3563 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3564 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3565 | HttpRequestHeaders headers; |
| 3566 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3567 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3568 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3569 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3570 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3571 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3572 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3573 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3574 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3575 | if (include_data) { |
| 3576 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3577 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3578 | HttpRequestHeaders::kContentLength)); |
| 3579 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3580 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3581 | EXPECT_EQ(kData, d.data_received()); |
| 3582 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3583 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3584 | HttpRequestHeaders::kContentLength)); |
| 3585 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3586 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3587 | EXPECT_NE(kData, d.data_received()); |
| 3588 | } |
| 3589 | } |
| 3590 | if (HasFailure()) |
| 3591 | LOG(WARNING) << "Request method was: " << request_method; |
| 3592 | } |
| 3593 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3594 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 3595 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3596 | // |request_method| is the method to use for the initial request. |
| 3597 | // |redirect_method| is the method that is expected to be used for the second |
| 3598 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3599 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3600 | // redirection. If empty, expects that there will be no Origin header. |
| 3601 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3602 | const std::string& request_method, |
| 3603 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3604 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3605 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3606 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3607 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3608 | req->set_method(request_method); |
| 3609 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3610 | redirect_url.GetOrigin().spec(), false); |
| 3611 | req->Start(); |
| 3612 | |
| 3613 | base::RunLoop().Run(); |
| 3614 | |
| 3615 | EXPECT_EQ(redirect_method, req->method()); |
| 3616 | // Note that there is no check for request success here because, for |
| 3617 | // purposes of testing, the request very well may fail. For example, if the |
| 3618 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3619 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3620 | // request would fail. However, that's fine, as long as the request headers |
| 3621 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3622 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3623 | EXPECT_FALSE( |
| 3624 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3625 | } else { |
| 3626 | std::string origin_header; |
| 3627 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3628 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3629 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3630 | } |
| 3631 | } |
| 3632 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3633 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3634 | const int kMsgSize = 20000; // multiple of 10 |
| 3635 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3636 | char* uploadBytes = new char[kMsgSize+1]; |
| 3637 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3638 | char marker = 'a'; |
| 3639 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3640 | memcpy(ptr, "----------", 10); |
| 3641 | ptr += 10; |
| 3642 | if (idx % 100 == 0) { |
| 3643 | ptr--; |
| 3644 | *ptr++ = marker; |
| 3645 | if (++marker > 'z') |
| 3646 | marker = 'a'; |
| 3647 | } |
| 3648 | } |
| 3649 | uploadBytes[kMsgSize] = '\0'; |
| 3650 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3651 | for (int i = 0; i < kIterations; ++i) { |
| 3652 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3653 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3654 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 3655 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3656 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3657 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3658 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3659 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3660 | r->Start(); |
| 3661 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3662 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3663 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3664 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3665 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 3666 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3667 | |
| 3668 | EXPECT_FALSE(d.received_data_before_response()); |
| 3669 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3670 | } |
| 3671 | delete[] uploadBytes; |
| 3672 | } |
| 3673 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3674 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3675 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3676 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3677 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3678 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3679 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3680 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3681 | r->Start(); |
| 3682 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3683 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3684 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3685 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3686 | if (d.request_status() != OK) { |
| 3687 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 3688 | return false; |
| 3689 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3690 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3691 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3692 | } |
| 3693 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3694 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3695 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3696 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3697 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3698 | }; |
| 3699 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3700 | namespace { |
| 3701 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3702 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3703 | const test_server::HttpRequest& request) { |
| 3704 | if (request.headers.find("Host") == request.headers.end() || |
| 3705 | request.headers.at("Host") != "www.redirect.com" || |
| 3706 | request.method != test_server::METHOD_CONNECT) { |
| 3707 | return nullptr; |
| 3708 | } |
| 3709 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3710 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3711 | new test_server::BasicHttpResponse); |
| 3712 | http_response->set_code(HTTP_FOUND); |
| 3713 | http_response->AddCustomHeader("Location", |
| 3714 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3715 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | } // namespace |
| 3719 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3720 | class TestSSLConfigService : public SSLConfigService { |
| 3721 | public: |
| 3722 | TestSSLConfigService(bool ev_enabled, |
| 3723 | bool online_rev_checking, |
| 3724 | bool rev_checking_required_local_anchors, |
| 3725 | bool token_binding_enabled) |
| 3726 | : ev_enabled_(ev_enabled), |
| 3727 | online_rev_checking_(online_rev_checking), |
| 3728 | rev_checking_required_local_anchors_( |
| 3729 | rev_checking_required_local_anchors), |
| 3730 | token_binding_enabled_(token_binding_enabled), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3731 | min_version_(kDefaultSSLVersionMin), |
| 3732 | max_version_(kDefaultSSLVersionMax) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3733 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3734 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3735 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3736 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3737 | // SSLConfigService: |
| 3738 | void GetSSLConfig(SSLConfig* config) override { |
| 3739 | *config = SSLConfig(); |
| 3740 | config->rev_checking_enabled = online_rev_checking_; |
| 3741 | config->verify_ev_cert = ev_enabled_; |
| 3742 | config->rev_checking_required_local_anchors = |
| 3743 | rev_checking_required_local_anchors_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3744 | config->version_min = min_version_; |
| 3745 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3746 | if (token_binding_enabled_) { |
| 3747 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | protected: |
| 3752 | ~TestSSLConfigService() override {} |
| 3753 | |
| 3754 | private: |
| 3755 | const bool ev_enabled_; |
| 3756 | const bool online_rev_checking_; |
| 3757 | const bool rev_checking_required_local_anchors_; |
| 3758 | const bool token_binding_enabled_; |
| 3759 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3760 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3761 | }; |
| 3762 | |
| 3763 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3764 | #if !defined(OS_IOS) |
| 3765 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3766 | public: |
fdoray | 9f0b478 | 2017-02-10 12:55:47 | [diff] [blame] | 3767 | TokenBindingURLRequestTest() = default; |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3768 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3769 | void SetUp() override { |
| 3770 | default_context_.set_ssl_config_service( |
| 3771 | new TestSSLConfigService(false, false, false, true)); |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3772 | channel_id_service_.reset( |
| 3773 | new ChannelIDService(new DefaultChannelIDStore(NULL))); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3774 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3775 | URLRequestTestHTTP::SetUp(); |
| 3776 | } |
| 3777 | |
| 3778 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3779 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3780 | }; |
| 3781 | |
| 3782 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3783 | SpawnedTestServer::SSLOptions ssl_options; |
| 3784 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3785 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3786 | ssl_options, |
| 3787 | base::FilePath(kTestFilePath)); |
| 3788 | ASSERT_TRUE(https_test_server.Start()); |
| 3789 | |
| 3790 | TestDelegate d; |
| 3791 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3792 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3793 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d, |
| 3794 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3795 | r->Start(); |
| 3796 | EXPECT_TRUE(r->is_pending()); |
| 3797 | |
| 3798 | base::RunLoop().Run(); |
| 3799 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3800 | EXPECT_EQ(OK, d.request_status()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3801 | |
| 3802 | HttpRequestHeaders headers; |
| 3803 | std::string token_binding_header, token_binding_message; |
| 3804 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3805 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3806 | &token_binding_header)); |
| 3807 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3808 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3809 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3810 | std::vector<TokenBinding> token_bindings; |
| 3811 | ASSERT_TRUE( |
| 3812 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3813 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3814 | |
| 3815 | EXPECT_GT(d.bytes_received(), 0); |
| 3816 | std::string ekm = d.data_received(); |
| 3817 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3818 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3819 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3820 | token_bindings[0].signature, |
| 3821 | TokenBindingType::PROVIDED, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3822 | } |
| 3823 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3824 | |
| 3825 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3826 | SpawnedTestServer::SSLOptions ssl_options; |
| 3827 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3828 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3829 | ssl_options, |
| 3830 | base::FilePath(kTestFilePath)); |
| 3831 | ASSERT_TRUE(https_test_server.Start()); |
| 3832 | |
| 3833 | TestDelegate d; |
| 3834 | { |
| 3835 | GURL redirect_url = |
| 3836 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3837 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3838 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3839 | r->Start(); |
| 3840 | EXPECT_TRUE(r->is_pending()); |
| 3841 | |
| 3842 | base::RunLoop().Run(); |
| 3843 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3844 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3845 | |
| 3846 | HttpRequestHeaders headers; |
| 3847 | std::string token_binding_header, token_binding_message; |
| 3848 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3849 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3850 | &token_binding_header)); |
| 3851 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3852 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3853 | &token_binding_message)); |
| 3854 | std::vector<TokenBinding> token_bindings; |
| 3855 | ASSERT_TRUE( |
| 3856 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3857 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3858 | |
| 3859 | EXPECT_GT(d.bytes_received(), 0); |
| 3860 | std::string ekm = d.data_received(); |
| 3861 | |
| 3862 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3863 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3864 | token_bindings[0].signature, |
| 3865 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3866 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3867 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[1].ec_point, |
| 3868 | token_bindings[1].signature, |
| 3869 | TokenBindingType::REFERRED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3874 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3875 | #if !defined(OS_ANDROID) |
| 3876 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 3877 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, base::FilePath()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3878 | ASSERT_TRUE(http_server.Start()); |
| 3879 | SpawnedTestServer::SSLOptions ssl_options; |
| 3880 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3881 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3882 | ssl_options, |
| 3883 | base::FilePath(kTestFilePath)); |
| 3884 | ASSERT_TRUE(https_test_server.Start()); |
| 3885 | |
| 3886 | TestDelegate d; |
| 3887 | { |
| 3888 | GURL redirect_url = http_server.GetURL( |
| 3889 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3890 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3891 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3892 | r->Start(); |
| 3893 | EXPECT_TRUE(r->is_pending()); |
| 3894 | |
| 3895 | base::RunLoop().Run(); |
| 3896 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3897 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3898 | |
| 3899 | HttpRequestHeaders headers; |
| 3900 | std::string token_binding_header, token_binding_message; |
| 3901 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3902 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3903 | &token_binding_header)); |
| 3904 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3905 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3906 | &token_binding_message)); |
| 3907 | std::vector<TokenBinding> token_bindings; |
| 3908 | ASSERT_TRUE( |
| 3909 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3910 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3911 | |
| 3912 | EXPECT_GT(d.bytes_received(), 0); |
| 3913 | std::string ekm = d.data_received(); |
| 3914 | |
| 3915 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3916 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3917 | token_bindings[0].signature, |
| 3918 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 3923 | // 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] | 3924 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3925 | // the redirect. |
| 3926 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3927 | SpawnedTestServer::SSLOptions ssl_options; |
| 3928 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3929 | ssl_options, |
| 3930 | base::FilePath(kTestFilePath)); |
| 3931 | ASSERT_TRUE(https_test_server.Start()); |
| 3932 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3933 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3934 | ssl_options, |
| 3935 | base::FilePath(kTestFilePath)); |
| 3936 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3937 | |
| 3938 | TestDelegate d; |
| 3939 | { |
| 3940 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3941 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3942 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3943 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3944 | r->Start(); |
| 3945 | EXPECT_TRUE(r->is_pending()); |
| 3946 | |
| 3947 | base::RunLoop().Run(); |
| 3948 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3949 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3950 | |
| 3951 | HttpRequestHeaders headers; |
| 3952 | std::string token_binding_header, token_binding_message; |
| 3953 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3954 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3955 | &token_binding_header)); |
| 3956 | } |
| 3957 | } |
| 3958 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3959 | #endif // !defined(OS_IOS) |
| 3960 | |
| 3961 | // 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] | 3962 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3963 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3964 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3965 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3966 | http_test_server()->RegisterRequestHandler( |
| 3967 | base::Bind(&HandleRedirectConnect)); |
| 3968 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3969 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3970 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3971 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3972 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3973 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3974 | TestDelegate d; |
| 3975 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3976 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3977 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3978 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3979 | r->Start(); |
| 3980 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3981 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3982 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3983 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3984 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 3985 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3986 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3987 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3988 | // We should not have followed the redirect. |
| 3989 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3990 | } |
| 3991 | } |
| 3992 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3993 | // This is the same as the previous test, but checks that the network delegate |
| 3994 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3995 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3996 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3997 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3998 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3999 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4000 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4001 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4002 | TestDelegate d; |
| 4003 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4004 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4005 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4006 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4007 | r->Start(); |
| 4008 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4009 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4010 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4011 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4012 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4013 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4014 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4015 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4016 | // We should not have followed the redirect. |
| 4017 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4018 | |
| 4019 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4020 | EXPECT_THAT(network_delegate.last_error(), |
| 4021 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4022 | } |
| 4023 | } |
| 4024 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4025 | // Tests that we can block and asynchronously return OK in various stages. |
| 4026 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4027 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4028 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4029 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4030 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4031 | }; |
| 4032 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 4033 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4034 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4035 | |
| 4036 | TestDelegate d; |
| 4037 | BlockingNetworkDelegate network_delegate( |
| 4038 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4039 | network_delegate.set_block_on( |
| 4040 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4041 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4042 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4043 | |
| 4044 | TestURLRequestContext context(true); |
| 4045 | context.set_network_delegate(&network_delegate); |
| 4046 | context.Init(); |
| 4047 | |
| 4048 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4049 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4050 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4051 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4052 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4053 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4054 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4055 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4056 | EXPECT_EQ(blocking_stages[i], |
| 4057 | network_delegate.stage_blocked_for_callback()); |
| 4058 | network_delegate.DoCallback(OK); |
| 4059 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4060 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4061 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4062 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4063 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4064 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4065 | } |
| 4066 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4067 | } |
| 4068 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4069 | // Tests that the network delegate can block and cancel a request. |
| 4070 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4071 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4072 | |
| 4073 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4074 | BlockingNetworkDelegate network_delegate( |
| 4075 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4076 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4077 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4078 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4079 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4080 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4081 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4082 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4083 | std::unique_ptr<URLRequest> r( |
| 4084 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4085 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4086 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4087 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4088 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4089 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4090 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4091 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4092 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4093 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4094 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4095 | } |
| 4096 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4097 | } |
| 4098 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4099 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4100 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4101 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4102 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4103 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4104 | BlockingNetworkDelegate::Stage stage, |
| 4105 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4106 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4107 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4108 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4109 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4110 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4111 | TestURLRequestContext context(true); |
| 4112 | context.set_network_delegate(&network_delegate); |
| 4113 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4114 | |
| 4115 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4116 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4117 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4118 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4119 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4120 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4121 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4122 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4123 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4124 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4125 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4126 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4127 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4128 | } else { |
| 4129 | NOTREACHED(); |
| 4130 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4131 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4132 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4133 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4134 | } |
| 4135 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4136 | } |
| 4137 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4138 | // The following 3 tests check that the network delegate can cancel a request |
| 4139 | // synchronously in various stages of the request. |
| 4140 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4141 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4142 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4143 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4144 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4148 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4149 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4150 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4151 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4155 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4156 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4157 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4158 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4159 | } |
| 4160 | |
| 4161 | // The following 3 tests check that the network delegate can cancel a request |
| 4162 | // asynchronously in various stages of the request. |
| 4163 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4164 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4165 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4166 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4167 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4171 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4172 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4173 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4174 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4178 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4179 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4180 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4181 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4182 | } |
| 4183 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4184 | // Tests that the network delegate can block and redirect a request to a new |
| 4185 | // URL. |
| 4186 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4187 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4188 | |
| 4189 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4190 | BlockingNetworkDelegate network_delegate( |
| 4191 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4192 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4193 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4194 | network_delegate.set_redirect_url(redirect_url); |
| 4195 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4196 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4197 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4198 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4199 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4200 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4201 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4202 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4203 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4204 | // Quit after hitting the redirect, so can check the headers. |
| 4205 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4206 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4207 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4208 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4209 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4210 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4211 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4212 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4213 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4214 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4215 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4216 | |
| 4217 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4218 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4219 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4220 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4221 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4222 | http_test_server()->host_port_pair()), |
| 4223 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4224 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4225 | // through an untunneled proxy. |
| 4226 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4227 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4228 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4229 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4230 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4231 | EXPECT_EQ(redirect_url, r->url()); |
| 4232 | EXPECT_EQ(original_url, r->original_url()); |
| 4233 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4234 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4235 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4236 | } |
| 4237 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4238 | } |
| 4239 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4240 | // Tests that the network delegate can block and redirect a request to a new |
| 4241 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4242 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4243 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4244 | |
| 4245 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4246 | BlockingNetworkDelegate network_delegate( |
| 4247 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4248 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4249 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4250 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4251 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4252 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4253 | |
| 4254 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4255 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4256 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4257 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4258 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4259 | // Quit after hitting the redirect, so can check the headers. |
| 4260 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4261 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4262 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4263 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4264 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4265 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4266 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4267 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4268 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4269 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4270 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4271 | |
| 4272 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4273 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4274 | base::RunLoop().Run(); |
| 4275 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4276 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4277 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4278 | http_test_server()->host_port_pair()), |
| 4279 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4280 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4281 | // through an untunneled proxy. |
| 4282 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4283 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4284 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4285 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4286 | EXPECT_EQ(redirect_url, r->url()); |
| 4287 | EXPECT_EQ(original_url, r->original_url()); |
| 4288 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4289 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4290 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4291 | } |
| 4292 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4293 | } |
| 4294 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4295 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4296 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4297 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4298 | |
| 4299 | const char kData[] = "hello world"; |
| 4300 | |
| 4301 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4302 | BlockingNetworkDelegate network_delegate( |
| 4303 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4304 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4305 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4306 | network_delegate.set_redirect_url(redirect_url); |
| 4307 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4308 | TestURLRequestContext context(true); |
| 4309 | context.set_network_delegate(&network_delegate); |
| 4310 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4311 | |
| 4312 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4313 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4314 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4315 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4316 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4317 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4318 | HttpRequestHeaders headers; |
| 4319 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 4320 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4321 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4322 | |
| 4323 | // Quit after hitting the redirect, so can check the headers. |
| 4324 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4325 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4326 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4327 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4328 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4329 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4330 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4331 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4332 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4333 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4334 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4335 | |
| 4336 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4337 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4338 | base::RunLoop().Run(); |
| 4339 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4340 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4341 | EXPECT_EQ(redirect_url, r->url()); |
| 4342 | EXPECT_EQ(original_url, r->original_url()); |
| 4343 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4344 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4345 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4346 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4347 | EXPECT_EQ(kData, d.data_received()); |
| 4348 | } |
| 4349 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4350 | } |
| 4351 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4352 | // Tests that the network delegate can block and redirect a request to a new |
| 4353 | // URL during OnHeadersReceived. |
| 4354 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4355 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4356 | |
| 4357 | TestDelegate d; |
| 4358 | BlockingNetworkDelegate network_delegate( |
| 4359 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4360 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4361 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4362 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4363 | |
| 4364 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4365 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4366 | |
| 4367 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4368 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4369 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4370 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4371 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4372 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4373 | base::RunLoop().Run(); |
| 4374 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4375 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4376 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4377 | http_test_server()->host_port_pair()), |
| 4378 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4379 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4380 | // through an untunneled proxy. |
| 4381 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4382 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4383 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4384 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4385 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4386 | EXPECT_EQ(redirect_url, r->url()); |
| 4387 | EXPECT_EQ(original_url, r->original_url()); |
| 4388 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4389 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4390 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4391 | } |
| 4392 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4393 | } |
| 4394 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4395 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4396 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4397 | // handle the challenge, and is passing the buck along to the |
| 4398 | // URLRequest::Delegate. |
| 4399 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4400 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4401 | |
| 4402 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4403 | BlockingNetworkDelegate network_delegate( |
| 4404 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4405 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4406 | TestURLRequestContext context(true); |
| 4407 | context.set_network_delegate(&network_delegate); |
| 4408 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4409 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4410 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4411 | |
| 4412 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4413 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4414 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4415 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4416 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4417 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4418 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4419 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4420 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4421 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4422 | EXPECT_TRUE(d.auth_required_called()); |
| 4423 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4424 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4425 | } |
| 4426 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4427 | } |
| 4428 | |
| 4429 | TEST_F(URLRequestTestHTTP, |
| 4430 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4431 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4432 | |
| 4433 | TestDelegate d; |
| 4434 | BlockingNetworkDelegate network_delegate( |
| 4435 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4436 | |
| 4437 | TestURLRequestContext context(true); |
| 4438 | context.set_network_delegate(&network_delegate); |
| 4439 | context.Init(); |
| 4440 | |
| 4441 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4442 | |
| 4443 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4444 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4445 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4446 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4447 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4448 | |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4449 | base::RunLoop().Run(); |
| 4450 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4451 | { |
| 4452 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4453 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4454 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4455 | } |
| 4456 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4457 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4458 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4459 | EXPECT_TRUE(d.auth_required_called()); |
| 4460 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4461 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4462 | } |
| 4463 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4464 | } |
| 4465 | |
| 4466 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4467 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4468 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4469 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4470 | |
| 4471 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4472 | BlockingNetworkDelegate network_delegate( |
| 4473 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4474 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4475 | network_delegate.set_auth_retval( |
| 4476 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4477 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4478 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4479 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4480 | TestURLRequestContext context(true); |
| 4481 | context.set_network_delegate(&network_delegate); |
| 4482 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4483 | |
| 4484 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4485 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4486 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4487 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4488 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4489 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4490 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4491 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4492 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4493 | EXPECT_FALSE(d.auth_required_called()); |
| 4494 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4495 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4496 | } |
| 4497 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4498 | } |
| 4499 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4500 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4501 | // headers (for the first or second request) when called at the proper times. |
| 4502 | TEST_F(URLRequestTestHTTP, |
| 4503 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4504 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4505 | |
| 4506 | TestDelegate d; |
| 4507 | BlockingNetworkDelegate network_delegate( |
| 4508 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4509 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4510 | network_delegate.set_auth_retval( |
| 4511 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4512 | |
| 4513 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4514 | |
| 4515 | TestURLRequestContext context(true); |
| 4516 | context.set_network_delegate(&network_delegate); |
| 4517 | context.Init(); |
| 4518 | |
| 4519 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4520 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4521 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4522 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4523 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4524 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4525 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4526 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4527 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4528 | EXPECT_FALSE(d.auth_required_called()); |
| 4529 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4530 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4531 | |
| 4532 | { |
| 4533 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4534 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4535 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4536 | } |
| 4537 | } |
| 4538 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4539 | } |
| 4540 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4541 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4542 | // by cancelling authentication. |
| 4543 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4544 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4545 | |
| 4546 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4547 | BlockingNetworkDelegate network_delegate( |
| 4548 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4549 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4550 | network_delegate.set_auth_retval( |
| 4551 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4552 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4553 | TestURLRequestContext context(true); |
| 4554 | context.set_network_delegate(&network_delegate); |
| 4555 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4556 | |
| 4557 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4558 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4559 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4560 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4561 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4562 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4563 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4564 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4565 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4566 | EXPECT_FALSE(d.auth_required_called()); |
| 4567 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4568 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4569 | } |
| 4570 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4571 | } |
| 4572 | |
| 4573 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4574 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4575 | // to handle the challenge, and is passing the buck along to the |
| 4576 | // URLRequest::Delegate. |
| 4577 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4578 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4579 | |
| 4580 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4581 | BlockingNetworkDelegate network_delegate( |
| 4582 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4583 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4584 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4585 | TestURLRequestContext context(true); |
| 4586 | context.set_network_delegate(&network_delegate); |
| 4587 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4588 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4589 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4590 | |
| 4591 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4592 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4593 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4594 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4595 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4596 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4597 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4598 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4599 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4600 | EXPECT_TRUE(d.auth_required_called()); |
| 4601 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4602 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4603 | } |
| 4604 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4605 | } |
| 4606 | |
| 4607 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4608 | // by setting credentials. |
| 4609 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4610 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4611 | |
| 4612 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4613 | BlockingNetworkDelegate network_delegate( |
| 4614 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4615 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4616 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4617 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4618 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4619 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4620 | network_delegate.set_auth_credentials(auth_credentials); |
| 4621 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4622 | TestURLRequestContext context(true); |
| 4623 | context.set_network_delegate(&network_delegate); |
| 4624 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4625 | |
| 4626 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4627 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4628 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4629 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4630 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4631 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4632 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4633 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4634 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4635 | EXPECT_FALSE(d.auth_required_called()); |
| 4636 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4637 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4638 | } |
| 4639 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4640 | } |
| 4641 | |
| 4642 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4643 | // by cancelling authentication. |
| 4644 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4645 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4646 | |
| 4647 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4648 | BlockingNetworkDelegate network_delegate( |
| 4649 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4650 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4651 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4652 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4653 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4654 | TestURLRequestContext context(true); |
| 4655 | context.set_network_delegate(&network_delegate); |
| 4656 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4657 | |
| 4658 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4659 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4660 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4661 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4662 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4663 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4664 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4665 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4666 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4667 | EXPECT_FALSE(d.auth_required_called()); |
| 4668 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4669 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4670 | } |
| 4671 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4672 | } |
| 4673 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4674 | // Tests that we can handle when a network request was canceled while we were |
| 4675 | // waiting for the network delegate. |
| 4676 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4677 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4678 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4679 | |
| 4680 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4681 | BlockingNetworkDelegate network_delegate( |
| 4682 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4683 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4684 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4685 | TestURLRequestContext context(true); |
| 4686 | context.set_network_delegate(&network_delegate); |
| 4687 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4688 | |
| 4689 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4690 | std::unique_ptr<URLRequest> r( |
| 4691 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4692 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4693 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4694 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4695 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4696 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4697 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4698 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4699 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4700 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4701 | // Ensure that network delegate is notified. |
| 4702 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4703 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4704 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4705 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4706 | } |
| 4707 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4708 | } |
| 4709 | |
| 4710 | // Tests that we can handle when a network request was canceled while we were |
| 4711 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4712 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4713 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4714 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4715 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4716 | |
| 4717 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4718 | BlockingNetworkDelegate network_delegate( |
| 4719 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4720 | network_delegate.set_block_on( |
| 4721 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4722 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4723 | TestURLRequestContext context(true); |
| 4724 | context.set_network_delegate(&network_delegate); |
| 4725 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4726 | |
| 4727 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4728 | std::unique_ptr<URLRequest> r( |
| 4729 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4730 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4731 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4732 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4733 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4734 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4735 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4736 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4737 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4738 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4739 | // Ensure that network delegate is notified. |
| 4740 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4741 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4742 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4743 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4744 | } |
| 4745 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4746 | } |
| 4747 | |
| 4748 | // Tests that we can handle when a network request was canceled while we were |
| 4749 | // waiting for the network delegate. |
| 4750 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4751 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4752 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4753 | |
| 4754 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4755 | BlockingNetworkDelegate network_delegate( |
| 4756 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4757 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4758 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4759 | TestURLRequestContext context(true); |
| 4760 | context.set_network_delegate(&network_delegate); |
| 4761 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4762 | |
| 4763 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4764 | std::unique_ptr<URLRequest> r( |
| 4765 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4766 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4767 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4768 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4769 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4770 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4771 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4772 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4773 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4774 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4775 | // Ensure that network delegate is notified. |
| 4776 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4777 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4778 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4779 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4780 | } |
| 4781 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4782 | } |
| 4783 | |
| 4784 | // Tests that we can handle when a network request was canceled while we were |
| 4785 | // waiting for the network delegate. |
| 4786 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4787 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4788 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4789 | |
| 4790 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4791 | BlockingNetworkDelegate network_delegate( |
| 4792 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4793 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4794 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4795 | TestURLRequestContext context(true); |
| 4796 | context.set_network_delegate(&network_delegate); |
| 4797 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4798 | |
| 4799 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4800 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4801 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 4802 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4803 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4804 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4805 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4806 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4807 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4808 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4809 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4810 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4811 | // Ensure that network delegate is notified. |
| 4812 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4813 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4814 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4815 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4816 | } |
| 4817 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4818 | } |
| 4819 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4820 | namespace { |
| 4821 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4822 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4823 | const test_server::HttpRequest& request) { |
| 4824 | if (request.headers.find("Host") == request.headers.end() || |
| 4825 | request.headers.at("Host") != "www.server-auth.com" || |
| 4826 | request.method != test_server::METHOD_CONNECT) { |
| 4827 | return nullptr; |
| 4828 | } |
| 4829 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4830 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4831 | new test_server::BasicHttpResponse); |
| 4832 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4833 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4834 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4835 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | } // namespace |
| 4839 | |
| 4840 | // 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] | 4841 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4842 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4843 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4844 | http_test_server()->RegisterRequestHandler( |
| 4845 | base::Bind(&HandleServerAuthConnect)); |
| 4846 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4847 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4848 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4849 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4850 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4851 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4852 | TestDelegate d; |
| 4853 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4854 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4855 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 4856 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4857 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4858 | r->Start(); |
| 4859 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4860 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4861 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4862 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4863 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4864 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4865 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4866 | } |
| 4867 | } |
| 4868 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4869 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4870 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4871 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4872 | TestDelegate d; |
| 4873 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4874 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4875 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4876 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4877 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4878 | r->Start(); |
| 4879 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4880 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4881 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4882 | |
| 4883 | EXPECT_EQ(1, d.response_started_count()); |
| 4884 | EXPECT_FALSE(d.received_data_before_response()); |
| 4885 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4886 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4887 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4888 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4889 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4890 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4891 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4892 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4893 | } |
| 4894 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4895 | // This test has the server send a large number of cookies to the client. |
| 4896 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4897 | // search is used to estimate that maximum number of cookies that are accepted |
| 4898 | // by the browser. Beyond the maximum number, the request will fail with |
| 4899 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 4900 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4901 | // http://crbug.com/177916 |
| 4902 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4903 | #else |
| 4904 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4905 | #endif // defined(OS_WIN) |
| 4906 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4907 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4908 | |
| 4909 | int lower_bound = 0; |
| 4910 | int upper_bound = 1; |
| 4911 | |
| 4912 | // Double the number of cookies until the response header limits are |
| 4913 | // exceeded. |
| 4914 | while (DoManyCookiesRequest(upper_bound)) { |
| 4915 | lower_bound = upper_bound; |
| 4916 | upper_bound *= 2; |
| 4917 | ASSERT_LT(upper_bound, 1000000); |
| 4918 | } |
| 4919 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4920 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4921 | if (tolerance < 2) |
| 4922 | tolerance = 2; |
| 4923 | |
| 4924 | // Perform a binary search to find the highest possible number of cookies, |
| 4925 | // within the desired tolerance. |
| 4926 | while (upper_bound - lower_bound >= tolerance) { |
| 4927 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4928 | |
| 4929 | if (DoManyCookiesRequest(num_cookies)) |
| 4930 | lower_bound = num_cookies; |
| 4931 | else |
| 4932 | upper_bound = num_cookies; |
| 4933 | } |
| 4934 | // Success: the test did not crash. |
| 4935 | } |
| 4936 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4937 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4938 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4939 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4940 | TestDelegate d; |
| 4941 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4942 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4943 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4944 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4945 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4946 | r->Start(); |
| 4947 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4948 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4949 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4950 | |
| 4951 | EXPECT_EQ(1, d.response_started_count()); |
| 4952 | EXPECT_FALSE(d.received_data_before_response()); |
| 4953 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4954 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4955 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4956 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4957 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4958 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4959 | } |
| 4960 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4961 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4962 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4963 | |
| 4964 | TestDelegate d; |
| 4965 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4966 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4967 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4968 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4969 | |
| 4970 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4971 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4972 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4973 | r->Start(); |
| 4974 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4975 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4976 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4977 | |
| 4978 | EXPECT_EQ(1, d.response_started_count()); |
| 4979 | EXPECT_FALSE(d.received_data_before_response()); |
| 4980 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4981 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4982 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4983 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4984 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4985 | |
| 4986 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4987 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4988 | } |
| 4989 | } |
| 4990 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4991 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4992 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4993 | |
| 4994 | TestDelegate d; |
| 4995 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4996 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4997 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4998 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4999 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5000 | r->Start(); |
| 5001 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5002 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5003 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5004 | |
| 5005 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5006 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5007 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5008 | |
| 5009 | EXPECT_EQ(1, d.response_started_count()); |
| 5010 | EXPECT_FALSE(d.received_data_before_response()); |
| 5011 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5012 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5013 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5014 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5015 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5019 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5020 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5021 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5022 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5023 | base::FilePath(kTestFilePath)); |
| 5024 | |
| 5025 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5026 | |
| 5027 | // Parameter that specifies the Content-Length field in the response: |
| 5028 | // C - Compressed length. |
| 5029 | // U - Uncompressed length. |
| 5030 | // L - Large length (larger than both C & U). |
| 5031 | // M - Medium length (between C & U). |
| 5032 | // S - Small length (smaller than both C & U). |
| 5033 | const char test_parameters[] = "CULMS"; |
| 5034 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 5035 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5036 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5037 | // S has too little data, but we seem to accept it. |
| 5038 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5039 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5040 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5041 | base::FilePath file_path; |
| 5042 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 5043 | file_path = file_path.Append(kTestFilePath); |
| 5044 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5045 | std::string expected_content; |
| 5046 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5047 | |
| 5048 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5049 | TestDelegate d; |
| 5050 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5051 | std::string test_file = base::StringPrintf( |
| 5052 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5053 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5054 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5055 | TestURLRequestContext context(true); |
| 5056 | context.set_network_delegate(&network_delegate); |
| 5057 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5058 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5059 | std::unique_ptr<URLRequest> r( |
| 5060 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5061 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5062 | r->Start(); |
| 5063 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5064 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5065 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5066 | |
| 5067 | EXPECT_EQ(1, d.response_started_count()); |
| 5068 | EXPECT_FALSE(d.received_data_before_response()); |
| 5069 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5070 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5071 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5072 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5073 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5074 | if (test_parameters[i] == 'S') { |
| 5075 | // When content length is smaller than both compressed length and |
| 5076 | // uncompressed length, HttpStreamParser might not read the full |
| 5077 | // response body. |
| 5078 | continue; |
| 5079 | } |
| 5080 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5081 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5082 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5083 | << " Parameter = \"" << test_file << "\""; |
| 5084 | } |
| 5085 | } |
| 5086 | } |
| 5087 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5088 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5089 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5090 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5091 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5092 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5093 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5094 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5095 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5096 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5097 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5098 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5099 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5100 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5101 | |
| 5102 | EXPECT_EQ(1, d.response_started_count()); |
| 5103 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5104 | EXPECT_EQ(destination_url, req->url()); |
| 5105 | EXPECT_EQ(original_url, req->original_url()); |
| 5106 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5107 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5108 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5109 | |
| 5110 | LoadTimingInfo load_timing_info_before_redirect; |
| 5111 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5112 | &load_timing_info_before_redirect)); |
| 5113 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5114 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5115 | |
| 5116 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5117 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5118 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5119 | |
| 5120 | // Check that a new socket was used on redirect, since the server does not |
| 5121 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5122 | // before the ones recorded for the second request. |
| 5123 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5124 | load_timing_info.socket_log_id); |
| 5125 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5126 | load_timing_info.connect_timing.connect_start); |
| 5127 | } |
| 5128 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5129 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5130 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5131 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5132 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5133 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5134 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5135 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5136 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5137 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5138 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5139 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5140 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5141 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5142 | |
| 5143 | EXPECT_EQ(1, d.response_started_count()); |
| 5144 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5145 | EXPECT_EQ(destination_url, req->url()); |
| 5146 | EXPECT_EQ(original_url, req->original_url()); |
| 5147 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5148 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5149 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5150 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5151 | } |
| 5152 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5153 | // First and second pieces of information logged by delegates to URLRequests. |
| 5154 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5155 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5156 | |
| 5157 | // Logs delegate information to a URLRequest. The first string is logged |
| 5158 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5159 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5160 | // another asynchronous call is used to clear the delegate information |
| 5161 | // before calling a callback. The object then deletes itself. |
| 5162 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5163 | public: |
| 5164 | typedef base::Callback<void()> Callback; |
| 5165 | |
| 5166 | // Each time delegate information is added to the URLRequest, the resulting |
| 5167 | // load state is checked. The expected load state after each request is |
| 5168 | // passed in as an argument. |
| 5169 | static void Run(URLRequest* url_request, |
| 5170 | LoadState expected_first_load_state, |
| 5171 | LoadState expected_second_load_state, |
| 5172 | LoadState expected_third_load_state, |
| 5173 | const Callback& callback) { |
| 5174 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 5175 | url_request, |
| 5176 | expected_first_load_state, |
| 5177 | expected_second_load_state, |
| 5178 | expected_third_load_state, |
| 5179 | callback); |
| 5180 | logger->Start(); |
| 5181 | } |
| 5182 | |
| 5183 | // Checks that the log entries, starting with log_position, contain the |
| 5184 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5185 | // recorded. Returns the index of entry after the expected number of |
| 5186 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5187 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5188 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5189 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5190 | if (log_position + 3 >= entries.size()) { |
| 5191 | ADD_FAILURE() << "Not enough log entries"; |
| 5192 | return entries.size(); |
| 5193 | } |
| 5194 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5195 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5196 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5197 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5198 | &delegate_info)); |
| 5199 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5200 | |
| 5201 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5202 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5203 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5204 | |
| 5205 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5206 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5207 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5208 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5209 | &delegate_info)); |
| 5210 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5211 | |
| 5212 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5213 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5214 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5215 | |
| 5216 | return log_position + 1; |
| 5217 | } |
| 5218 | |
| 5219 | private: |
| 5220 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5221 | |
| 5222 | AsyncDelegateLogger(URLRequest* url_request, |
| 5223 | LoadState expected_first_load_state, |
| 5224 | LoadState expected_second_load_state, |
| 5225 | LoadState expected_third_load_state, |
| 5226 | const Callback& callback) |
| 5227 | : url_request_(url_request), |
| 5228 | expected_first_load_state_(expected_first_load_state), |
| 5229 | expected_second_load_state_(expected_second_load_state), |
| 5230 | expected_third_load_state_(expected_third_load_state), |
| 5231 | callback_(callback) { |
| 5232 | } |
| 5233 | |
| 5234 | ~AsyncDelegateLogger() {} |
| 5235 | |
| 5236 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5237 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5238 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5239 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5240 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5241 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5242 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5246 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5247 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5248 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5249 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5250 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5251 | } else { |
| 5252 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5253 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5254 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5255 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5259 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5260 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5261 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5262 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5263 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5264 | callback_.Run(); |
| 5265 | } |
| 5266 | |
| 5267 | URLRequest* url_request_; |
| 5268 | const int expected_first_load_state_; |
| 5269 | const int expected_second_load_state_; |
| 5270 | const int expected_third_load_state_; |
| 5271 | const Callback callback_; |
| 5272 | |
| 5273 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5274 | }; |
| 5275 | |
| 5276 | // NetworkDelegate that logs delegate information before a request is started, |
| 5277 | // before headers are sent, when headers are read, and when auth information |
| 5278 | // is requested. Uses AsyncDelegateLogger. |
| 5279 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5280 | public: |
| 5281 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5282 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5283 | |
| 5284 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5285 | int OnBeforeURLRequest(URLRequest* request, |
| 5286 | const CompletionCallback& callback, |
| 5287 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5288 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 5289 | return RunCallbackAsynchronously(request, callback); |
| 5290 | } |
| 5291 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5292 | int OnBeforeStartTransaction(URLRequest* request, |
| 5293 | const CompletionCallback& callback, |
| 5294 | HttpRequestHeaders* headers) override { |
| 5295 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5296 | return RunCallbackAsynchronously(request, callback); |
| 5297 | } |
| 5298 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5299 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5300 | URLRequest* request, |
| 5301 | const CompletionCallback& callback, |
| 5302 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5303 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5304 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5305 | TestNetworkDelegate::OnHeadersReceived(request, |
| 5306 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5307 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5308 | override_response_headers, |
| 5309 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5310 | return RunCallbackAsynchronously(request, callback); |
| 5311 | } |
| 5312 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5313 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5314 | URLRequest* request, |
| 5315 | const AuthChallengeInfo& auth_info, |
| 5316 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5317 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5318 | AsyncDelegateLogger::Run( |
| 5319 | request, |
| 5320 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5321 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5322 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5323 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5324 | callback, credentials)); |
| 5325 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5326 | } |
| 5327 | |
| 5328 | private: |
| 5329 | static int RunCallbackAsynchronously( |
| 5330 | URLRequest* request, |
| 5331 | const CompletionCallback& callback) { |
| 5332 | AsyncDelegateLogger::Run( |
| 5333 | request, |
| 5334 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5335 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5336 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5337 | base::Bind(callback, OK)); |
| 5338 | return ERR_IO_PENDING; |
| 5339 | } |
| 5340 | |
| 5341 | static void SetAuthAndResume(const AuthCallback& callback, |
| 5342 | AuthCredentials* credentials) { |
| 5343 | *credentials = AuthCredentials(kUser, kSecret); |
| 5344 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5345 | } |
| 5346 | |
| 5347 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5348 | }; |
| 5349 | |
| 5350 | // URLRequest::Delegate that logs delegate information when the headers |
| 5351 | // are received, when each read completes, and during redirects. Uses |
| 5352 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5353 | // |
| 5354 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5355 | // advancing to the next step in most cases, as well as cancellation. |
| 5356 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5357 | public: |
| 5358 | enum CancelStage { |
| 5359 | NO_CANCEL = 0, |
| 5360 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5361 | CANCEL_ON_RESPONSE_STARTED, |
| 5362 | CANCEL_ON_READ_COMPLETED |
| 5363 | }; |
| 5364 | |
| 5365 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5366 | : cancel_stage_(cancel_stage) { |
| 5367 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5368 | set_cancel_in_received_redirect(true); |
| 5369 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5370 | set_cancel_in_response_started(true); |
| 5371 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5372 | set_cancel_in_received_data(true); |
| 5373 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5374 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5375 | |
| 5376 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5377 | void OnReceivedRedirect(URLRequest* request, |
| 5378 | const RedirectInfo& redirect_info, |
| 5379 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5380 | *defer_redirect = true; |
| 5381 | AsyncDelegateLogger::Run( |
| 5382 | request, |
| 5383 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5384 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5385 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5386 | base::Bind( |
| 5387 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5388 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5389 | } |
| 5390 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5391 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5392 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5393 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5394 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5395 | base::Bind( |
| 5396 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5397 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5398 | } |
| 5399 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5400 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5401 | AsyncDelegateLogger::Run( |
| 5402 | request, |
| 5403 | LOAD_STATE_IDLE, |
| 5404 | LOAD_STATE_IDLE, |
| 5405 | LOAD_STATE_IDLE, |
| 5406 | base::Bind( |
| 5407 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5408 | base::Unretained(this), request, bytes_read)); |
| 5409 | } |
| 5410 | |
| 5411 | private: |
| 5412 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5413 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5414 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5415 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5416 | // FollowDeferredRedirect should not be called after cancellation. |
| 5417 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5418 | return; |
| 5419 | if (!defer_redirect) |
| 5420 | request->FollowDeferredRedirect(); |
| 5421 | } |
| 5422 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5423 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5424 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5425 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5429 | // The parent class continues the request. |
| 5430 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5431 | } |
| 5432 | |
| 5433 | const CancelStage cancel_stage_; |
| 5434 | |
| 5435 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5436 | }; |
| 5437 | |
| 5438 | // Tests handling of delegate info before a request starts. |
| 5439 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5440 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5441 | |
| 5442 | TestDelegate request_delegate; |
| 5443 | TestURLRequestContext context(true); |
| 5444 | context.set_network_delegate(NULL); |
| 5445 | context.set_net_log(&net_log_); |
| 5446 | context.Init(); |
| 5447 | |
| 5448 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5449 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5450 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5451 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5452 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5453 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5454 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5455 | |
| 5456 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5457 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5458 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5459 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5460 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5461 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5462 | |
| 5463 | base::RunLoop().Run(); |
| 5464 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5465 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5466 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5467 | } |
| 5468 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5469 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5470 | net_log_.GetEntries(&entries); |
| 5471 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5472 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5473 | |
| 5474 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5475 | |
| 5476 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5477 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5478 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | // Tests handling of delegate info from a network delegate. |
| 5482 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5483 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5484 | |
| 5485 | TestDelegate request_delegate; |
| 5486 | AsyncLoggingNetworkDelegate network_delegate; |
| 5487 | TestURLRequestContext context(true); |
| 5488 | context.set_network_delegate(&network_delegate); |
| 5489 | context.set_net_log(&net_log_); |
| 5490 | context.Init(); |
| 5491 | |
| 5492 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5493 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5494 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5495 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5496 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5497 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5498 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5499 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5500 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5501 | base::RunLoop().Run(); |
| 5502 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5503 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5504 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5505 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5506 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5507 | } |
| 5508 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5509 | |
| 5510 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5511 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5512 | net_log_.GetEntries(&entries); |
| 5513 | for (size_t i = 0; i < 3; ++i) { |
| 5514 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5515 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5516 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5517 | |
| 5518 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5519 | log_position + 1); |
| 5520 | |
| 5521 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5522 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5523 | entries[log_position].type); |
| 5524 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5525 | } |
| 5526 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5527 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5528 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | // Tests handling of delegate info from a network delegate in the case of an |
| 5532 | // HTTP redirect. |
| 5533 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5534 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5535 | |
| 5536 | TestDelegate request_delegate; |
| 5537 | AsyncLoggingNetworkDelegate network_delegate; |
| 5538 | TestURLRequestContext context(true); |
| 5539 | context.set_network_delegate(&network_delegate); |
| 5540 | context.set_net_log(&net_log_); |
| 5541 | context.Init(); |
| 5542 | |
| 5543 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5544 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5545 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5546 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5547 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5548 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5549 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5550 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5551 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5552 | base::RunLoop().Run(); |
| 5553 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5554 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5555 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5556 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5557 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5558 | } |
| 5559 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5560 | |
| 5561 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5562 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5563 | net_log_.GetEntries(&entries); |
| 5564 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5565 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5566 | for (size_t i = 0; i < 3; ++i) { |
| 5567 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5568 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5569 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5570 | |
| 5571 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5572 | log_position + 1); |
| 5573 | |
| 5574 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5575 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5576 | entries[log_position].type); |
| 5577 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5581 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5582 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5583 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5584 | |
| 5585 | // The NetworkDelegate logged information in the same three events as before. |
| 5586 | for (size_t i = 0; i < 3; ++i) { |
| 5587 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5588 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5589 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5590 | |
| 5591 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5592 | log_position + 1); |
| 5593 | |
| 5594 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5595 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5596 | entries[log_position].type); |
| 5597 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5598 | } |
| 5599 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5600 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5601 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5605 | // AUTH. |
| 5606 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5607 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5608 | |
| 5609 | TestDelegate request_delegate; |
| 5610 | AsyncLoggingNetworkDelegate network_delegate; |
| 5611 | TestURLRequestContext context(true); |
| 5612 | context.set_network_delegate(&network_delegate); |
| 5613 | context.set_net_log(&net_log_); |
| 5614 | context.Init(); |
| 5615 | |
| 5616 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5617 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5618 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5619 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5620 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5621 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5622 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5623 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5624 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5625 | base::RunLoop().Run(); |
| 5626 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5627 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5628 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5629 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5630 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5631 | } |
| 5632 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5633 | |
| 5634 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5635 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5636 | net_log_.GetEntries(&entries); |
| 5637 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5638 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5639 | // in |
| 5640 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5641 | for (size_t i = 0; i < 6; ++i) { |
| 5642 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5643 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5644 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5645 | |
| 5646 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5647 | log_position + 1); |
| 5648 | |
| 5649 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5650 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5651 | entries[log_position].type); |
| 5652 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5653 | } |
| 5654 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5655 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5656 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5657 | } |
| 5658 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5659 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5660 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5661 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5662 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5663 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5664 | base::FilePath(kTestFilePath)); |
| 5665 | |
| 5666 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5667 | |
| 5668 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5669 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5670 | TestURLRequestContext context(true); |
| 5671 | context.set_network_delegate(NULL); |
| 5672 | context.set_net_log(&net_log_); |
| 5673 | context.Init(); |
| 5674 | |
| 5675 | { |
| 5676 | // A chunked response with delays between chunks is used to make sure that |
| 5677 | // attempts by the URLRequest delegate to log information while reading the |
| 5678 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5679 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5680 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5681 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5682 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5683 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5684 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5685 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5686 | base::RunLoop().Run(); |
| 5687 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5688 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5689 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5690 | } |
| 5691 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5692 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5693 | net_log_.GetEntries(&entries); |
| 5694 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5695 | size_t log_position = 0; |
| 5696 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5697 | // The delegate info should only have been logged on header complete. Other |
| 5698 | // times it should silently be ignored. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5699 | log_position = ExpectLogContainsSomewhereAfter( |
| 5700 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5701 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5702 | |
| 5703 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5704 | log_position + 1); |
| 5705 | |
| 5706 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5707 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5708 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5709 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5710 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5711 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5712 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5713 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5714 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5715 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5716 | |
| 5717 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5718 | // an HTTP redirect. |
| 5719 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5720 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5721 | |
| 5722 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5723 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5724 | TestURLRequestContext context(true); |
| 5725 | context.set_network_delegate(NULL); |
| 5726 | context.set_net_log(&net_log_); |
| 5727 | context.Init(); |
| 5728 | |
| 5729 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5730 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5731 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5732 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5733 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5734 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5735 | base::RunLoop().Run(); |
| 5736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5737 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5738 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5739 | } |
| 5740 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5741 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5742 | net_log_.GetEntries(&entries); |
| 5743 | |
| 5744 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5745 | // OnResponseStarted. |
| 5746 | size_t log_position = 0; |
| 5747 | for (int i = 0; i < 2; ++i) { |
| 5748 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5749 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5750 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5751 | |
| 5752 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5753 | log_position + 1); |
| 5754 | |
| 5755 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5756 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5757 | entries[log_position].type); |
| 5758 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5759 | } |
| 5760 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5761 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5762 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5763 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5764 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5768 | // an HTTP redirect, with cancellation at various points. |
| 5769 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5770 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5771 | |
| 5772 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5773 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5774 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5775 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5776 | }; |
| 5777 | |
| 5778 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5779 | ++test_case) { |
| 5780 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5781 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 5782 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5783 | context.set_network_delegate(NULL); |
| 5784 | context.set_net_log(&net_log); |
| 5785 | context.Init(); |
| 5786 | |
| 5787 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5788 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5789 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5790 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5791 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5792 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5793 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5794 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5795 | } |
| 5796 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5797 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5798 | net_log.GetEntries(&entries); |
| 5799 | |
| 5800 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5801 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5802 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5803 | // still currently supported in that call. |
| 5804 | size_t log_position = 0; |
| 5805 | for (int i = 0; i < 2; ++i) { |
| 5806 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5807 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5808 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5809 | |
| 5810 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5811 | log_position + 1); |
| 5812 | |
| 5813 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5814 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5815 | entries[log_position].type); |
| 5816 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5817 | } |
| 5818 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5819 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5820 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5821 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5822 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5823 | } |
| 5824 | } |
| 5825 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5826 | namespace { |
| 5827 | |
| 5828 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5829 | const char kExtraValue[] = "fubar"; |
| 5830 | |
| 5831 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5832 | void OnReceivedRedirect(URLRequest* request, |
| 5833 | const RedirectInfo& redirect_info, |
| 5834 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5835 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5836 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5837 | } |
| 5838 | }; |
| 5839 | |
| 5840 | } // namespace |
| 5841 | |
| 5842 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5843 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5844 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5845 | GURL destination_url = |
| 5846 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5847 | GURL original_url = |
| 5848 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5849 | RedirectWithAdditionalHeadersDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5850 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5851 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5852 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5853 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5854 | |
| 5855 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5856 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5857 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5858 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5859 | EXPECT_FALSE(req->is_pending()); |
| 5860 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5861 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5862 | } |
| 5863 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5864 | namespace { |
| 5865 | |
| 5866 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5867 | |
| 5868 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5869 | void OnReceivedRedirect(URLRequest* request, |
| 5870 | const RedirectInfo& redirect_info, |
| 5871 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5872 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5873 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5874 | } |
| 5875 | }; |
| 5876 | |
| 5877 | } // namespace |
| 5878 | |
| 5879 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5880 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5881 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5882 | GURL destination_url = http_test_server()->GetURL( |
| 5883 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5884 | GURL original_url = |
| 5885 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5886 | RedirectWithHeaderRemovalDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5887 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5888 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5889 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5890 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5891 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5892 | |
| 5893 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5894 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5895 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5896 | EXPECT_FALSE(req->is_pending()); |
| 5897 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5898 | EXPECT_EQ("None", d.data_received()); |
| 5899 | } |
| 5900 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5901 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5902 | TestDelegate d; |
| 5903 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5904 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5905 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 5906 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5907 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5908 | r->Start(); |
| 5909 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5910 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5911 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5912 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5913 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5914 | |
| 5915 | // We expect to receive OnResponseStarted even though the request has been |
| 5916 | // cancelled. |
| 5917 | EXPECT_EQ(1, d.response_started_count()); |
| 5918 | EXPECT_EQ(0, d.bytes_received()); |
| 5919 | EXPECT_FALSE(d.received_data_before_response()); |
| 5920 | } |
| 5921 | } |
| 5922 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5923 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5924 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5925 | |
| 5926 | TestDelegate d; |
| 5927 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5928 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5929 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5930 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5931 | |
| 5932 | d.set_cancel_in_response_started(true); |
| 5933 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5934 | r->Start(); |
| 5935 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5936 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5937 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5938 | |
| 5939 | EXPECT_EQ(1, d.response_started_count()); |
| 5940 | EXPECT_EQ(0, d.bytes_received()); |
| 5941 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5942 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5943 | } |
| 5944 | } |
| 5945 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5946 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5947 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5948 | |
| 5949 | TestDelegate d; |
| 5950 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5951 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5952 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5953 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5954 | |
| 5955 | d.set_cancel_in_received_data(true); |
| 5956 | |
| 5957 | r->Start(); |
| 5958 | EXPECT_TRUE(r->is_pending()); |
| 5959 | |
| 5960 | base::RunLoop().Run(); |
| 5961 | |
| 5962 | EXPECT_EQ(1, d.response_started_count()); |
| 5963 | EXPECT_NE(0, d.received_bytes_count()); |
| 5964 | EXPECT_FALSE(d.received_data_before_response()); |
| 5965 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 5966 | } |
| 5967 | } |
| 5968 | |
| 5969 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 5970 | ASSERT_TRUE(http_test_server()->Start()); |
| 5971 | |
| 5972 | TestDelegate d; |
| 5973 | { |
| 5974 | // This returns an empty response (With headers). |
| 5975 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5976 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5977 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5978 | |
| 5979 | d.set_cancel_in_received_data(true); |
| 5980 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5981 | r->Start(); |
| 5982 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5983 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5984 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5985 | |
| 5986 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5987 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5988 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5989 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5993 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5994 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5995 | |
| 5996 | TestDelegate d; |
| 5997 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5998 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5999 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6001 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6002 | r->Start(); |
| 6003 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6004 | |
| 6005 | // The request will be implicitly canceled when it is destroyed. The |
| 6006 | // test delegate must not post a quit message when this happens because |
| 6007 | // this test doesn't actually have a message loop. The quit message would |
| 6008 | // get put on this thread's message queue and the next test would exit |
| 6009 | // early, causing problems. |
| 6010 | d.set_quit_on_complete(false); |
| 6011 | } |
| 6012 | // expect things to just cleanup properly. |
| 6013 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6014 | // 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] | 6015 | // message loop |
| 6016 | EXPECT_FALSE(d.received_data_before_response()); |
| 6017 | EXPECT_EQ(0, d.bytes_received()); |
| 6018 | } |
| 6019 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6020 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6021 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6022 | |
| 6023 | // populate cache |
| 6024 | { |
| 6025 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6026 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6027 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6028 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6029 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6030 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6031 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | // cancel read from cache (see bug 990242) |
| 6035 | { |
| 6036 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6037 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6038 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6039 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6040 | r->Start(); |
| 6041 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6042 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6043 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6044 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6045 | EXPECT_EQ(1, d.response_started_count()); |
| 6046 | EXPECT_EQ(0, d.bytes_received()); |
| 6047 | EXPECT_FALSE(d.received_data_before_response()); |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6052 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6053 | HTTPUploadDataOperationTest("POST"); |
| 6054 | } |
| 6055 | |
| 6056 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6057 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6058 | HTTPUploadDataOperationTest("PUT"); |
| 6059 | } |
| 6060 | |
| 6061 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6062 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6063 | |
| 6064 | TestDelegate d; |
| 6065 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6066 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6067 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6068 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6069 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6070 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6071 | r->Start(); |
| 6072 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6073 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6074 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6075 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6076 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6077 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6078 | |
| 6079 | EXPECT_FALSE(d.received_data_before_response()); |
| 6080 | EXPECT_TRUE(d.data_received().empty()); |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6085 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6086 | |
| 6087 | TestDelegate d; |
| 6088 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6089 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6090 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6091 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6092 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6093 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6094 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6095 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6096 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6097 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6098 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6099 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6100 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6101 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6102 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6103 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6104 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6105 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6106 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6107 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6108 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6109 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6110 | r->Start(); |
| 6111 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6112 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6113 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6114 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6115 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6116 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6117 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6118 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6119 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6120 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6121 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6122 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6123 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6124 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6125 | |
| 6126 | EXPECT_FALSE(d.received_data_before_response()); |
| 6127 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6128 | EXPECT_EQ(size, d.bytes_received()); |
| 6129 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6130 | } |
| 6131 | } |
| 6132 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6133 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6134 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6135 | |
| 6136 | TestDelegate d; |
| 6137 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6138 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6139 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6140 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6141 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6142 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6143 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6144 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6145 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6146 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6147 | base::FilePath(FILE_PATH_LITERAL( |
| 6148 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6149 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6150 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6151 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6152 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6153 | r->Start(); |
| 6154 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6155 | |
| 6156 | base::RunLoop().Run(); |
| 6157 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6158 | EXPECT_TRUE(d.request_failed()); |
| 6159 | EXPECT_FALSE(d.received_data_before_response()); |
| 6160 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6161 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6165 | namespace { |
| 6166 | |
| 6167 | // Adds a standard set of data to an upload for chunked upload integration |
| 6168 | // tests. |
| 6169 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6170 | writer->AppendData("a", 1, false); |
| 6171 | writer->AppendData("bcd", 3, false); |
| 6172 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6173 | writer->AppendData("\r\n\r\n", 4, false); |
| 6174 | writer->AppendData("0", 1, false); |
| 6175 | writer->AppendData("2323", 4, true); |
| 6176 | } |
| 6177 | |
| 6178 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6179 | // the server. |
| 6180 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6181 | // This should match the chunks sent by AddChunksToUpload(). |
| 6182 | const std::string expected_data = |
| 6183 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6184 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6185 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6186 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6187 | |
| 6188 | EXPECT_FALSE(d->received_data_before_response()); |
| 6189 | |
| 6190 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6191 | EXPECT_EQ(expected_data, d->data_received()); |
| 6192 | } |
| 6193 | |
| 6194 | } // namespace |
| 6195 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6196 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6197 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6198 | |
| 6199 | TestDelegate d; |
| 6200 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6201 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6202 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6203 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6204 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6205 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6206 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6207 | upload_data_stream->CreateWriter(); |
| 6208 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6209 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6210 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6211 | r->Start(); |
| 6212 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6213 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6214 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6215 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6216 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6217 | } |
| 6218 | } |
| 6219 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6220 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6221 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6222 | |
| 6223 | TestDelegate d; |
| 6224 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6225 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6226 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6227 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6228 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6229 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6230 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6231 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6232 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6233 | r->set_method("POST"); |
| 6234 | r->Start(); |
| 6235 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6236 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6237 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6238 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6239 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6240 | } |
| 6241 | } |
| 6242 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6243 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6244 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6245 | |
| 6246 | TestDelegate d; |
| 6247 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6248 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6249 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6250 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6251 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6252 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6253 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6254 | upload_data_stream->CreateWriter(); |
| 6255 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6256 | r->set_method("POST"); |
| 6257 | r->Start(); |
| 6258 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6259 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6260 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6261 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6262 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6263 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6264 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6265 | } |
| 6266 | } |
| 6267 | |
| 6268 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6269 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6270 | |
| 6271 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6272 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6273 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6274 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6275 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6276 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6277 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6278 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6279 | |
| 6280 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6281 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6282 | |
| 6283 | std::string header; |
| 6284 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6285 | EXPECT_EQ("private", header); |
| 6286 | |
| 6287 | header.clear(); |
| 6288 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6289 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6290 | |
| 6291 | // The response has two "X-Multiple-Entries" headers. |
| 6292 | // This verfies our output has them concatenated together. |
| 6293 | header.clear(); |
| 6294 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6295 | EXPECT_EQ("a, b", header); |
| 6296 | } |
| 6297 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6298 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6299 | // security state. (see http://crbug.com/550977). |
| 6300 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6301 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6302 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6303 | https_test_server.SetSSLConfig( |
| 6304 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6305 | https_test_server.ServeFilesFromSourceDirectory( |
| 6306 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6307 | ASSERT_TRUE(https_test_server.Start()); |
| 6308 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6309 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6310 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6311 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6312 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6313 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6314 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6315 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6316 | |
| 6317 | TransportSecurityState* security_state = |
| 6318 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6319 | TransportSecurityState::STSState sts_state; |
| 6320 | TransportSecurityState::PKPState pkp_state; |
| 6321 | EXPECT_TRUE( |
| 6322 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6323 | EXPECT_FALSE( |
| 6324 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6325 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6326 | sts_state.upgrade_mode); |
| 6327 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6328 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6329 | #if defined(OS_ANDROID) |
| 6330 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6331 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6332 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6333 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6334 | } |
| 6335 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6336 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6337 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6338 | https_test_server.ServeFilesFromSourceDirectory( |
| 6339 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6340 | ASSERT_TRUE(https_test_server.Start()); |
| 6341 | // Make sure this test fails if the test server is changed to not |
| 6342 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6343 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6344 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6345 | |
| 6346 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6347 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6348 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6349 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6350 | request->Start(); |
| 6351 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6352 | TransportSecurityState* security_state = |
| 6353 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6354 | TransportSecurityState::STSState sts_state; |
| 6355 | EXPECT_FALSE( |
| 6356 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6357 | } |
| 6358 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6359 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6360 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
| 6361 | const char kExpectStapleStaticHostname[] = "expect-staple.preloaded.test"; |
| 6362 | const char kExpectStapleReportURI[] = |
| 6363 | "http://report-uri.preloaded.test/expect-staple"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6364 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 6365 | } // namespace |
| 6366 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6367 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 6368 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6369 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6370 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6371 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6372 | https_test_server.SetSSLConfig( |
| 6373 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6374 | https_test_server.ServeFilesFromSourceDirectory( |
| 6375 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6376 | ASSERT_TRUE(https_test_server.Start()); |
| 6377 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6378 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6379 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6380 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6381 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6382 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6383 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6384 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6385 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6386 | TransportSecurityState* security_state = |
| 6387 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6388 | TransportSecurityState::STSState sts_state; |
| 6389 | TransportSecurityState::PKPState pkp_state; |
| 6390 | EXPECT_FALSE( |
| 6391 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6392 | EXPECT_TRUE( |
| 6393 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6394 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6395 | sts_state.upgrade_mode); |
| 6396 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6397 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6398 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6399 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6400 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6401 | } |
| 6402 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6403 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6404 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6405 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6406 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6407 | https_test_server.SetSSLConfig( |
| 6408 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6409 | https_test_server.ServeFilesFromSourceDirectory( |
| 6410 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6411 | ASSERT_TRUE(https_test_server.Start()); |
| 6412 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6413 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6414 | |
| 6415 | // Set up a pin for |test_server_hostname|. |
| 6416 | TransportSecurityState security_state; |
| 6417 | const base::Time current_time(base::Time::Now()); |
| 6418 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6419 | HashValueVector hashes; |
| 6420 | HashValue hash1; |
| 6421 | HashValue hash2; |
| 6422 | // The values here don't matter, as long as they are different from |
| 6423 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6424 | ASSERT_TRUE( |
| 6425 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6426 | ASSERT_TRUE( |
| 6427 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6428 | hashes.push_back(hash1); |
| 6429 | hashes.push_back(hash2); |
| 6430 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6431 | false, /* include subdomains */ |
| 6432 | hashes, report_uri); |
| 6433 | |
| 6434 | MockCertificateReportSender mock_report_sender; |
| 6435 | security_state.SetReportSender(&mock_report_sender); |
| 6436 | |
| 6437 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6438 | // set pin. |
| 6439 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6440 | ASSERT_TRUE(cert); |
| 6441 | |
| 6442 | MockCertVerifier cert_verifier; |
| 6443 | CertVerifyResult verify_result; |
| 6444 | verify_result.verified_cert = cert; |
| 6445 | verify_result.is_issued_by_known_root = true; |
| 6446 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6447 | ASSERT_TRUE( |
| 6448 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6449 | verify_result.public_key_hashes.push_back(hash3); |
| 6450 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6451 | |
| 6452 | TestNetworkDelegate network_delegate; |
| 6453 | TestURLRequestContext context(true); |
| 6454 | context.set_transport_security_state(&security_state); |
| 6455 | context.set_network_delegate(&network_delegate); |
| 6456 | context.set_cert_verifier(&cert_verifier); |
| 6457 | context.Init(); |
| 6458 | |
| 6459 | // Now send a request to trigger the violation. |
| 6460 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6461 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6462 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 6463 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6464 | violating_request->Start(); |
| 6465 | base::RunLoop().Run(); |
| 6466 | |
| 6467 | // Check that a report was sent. |
| 6468 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6469 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6470 | EXPECT_EQ("application/json; charset=utf-8", |
| 6471 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6472 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6473 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6474 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6475 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6476 | base::DictionaryValue* report_dict; |
| 6477 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6478 | std::string report_hostname; |
| 6479 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6480 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6481 | } |
| 6482 | |
| 6483 | // Tests that reports get sent on requests with |
| 6484 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6485 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6486 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6487 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6488 | https_test_server.SetSSLConfig( |
| 6489 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6490 | https_test_server.ServeFilesFromSourceDirectory( |
| 6491 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6492 | ASSERT_TRUE(https_test_server.Start()); |
| 6493 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6494 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6495 | |
| 6496 | TransportSecurityState security_state; |
| 6497 | MockCertificateReportSender mock_report_sender; |
| 6498 | security_state.SetReportSender(&mock_report_sender); |
| 6499 | |
| 6500 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6501 | // header. |
| 6502 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6503 | ASSERT_TRUE(cert); |
| 6504 | |
| 6505 | MockCertVerifier cert_verifier; |
| 6506 | CertVerifyResult verify_result; |
| 6507 | verify_result.verified_cert = cert; |
| 6508 | verify_result.is_issued_by_known_root = true; |
| 6509 | HashValue hash; |
| 6510 | // This value doesn't matter, as long as it is different from the pins |
| 6511 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6512 | ASSERT_TRUE( |
| 6513 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6514 | verify_result.public_key_hashes.push_back(hash); |
| 6515 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6516 | |
| 6517 | TestNetworkDelegate network_delegate; |
| 6518 | TestURLRequestContext context(true); |
| 6519 | context.set_transport_security_state(&security_state); |
| 6520 | context.set_network_delegate(&network_delegate); |
| 6521 | context.set_cert_verifier(&cert_verifier); |
| 6522 | context.Init(); |
| 6523 | |
| 6524 | // Now send a request to trigger the violation. |
| 6525 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6526 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6527 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6528 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6529 | violating_request->Start(); |
| 6530 | base::RunLoop().Run(); |
| 6531 | |
| 6532 | // Check that a report was sent. |
| 6533 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6534 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6535 | EXPECT_EQ("application/json; charset=utf-8", |
| 6536 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6537 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6538 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6539 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6540 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6541 | base::DictionaryValue* report_dict; |
| 6542 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6543 | std::string report_hostname; |
| 6544 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6545 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6546 | } |
| 6547 | |
| 6548 | // Tests that reports do not get sent on requests with |
| 6549 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6550 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6551 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6552 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6553 | https_test_server.SetSSLConfig( |
| 6554 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6555 | https_test_server.ServeFilesFromSourceDirectory( |
| 6556 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6557 | ASSERT_TRUE(https_test_server.Start()); |
| 6558 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6559 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6560 | |
| 6561 | TransportSecurityState security_state; |
| 6562 | MockCertificateReportSender mock_report_sender; |
| 6563 | security_state.SetReportSender(&mock_report_sender); |
| 6564 | |
| 6565 | TestNetworkDelegate network_delegate; |
| 6566 | MockCertVerifier mock_cert_verifier; |
| 6567 | TestURLRequestContext context(true); |
| 6568 | context.set_transport_security_state(&security_state); |
| 6569 | context.set_network_delegate(&network_delegate); |
| 6570 | context.set_cert_verifier(&mock_cert_verifier); |
| 6571 | mock_cert_verifier.set_default_result(OK); |
| 6572 | context.Init(); |
| 6573 | |
| 6574 | // Now send a request that does not trigger the violation. |
| 6575 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6576 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6577 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6578 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6579 | request->Start(); |
| 6580 | base::RunLoop().Run(); |
| 6581 | |
| 6582 | // Check that a report was not sent. |
| 6583 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6584 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6585 | } |
| 6586 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6587 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6588 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6589 | https_test_server.ServeFilesFromSourceDirectory( |
| 6590 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6591 | ASSERT_TRUE(https_test_server.Start()); |
| 6592 | // Make sure this test fails if the test server is changed to not |
| 6593 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6594 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6595 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6596 | |
| 6597 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6598 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6599 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6600 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6601 | request->Start(); |
| 6602 | base::RunLoop().Run(); |
| 6603 | |
| 6604 | TransportSecurityState* security_state = |
| 6605 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6606 | TransportSecurityState::PKPState pkp_state; |
| 6607 | EXPECT_FALSE( |
| 6608 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6609 | } |
| 6610 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6611 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6612 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6613 | https_test_server.SetSSLConfig( |
| 6614 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6615 | https_test_server.ServeFilesFromSourceDirectory( |
| 6616 | base::FilePath(kTestFilePath)); |
| 6617 | ASSERT_TRUE(https_test_server.Start()); |
| 6618 | |
| 6619 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6620 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6621 | ASSERT_TRUE(cert); |
| 6622 | |
| 6623 | MockCertVerifier cert_verifier; |
| 6624 | CertVerifyResult verify_result; |
| 6625 | verify_result.verified_cert = cert; |
| 6626 | verify_result.is_issued_by_known_root = false; |
| 6627 | HashValue hash; |
| 6628 | ASSERT_TRUE( |
| 6629 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6630 | verify_result.public_key_hashes.push_back(hash); |
| 6631 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6632 | cert_verifier.set_default_result(OK); |
| 6633 | |
| 6634 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6635 | |
| 6636 | // Set up HPKP |
| 6637 | base::Time current_time = base::Time::Now(); |
| 6638 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6639 | HashValue pin; |
| 6640 | ASSERT_TRUE( |
| 6641 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6642 | HashValueVector hashes; |
| 6643 | hashes.push_back(pin); |
| 6644 | GURL report_uri(kHPKPReportUri); |
| 6645 | TransportSecurityState security_state; |
| 6646 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6647 | false, /* include subdomains */ |
| 6648 | hashes, report_uri); |
| 6649 | |
| 6650 | TestNetworkDelegate network_delegate; |
| 6651 | TestURLRequestContext context(true); |
| 6652 | context.set_transport_security_state(&security_state); |
| 6653 | context.set_network_delegate(&network_delegate); |
| 6654 | context.set_cert_verifier(&cert_verifier); |
| 6655 | context.Init(); |
| 6656 | |
| 6657 | TestDelegate d; |
| 6658 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6659 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6660 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6661 | request->Start(); |
| 6662 | base::RunLoop().Run(); |
| 6663 | |
| 6664 | TransportSecurityState::PKPState pkp_state; |
| 6665 | EXPECT_TRUE( |
| 6666 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6667 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6668 | } |
| 6669 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6670 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6671 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6672 | https_test_server.SetSSLConfig( |
| 6673 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6674 | https_test_server.ServeFilesFromSourceDirectory( |
| 6675 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6676 | ASSERT_TRUE(https_test_server.Start()); |
| 6677 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6678 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6679 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6680 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6681 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6682 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6683 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6684 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6685 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6686 | |
| 6687 | // We should have set parameters from the first header, not the second. |
| 6688 | TransportSecurityState* security_state = |
| 6689 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6690 | TransportSecurityState::STSState sts_state; |
| 6691 | EXPECT_TRUE( |
| 6692 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6693 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6694 | sts_state.upgrade_mode); |
| 6695 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6696 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6697 | } |
| 6698 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6699 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6700 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6701 | https_test_server.SetSSLConfig( |
| 6702 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6703 | https_test_server.ServeFilesFromSourceDirectory( |
| 6704 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6705 | ASSERT_TRUE(https_test_server.Start()); |
| 6706 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6707 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6708 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6709 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6710 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6711 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6712 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6713 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6714 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6715 | |
| 6716 | // We should have set parameters from the first header, not the second. |
| 6717 | TransportSecurityState* security_state = |
| 6718 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6719 | TransportSecurityState::STSState sts_state; |
| 6720 | TransportSecurityState::PKPState pkp_state; |
| 6721 | EXPECT_TRUE( |
| 6722 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6723 | EXPECT_TRUE( |
| 6724 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6725 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6726 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6727 | #if defined(OS_ANDROID) |
| 6728 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6729 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6730 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6731 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6732 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6733 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6734 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6735 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6736 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6737 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6738 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6739 | } |
| 6740 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6741 | // Tests that when multiple HPKP headers are present, asserting different |
| 6742 | // policies, that only the first such policy is processed. |
| 6743 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6744 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6745 | https_test_server.SetSSLConfig( |
| 6746 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6747 | https_test_server.ServeFilesFromSourceDirectory( |
| 6748 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6749 | ASSERT_TRUE(https_test_server.Start()); |
| 6750 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6751 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6752 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6753 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6754 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6755 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6756 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6757 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6758 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6759 | |
| 6760 | TransportSecurityState* security_state = |
| 6761 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6762 | TransportSecurityState::STSState sts_state; |
| 6763 | TransportSecurityState::PKPState pkp_state; |
| 6764 | EXPECT_TRUE( |
| 6765 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6766 | EXPECT_TRUE( |
| 6767 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6768 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6769 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6770 | #if defined(OS_ANDROID) |
| 6771 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6772 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6773 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6774 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6775 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6776 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6777 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6778 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6779 | } |
| 6780 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6781 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6782 | // called. |
| 6783 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6784 | public: |
| 6785 | MockExpectCTReporter() : num_failures_(0) {} |
| 6786 | ~MockExpectCTReporter() override {} |
| 6787 | |
| 6788 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6789 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6790 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6791 | const X509Certificate* validated_certificate_chain, |
| 6792 | const X509Certificate* served_certificate_chain, |
| 6793 | const SignedCertificateTimestampAndStatusList& |
| 6794 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6795 | num_failures_++; |
| 6796 | } |
| 6797 | |
| 6798 | uint32_t num_failures() { return num_failures_; } |
| 6799 | |
| 6800 | private: |
| 6801 | uint32_t num_failures_; |
| 6802 | }; |
| 6803 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6804 | // A CTPolicyEnforcer that returns a default CertPolicyCompliance value |
| 6805 | // for every certificate. |
| 6806 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6807 | public: |
| 6808 | MockCTPolicyEnforcer() |
| 6809 | : default_result_( |
| 6810 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS) {} |
| 6811 | ~MockCTPolicyEnforcer() override {} |
| 6812 | |
| 6813 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 6814 | X509Certificate* cert, |
| 6815 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6816 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6817 | return default_result_; |
| 6818 | } |
| 6819 | |
| 6820 | void set_default_result(ct::CertPolicyCompliance default_result) { |
| 6821 | default_result_ = default_result; |
| 6822 | } |
| 6823 | |
| 6824 | private: |
| 6825 | ct::CertPolicyCompliance default_result_; |
| 6826 | }; |
| 6827 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6828 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 6829 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6830 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6831 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6832 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6833 | https_test_server.SetSSLConfig( |
| 6834 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6835 | https_test_server.ServeFilesFromSourceDirectory( |
| 6836 | base::FilePath(kTestFilePath)); |
| 6837 | ASSERT_TRUE(https_test_server.Start()); |
| 6838 | |
| 6839 | MockExpectCTReporter reporter; |
| 6840 | TransportSecurityState transport_security_state; |
| 6841 | transport_security_state.enable_static_expect_ct_ = true; |
| 6842 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6843 | |
| 6844 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6845 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6846 | ASSERT_TRUE(cert); |
| 6847 | MockCertVerifier cert_verifier; |
| 6848 | CertVerifyResult verify_result; |
| 6849 | verify_result.verified_cert = cert; |
| 6850 | verify_result.is_issued_by_known_root = true; |
| 6851 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6852 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 6853 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 6854 | // CT violation. |
| 6855 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6856 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6857 | ct_policy_enforcer.set_default_result( |
| 6858 | ct::CertPolicyCompliance::CERT_POLICY_NOT_ENOUGH_SCTS); |
| 6859 | |
| 6860 | TestNetworkDelegate network_delegate; |
| 6861 | // Use a MockHostResolver (which by default maps all hosts to |
| 6862 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6863 | // CT preload list. |
| 6864 | MockHostResolver host_resolver; |
| 6865 | TestURLRequestContext context(true); |
| 6866 | context.set_host_resolver(&host_resolver); |
| 6867 | context.set_transport_security_state(&transport_security_state); |
| 6868 | context.set_network_delegate(&network_delegate); |
| 6869 | context.set_cert_verifier(&cert_verifier); |
| 6870 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 6871 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6872 | context.Init(); |
| 6873 | |
| 6874 | // Now send a request to trigger the violation. |
| 6875 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6876 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6877 | GURL::Replacements replace_host; |
| 6878 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6879 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6880 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 6881 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6882 | violating_request->Start(); |
| 6883 | base::RunLoop().Run(); |
| 6884 | |
| 6885 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6886 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6887 | |
| 6888 | // Tests that Expect CT HTTP headers are processed correctly. |
| 6889 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6890 | base::test::ScopedFeatureList feature_list; |
| 6891 | feature_list.InitAndEnableFeature( |
| 6892 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6893 | |
| 6894 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6895 | https_test_server.SetSSLConfig( |
| 6896 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6897 | https_test_server.ServeFilesFromSourceDirectory( |
| 6898 | base::FilePath(kTestFilePath)); |
| 6899 | ASSERT_TRUE(https_test_server.Start()); |
| 6900 | |
| 6901 | MockExpectCTReporter reporter; |
| 6902 | TransportSecurityState transport_security_state; |
| 6903 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6904 | |
| 6905 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6906 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6907 | ASSERT_TRUE(cert); |
| 6908 | MockCertVerifier cert_verifier; |
| 6909 | CertVerifyResult verify_result; |
| 6910 | verify_result.verified_cert = cert; |
| 6911 | verify_result.is_issued_by_known_root = true; |
| 6912 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6913 | |
| 6914 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6915 | // compliance. |
| 6916 | DoNothingCTVerifier ct_verifier; |
| 6917 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6918 | ct_policy_enforcer.set_default_result( |
| 6919 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6920 | |
| 6921 | TestNetworkDelegate network_delegate; |
| 6922 | // Use a MockHostResolver (which by default maps all hosts to |
| 6923 | // 127.0.0.1). |
| 6924 | MockHostResolver host_resolver; |
| 6925 | TestURLRequestContext context(true); |
| 6926 | context.set_host_resolver(&host_resolver); |
| 6927 | context.set_transport_security_state(&transport_security_state); |
| 6928 | context.set_network_delegate(&network_delegate); |
| 6929 | context.set_cert_verifier(&cert_verifier); |
| 6930 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6931 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6932 | context.Init(); |
| 6933 | |
| 6934 | // Now send a request to trigger the header processing. |
| 6935 | TestDelegate d; |
| 6936 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6937 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6938 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6939 | request->Start(); |
| 6940 | base::RunLoop().Run(); |
| 6941 | |
| 6942 | TransportSecurityState::ExpectCTState state; |
| 6943 | ASSERT_TRUE( |
| 6944 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6945 | EXPECT_TRUE(state.enforce); |
| 6946 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6947 | } |
| 6948 | |
| 6949 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 6950 | // processed. |
| 6951 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 6952 | base::test::ScopedFeatureList feature_list; |
| 6953 | feature_list.InitAndEnableFeature( |
| 6954 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6955 | |
| 6956 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6957 | https_test_server.SetSSLConfig( |
| 6958 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6959 | https_test_server.ServeFilesFromSourceDirectory( |
| 6960 | base::FilePath(kTestFilePath)); |
| 6961 | ASSERT_TRUE(https_test_server.Start()); |
| 6962 | |
| 6963 | MockExpectCTReporter reporter; |
| 6964 | TransportSecurityState transport_security_state; |
| 6965 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6966 | |
| 6967 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6968 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6969 | ASSERT_TRUE(cert); |
| 6970 | MockCertVerifier cert_verifier; |
| 6971 | CertVerifyResult verify_result; |
| 6972 | verify_result.verified_cert = cert; |
| 6973 | verify_result.is_issued_by_known_root = true; |
| 6974 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6975 | |
| 6976 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6977 | // compliance. |
| 6978 | DoNothingCTVerifier ct_verifier; |
| 6979 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6980 | ct_policy_enforcer.set_default_result( |
| 6981 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6982 | |
| 6983 | TestNetworkDelegate network_delegate; |
| 6984 | // Use a MockHostResolver (which by default maps all hosts to |
| 6985 | // 127.0.0.1). |
| 6986 | MockHostResolver host_resolver; |
| 6987 | TestURLRequestContext context(true); |
| 6988 | context.set_host_resolver(&host_resolver); |
| 6989 | context.set_transport_security_state(&transport_security_state); |
| 6990 | context.set_network_delegate(&network_delegate); |
| 6991 | context.set_cert_verifier(&cert_verifier); |
| 6992 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6993 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6994 | context.Init(); |
| 6995 | |
| 6996 | // Now send a request to trigger the header processing. |
| 6997 | TestDelegate d; |
| 6998 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 6999 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7000 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7001 | request->Start(); |
| 7002 | base::RunLoop().Run(); |
| 7003 | |
| 7004 | TransportSecurityState::ExpectCTState state; |
| 7005 | ASSERT_TRUE( |
| 7006 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7007 | EXPECT_TRUE(state.enforce); |
| 7008 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7009 | } |
| 7010 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7011 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7012 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7013 | #if BUILDFLAG(ENABLE_REPORTING) |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7014 | namespace { |
| 7015 | |
| 7016 | class TestReportingService : public ReportingService { |
| 7017 | public: |
| 7018 | struct Header { |
| 7019 | GURL url; |
| 7020 | std::string header_value; |
| 7021 | }; |
| 7022 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7023 | const std::vector<Header>& headers() { return headers_; } |
| 7024 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7025 | // ReportingService implementation: |
| 7026 | |
| 7027 | ~TestReportingService() override {} |
| 7028 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7029 | void QueueReport(const GURL& url, |
| 7030 | const std::string& group, |
| 7031 | const std::string& type, |
| 7032 | std::unique_ptr<const base::Value> body) override { |
| 7033 | NOTIMPLEMENTED(); |
| 7034 | } |
| 7035 | |
| 7036 | void ProcessHeader(const GURL& url, |
| 7037 | const std::string& header_value) override { |
| 7038 | headers_.push_back({url, header_value}); |
| 7039 | } |
| 7040 | |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 7041 | void RemoveBrowsingData( |
| 7042 | int data_type_mask, |
| 7043 | base::Callback<bool(const GURL&)> origin_filter) override { |
| 7044 | NOTIMPLEMENTED(); |
| 7045 | } |
| 7046 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7047 | private: |
| 7048 | std::vector<Header> headers_; |
| 7049 | }; |
| 7050 | |
| 7051 | std::unique_ptr<test_server::HttpResponse> SendReportToHeader( |
| 7052 | const test_server::HttpRequest& request) { |
| 7053 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7054 | new test_server::BasicHttpResponse); |
| 7055 | http_response->set_code(HTTP_OK); |
| 7056 | http_response->AddCustomHeader("Report-To", "foo"); |
| 7057 | http_response->AddCustomHeader("Report-To", "bar"); |
| 7058 | return std::move(http_response); |
| 7059 | } |
| 7060 | |
| 7061 | } // namespace |
| 7062 | |
| 7063 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderNoService) { |
| 7064 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7065 | ASSERT_TRUE(http_test_server()->Start()); |
| 7066 | GURL request_url = http_test_server()->GetURL("/"); |
| 7067 | |
| 7068 | TestNetworkDelegate network_delegate; |
| 7069 | TestURLRequestContext context(true); |
| 7070 | context.set_network_delegate(&network_delegate); |
| 7071 | context.Init(); |
| 7072 | |
| 7073 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7074 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7075 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7076 | request->Start(); |
| 7077 | base::RunLoop().Run(); |
| 7078 | } |
| 7079 | |
| 7080 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderHTTP) { |
| 7081 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7082 | ASSERT_TRUE(http_test_server()->Start()); |
| 7083 | GURL request_url = http_test_server()->GetURL("/"); |
| 7084 | |
| 7085 | TestNetworkDelegate network_delegate; |
| 7086 | TestReportingService reporting_service; |
| 7087 | TestURLRequestContext context(true); |
| 7088 | context.set_network_delegate(&network_delegate); |
| 7089 | context.set_reporting_service(&reporting_service); |
| 7090 | context.Init(); |
| 7091 | |
| 7092 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7093 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7094 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7095 | request->Start(); |
| 7096 | base::RunLoop().Run(); |
| 7097 | |
| 7098 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7099 | } |
| 7100 | |
| 7101 | TEST_F(URLRequestTestHTTP, ProcessReportToHeaderHTTPS) { |
| 7102 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7103 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7104 | ASSERT_TRUE(https_test_server.Start()); |
| 7105 | GURL request_url = https_test_server.GetURL("/"); |
| 7106 | |
| 7107 | TestNetworkDelegate network_delegate; |
| 7108 | TestReportingService reporting_service; |
| 7109 | TestURLRequestContext context(true); |
| 7110 | context.set_network_delegate(&network_delegate); |
| 7111 | context.set_reporting_service(&reporting_service); |
| 7112 | context.Init(); |
| 7113 | |
| 7114 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7115 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7116 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7117 | request->Start(); |
| 7118 | base::RunLoop().Run(); |
| 7119 | |
| 7120 | ASSERT_EQ(1u, reporting_service.headers().size()); |
| 7121 | EXPECT_EQ(request_url, reporting_service.headers()[0].url); |
| 7122 | EXPECT_EQ("foo, bar", reporting_service.headers()[0].header_value); |
| 7123 | } |
| 7124 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7125 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderInvalidHttps) { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7126 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7127 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7128 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7129 | ASSERT_TRUE(https_test_server.Start()); |
| 7130 | GURL request_url = https_test_server.GetURL("/"); |
| 7131 | |
| 7132 | TestNetworkDelegate network_delegate; |
| 7133 | TestReportingService reporting_service; |
| 7134 | TestURLRequestContext context(true); |
| 7135 | context.set_network_delegate(&network_delegate); |
| 7136 | context.set_reporting_service(&reporting_service); |
| 7137 | context.Init(); |
| 7138 | |
| 7139 | TestDelegate d; |
| 7140 | d.set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7141 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7142 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7143 | request->Start(); |
| 7144 | base::RunLoop().Run(); |
| 7145 | |
| 7146 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7147 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7148 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7149 | } |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7150 | |
| 7151 | // Network Error Logging is dependent on the Reporting API, so only run NEL |
| 7152 | // tests if Reporting is enabled in the build. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7153 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7154 | namespace { |
| 7155 | |
| 7156 | class TestNetworkErrorLoggingDelegate : public NetworkErrorLoggingDelegate { |
| 7157 | public: |
| 7158 | struct Header { |
| 7159 | Header() {} |
| 7160 | ~Header() {} |
| 7161 | |
| 7162 | url::Origin origin; |
| 7163 | std::string value; |
| 7164 | }; |
| 7165 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7166 | const std::vector<Header>& headers() { return headers_; } |
Julia Tuttle | b0d4547 | 2017-09-25 20:14:43 | [diff] [blame] | 7167 | const std::vector<ErrorDetails>& errors() { return errors_; } |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7168 | |
| 7169 | // NetworkErrorLoggingDelegate implementation: |
| 7170 | |
| 7171 | ~TestNetworkErrorLoggingDelegate() override {} |
| 7172 | |
| 7173 | void SetReportingService(ReportingService* reporting_service) override { |
| 7174 | NOTREACHED(); |
| 7175 | } |
| 7176 | |
| 7177 | void OnHeader(const url::Origin& origin, const std::string& value) override { |
| 7178 | Header header; |
| 7179 | header.origin = origin; |
| 7180 | header.value = value; |
| 7181 | headers_.push_back(header); |
| 7182 | } |
| 7183 | |
Julia Tuttle | b0d4547 | 2017-09-25 20:14:43 | [diff] [blame] | 7184 | void OnNetworkError(const ErrorDetails& details) override { |
| 7185 | errors_.push_back(details); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7186 | } |
| 7187 | |
| 7188 | private: |
| 7189 | std::vector<Header> headers_; |
Julia Tuttle | b0d4547 | 2017-09-25 20:14:43 | [diff] [blame] | 7190 | std::vector<ErrorDetails> errors_; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7191 | }; |
| 7192 | |
| 7193 | std::unique_ptr<test_server::HttpResponse> SendNelHeader( |
| 7194 | const test_server::HttpRequest& request) { |
| 7195 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7196 | new test_server::BasicHttpResponse); |
| 7197 | http_response->set_code(HTTP_OK); |
| 7198 | http_response->AddCustomHeader(NetworkErrorLoggingDelegate::kHeaderName, |
| 7199 | "foo"); |
| 7200 | return std::move(http_response); |
| 7201 | } |
| 7202 | |
| 7203 | } // namespace |
| 7204 | |
| 7205 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderNoDelegate) { |
| 7206 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7207 | ASSERT_TRUE(http_test_server()->Start()); |
| 7208 | GURL request_url = http_test_server()->GetURL("/"); |
| 7209 | |
| 7210 | TestNetworkDelegate network_delegate; |
| 7211 | TestURLRequestContext context(true); |
| 7212 | context.set_network_delegate(&network_delegate); |
| 7213 | context.Init(); |
| 7214 | |
| 7215 | TestDelegate d; |
| 7216 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7217 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7218 | request->Start(); |
| 7219 | base::RunLoop().Run(); |
| 7220 | } |
| 7221 | |
| 7222 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderHttp) { |
| 7223 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7224 | ASSERT_TRUE(http_test_server()->Start()); |
| 7225 | GURL request_url = http_test_server()->GetURL("/"); |
| 7226 | |
| 7227 | TestNetworkDelegate network_delegate; |
| 7228 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7229 | TestURLRequestContext context(true); |
| 7230 | context.set_network_delegate(&network_delegate); |
| 7231 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7232 | context.Init(); |
| 7233 | |
| 7234 | TestDelegate d; |
| 7235 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7236 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7237 | request->Start(); |
| 7238 | base::RunLoop().Run(); |
| 7239 | |
| 7240 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7241 | } |
| 7242 | |
| 7243 | TEST_F(URLRequestTestHTTP, ProcessNelHeaderHttps) { |
| 7244 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7245 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7246 | ASSERT_TRUE(https_test_server.Start()); |
| 7247 | GURL request_url = https_test_server.GetURL("/"); |
| 7248 | |
| 7249 | TestNetworkDelegate network_delegate; |
| 7250 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7251 | TestURLRequestContext context(true); |
| 7252 | context.set_network_delegate(&network_delegate); |
| 7253 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7254 | context.Init(); |
| 7255 | |
| 7256 | TestDelegate d; |
| 7257 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7258 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7259 | request->Start(); |
| 7260 | base::RunLoop().Run(); |
| 7261 | |
| 7262 | ASSERT_EQ(1u, nel_delegate.headers().size()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 7263 | EXPECT_EQ(url::Origin::Create(request_url), nel_delegate.headers()[0].origin); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7264 | EXPECT_EQ("foo", nel_delegate.headers()[0].value); |
| 7265 | } |
| 7266 | |
| 7267 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderInvalidHttps) { |
| 7268 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7269 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7270 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7271 | ASSERT_TRUE(https_test_server.Start()); |
| 7272 | GURL request_url = https_test_server.GetURL("/"); |
| 7273 | |
| 7274 | TestNetworkDelegate network_delegate; |
| 7275 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7276 | TestURLRequestContext context(true); |
| 7277 | context.set_network_delegate(&network_delegate); |
| 7278 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7279 | context.Init(); |
| 7280 | |
| 7281 | TestDelegate d; |
| 7282 | d.set_allow_certificate_errors(true); |
| 7283 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7284 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7285 | request->Start(); |
| 7286 | base::RunLoop().Run(); |
| 7287 | |
| 7288 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7289 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7290 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7291 | } |
| 7292 | |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7293 | TEST_F(URLRequestTestHTTP, DontForwardErrorToNelNoDelegate) { |
| 7294 | URLRequestFailedJob::AddUrlHandler(); |
| 7295 | |
| 7296 | GURL request_url = |
| 7297 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7298 | |
| 7299 | TestNetworkDelegate network_delegate; |
| 7300 | TestURLRequestContext context(true); |
| 7301 | context.set_network_delegate(&network_delegate); |
| 7302 | context.Init(); |
| 7303 | |
| 7304 | TestDelegate d; |
| 7305 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7306 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7307 | request->Start(); |
| 7308 | base::RunLoop().Run(); |
| 7309 | |
| 7310 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7311 | } |
| 7312 | |
| 7313 | // TODO(juliatuttle): Figure out whether this restriction should be in place, |
| 7314 | // and either implement it or remove this test. |
| 7315 | TEST_F(URLRequestTestHTTP, DISABLED_DontForwardErrorToNelHttp) { |
| 7316 | URLRequestFailedJob::AddUrlHandler(); |
| 7317 | |
| 7318 | GURL request_url = |
| 7319 | URLRequestFailedJob::GetMockHttpUrl(ERR_CONNECTION_REFUSED); |
| 7320 | |
| 7321 | TestNetworkDelegate network_delegate; |
| 7322 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7323 | TestURLRequestContext context(true); |
| 7324 | context.set_network_delegate(&network_delegate); |
| 7325 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7326 | context.Init(); |
| 7327 | |
| 7328 | TestDelegate d; |
| 7329 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7330 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7331 | request->Start(); |
| 7332 | base::RunLoop().Run(); |
| 7333 | |
| 7334 | EXPECT_TRUE(nel_delegate.errors().empty()); |
| 7335 | |
| 7336 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7337 | } |
| 7338 | |
| 7339 | TEST_F(URLRequestTestHTTP, ForwardErrorToNelHttps) { |
| 7340 | URLRequestFailedJob::AddUrlHandler(); |
| 7341 | |
| 7342 | GURL request_url = |
| 7343 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7344 | |
| 7345 | TestNetworkDelegate network_delegate; |
| 7346 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7347 | TestURLRequestContext context(true); |
| 7348 | context.set_network_delegate(&network_delegate); |
| 7349 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7350 | context.Init(); |
| 7351 | |
| 7352 | TestDelegate d; |
| 7353 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7354 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7355 | request->Start(); |
| 7356 | base::RunLoop().Run(); |
| 7357 | |
| 7358 | ASSERT_EQ(1u, nel_delegate.errors().size()); |
| 7359 | EXPECT_EQ(request_url, nel_delegate.errors()[0].uri); |
| 7360 | EXPECT_EQ(0, nel_delegate.errors()[0].status_code); |
| 7361 | EXPECT_EQ(ERR_CONNECTION_REFUSED, nel_delegate.errors()[0].type); |
| 7362 | |
| 7363 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7364 | } |
| 7365 | |
| 7366 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7367 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7368 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7369 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7370 | |
| 7371 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7372 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7373 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7374 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7375 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7376 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7377 | |
| 7378 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7379 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7380 | EXPECT_EQ("text/html", mime_type); |
| 7381 | |
| 7382 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7383 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7384 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7385 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7386 | } |
| 7387 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7388 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7389 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7390 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7391 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7392 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7393 | |
| 7394 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7395 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7396 | } |
| 7397 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7398 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7399 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7400 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7401 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7402 | FileProtocolHandler file_protocol_handler( |
| 7403 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7404 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7405 | |
| 7406 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7407 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7408 | } |
| 7409 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7410 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7411 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7412 | |
| 7413 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7414 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7415 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7416 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7417 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7418 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7419 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7420 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7421 | |
| 7422 | // The redirect should have been rejected before reporting it to the caller. |
| 7423 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7424 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7425 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7426 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7427 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7428 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7429 | |
| 7430 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7431 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7432 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7433 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7434 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 7435 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7436 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7437 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7438 | |
| 7439 | // The redirect should have been rejected before reporting it to the |
| 7440 | // caller. See https://crbug.com/723796 |
| 7441 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7442 | } |
| 7443 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7444 | // Test that redirects to invalid URLs are rejected. See |
| 7445 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7446 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7447 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7448 | |
| 7449 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7450 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7451 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7452 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7453 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7454 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7455 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7456 | EXPECT_EQ(1, d.response_started_count()); |
| 7457 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7458 | |
| 7459 | // The redirect should have been rejected before reporting it to the caller. |
| 7460 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7461 | } |
| 7462 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7463 | // Make sure redirects are cached, despite not reading their bodies. |
| 7464 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7465 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7466 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7467 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7468 | |
| 7469 | { |
| 7470 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7471 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7472 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7473 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7474 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7475 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7476 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7477 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7478 | } |
| 7479 | |
| 7480 | { |
| 7481 | TestDelegate d; |
| 7482 | d.set_quit_on_redirect(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7483 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7484 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7485 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7486 | base::RunLoop().Run(); |
| 7487 | |
| 7488 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7489 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7490 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7491 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7492 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7493 | base::RunLoop().Run(); |
| 7494 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7495 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7496 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7497 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7498 | } |
| 7499 | } |
| 7500 | |
| 7501 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7502 | // when the headers are read, since the body won't have been read. |
| 7503 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7504 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7505 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7506 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7507 | |
| 7508 | { |
| 7509 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7510 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7511 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7512 | redirect_to_url); |
| 7513 | |
| 7514 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7515 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7516 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7517 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7518 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7519 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7520 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7521 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | { |
| 7525 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7526 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7527 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7528 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7529 | base::RunLoop().Run(); |
| 7530 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7531 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7532 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7533 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7534 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7535 | } |
| 7536 | } |
| 7537 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7538 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7539 | // safe. |
| 7540 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7541 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7542 | |
| 7543 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7544 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7545 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7546 | |
| 7547 | TestDelegate d; |
| 7548 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7549 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7550 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7551 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7552 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7553 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7554 | base::RunLoop().Run(); |
| 7555 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7556 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7557 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7558 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7559 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7560 | } |
| 7561 | } |
| 7562 | |
| 7563 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7564 | // some other URL to the whitelist. |
| 7565 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7566 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7567 | |
| 7568 | GURL unsafe_url("data:text/html,something"); |
| 7569 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7570 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7571 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7572 | different_unsafe_url); |
| 7573 | |
| 7574 | TestDelegate d; |
| 7575 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7576 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7577 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7578 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7579 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7580 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7581 | base::RunLoop().Run(); |
| 7582 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7583 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7584 | |
| 7585 | // The redirect should have been rejected before reporting it to the caller. |
| 7586 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7587 | } |
| 7588 | } |
| 7589 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7590 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7591 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7592 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7593 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7594 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7595 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7596 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7597 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7598 | |
| 7599 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7600 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7601 | |
| 7602 | TestDelegate d; |
| 7603 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7604 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7605 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7606 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7607 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7608 | base::RunLoop().Run(); |
| 7609 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7610 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7611 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7612 | EXPECT_EQ(original_url, r->original_url()); |
| 7613 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7618 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7619 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7620 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7621 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7622 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7623 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7624 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7625 | GURL expected_redirect_url( |
| 7626 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7627 | |
| 7628 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7629 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7630 | |
| 7631 | TestDelegate d; |
| 7632 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7633 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7634 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7635 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7636 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7637 | base::RunLoop().Run(); |
| 7638 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7639 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7640 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7641 | EXPECT_EQ(original_url, r->original_url()); |
| 7642 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7643 | } |
| 7644 | } |
| 7645 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7646 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7647 | // URL should not be changed. In particular, the reference fragment should not |
| 7648 | // be modified. |
| 7649 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7650 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7651 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7652 | GURL original_url( |
| 7653 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7654 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7655 | |
| 7656 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7657 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7658 | |
| 7659 | TestDelegate d; |
| 7660 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7661 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7662 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7663 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7664 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7665 | base::RunLoop().Run(); |
| 7666 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7667 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7668 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7669 | EXPECT_EQ(original_url, r->original_url()); |
| 7670 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7671 | } |
| 7672 | } |
| 7673 | |
| 7674 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7675 | // the reference fragment of the target URL must not be modified. |
| 7676 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7677 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7678 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7679 | GURL original_url( |
| 7680 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7681 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7682 | |
| 7683 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7684 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7685 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7686 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7687 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7688 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7689 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7690 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7691 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7692 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7693 | base::RunLoop().Run(); |
| 7694 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7695 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7696 | EXPECT_EQ(original_url, r->original_url()); |
| 7697 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7698 | } |
| 7699 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7700 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7701 | ASSERT_TRUE(http_test_server()->Start()); |
| 7702 | |
| 7703 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7704 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7705 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7706 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7707 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7708 | req->SetReferrer(referrer); |
| 7709 | req->Start(); |
| 7710 | base::RunLoop().Run(); |
| 7711 | |
| 7712 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7713 | } |
| 7714 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7715 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7716 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7717 | |
| 7718 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7719 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7720 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7721 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7722 | req->SetReferrer("http://user:[email protected]/"); |
| 7723 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7724 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7725 | |
| 7726 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7727 | } |
| 7728 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7729 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7730 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7731 | |
| 7732 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7733 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7734 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7735 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7736 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7737 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7738 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7739 | |
| 7740 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7741 | } |
| 7742 | |
| 7743 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7744 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7745 | |
| 7746 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7747 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7748 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7749 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7750 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7751 | req->SetReferrer(""); |
| 7752 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7753 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7754 | |
| 7755 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7756 | } |
| 7757 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7758 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7759 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7760 | |
| 7761 | TestDelegate d; |
| 7762 | { |
| 7763 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7764 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7765 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7766 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7767 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7768 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7769 | |
| 7770 | EXPECT_EQ(1, d.response_started_count()); |
| 7771 | EXPECT_EQ(0, d.bytes_received()); |
| 7772 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7773 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7774 | } |
| 7775 | } |
| 7776 | |
| 7777 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7778 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7779 | |
| 7780 | TestDelegate d; |
| 7781 | { |
| 7782 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7783 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7784 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7785 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7786 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7787 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7788 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7789 | |
| 7790 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 7791 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7792 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7793 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7794 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7795 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7796 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7797 | |
| 7798 | EXPECT_EQ(1, d.response_started_count()); |
| 7799 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7800 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7801 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7802 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7803 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7804 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7805 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7806 | |
| 7807 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7808 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7809 | EXPECT_EQ(contents, d.data_received()); |
| 7810 | } |
| 7811 | } |
| 7812 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7813 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7814 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7815 | |
| 7816 | TestDelegate d; |
| 7817 | { |
| 7818 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7819 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7820 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7821 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7822 | |
| 7823 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7824 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7825 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7826 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7827 | |
| 7828 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7829 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7830 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7831 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7832 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7833 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7834 | EXPECT_EQ(1, d.response_started_count()); |
| 7835 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7836 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7837 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7838 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7839 | |
| 7840 | base::FilePath path; |
| 7841 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7842 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7843 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7844 | |
| 7845 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7846 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7847 | EXPECT_EQ(contents, d.data_received()); |
| 7848 | } |
| 7849 | } |
| 7850 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7851 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7852 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7853 | |
| 7854 | TestDelegate d; |
| 7855 | { |
| 7856 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7857 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7858 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7859 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7860 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7861 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7862 | |
| 7863 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7864 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7865 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7866 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7867 | |
| 7868 | EXPECT_EQ(1, d.response_started_count()); |
| 7869 | EXPECT_EQ(0, d.bytes_received()); |
| 7870 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7871 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7872 | } |
| 7873 | } |
| 7874 | |
| 7875 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7876 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7877 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7878 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7879 | { |
| 7880 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7881 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7882 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7883 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7884 | HttpRequestHeaders headers; |
| 7885 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7886 | req->SetExtraRequestHeaders(headers); |
| 7887 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7888 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7889 | |
| 7890 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7891 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7892 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7893 | } |
| 7894 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7895 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7896 | { |
| 7897 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7898 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7899 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7900 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7901 | HttpRequestHeaders headers; |
| 7902 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7903 | req->SetExtraRequestHeaders(headers); |
| 7904 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7905 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7906 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7907 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7908 | |
| 7909 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7910 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7911 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7912 | } |
| 7913 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7914 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7915 | { |
| 7916 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7917 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7918 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7919 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7920 | HttpRequestHeaders headers; |
| 7921 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7922 | req->SetExtraRequestHeaders(headers); |
| 7923 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7924 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7926 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7927 | |
| 7928 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7929 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7930 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7931 | } |
| 7932 | } |
| 7933 | |
| 7934 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7935 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7936 | |
| 7937 | // populate the cache |
| 7938 | { |
| 7939 | TestDelegate d; |
| 7940 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7941 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7942 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7943 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7944 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7945 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7946 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7947 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7948 | |
| 7949 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7950 | } |
| 7951 | |
| 7952 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7953 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7954 | // response should be fetched from the cache. |
| 7955 | { |
| 7956 | TestDelegate d; |
| 7957 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7958 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7959 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7960 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7961 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7962 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7963 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7964 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7965 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7966 | |
| 7967 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7968 | |
| 7969 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7970 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7971 | } |
| 7972 | } |
| 7973 | |
| 7974 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7975 | // http://crbug.com/6450 |
| 7976 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7977 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7978 | |
| 7979 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7980 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7981 | |
| 7982 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7983 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7984 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7985 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7986 | TestURLRequestContext context(true); |
| 7987 | context.set_network_delegate(&network_delegate); |
| 7988 | context.Init(); |
| 7989 | |
| 7990 | TestDelegate d; |
| 7991 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7992 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7993 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7994 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 7995 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7996 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7997 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7998 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7999 | |
| 8000 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8001 | |
| 8002 | // Make sure we sent the cookie in the restarted transaction. |
| 8003 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8004 | != std::string::npos); |
| 8005 | } |
| 8006 | |
| 8007 | // Same test as above, except this time the restart is initiated earlier |
| 8008 | // (without user intervention since identity is embedded in the URL). |
| 8009 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8010 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8011 | TestURLRequestContext context(true); |
| 8012 | context.set_network_delegate(&network_delegate); |
| 8013 | context.Init(); |
| 8014 | |
| 8015 | TestDelegate d; |
| 8016 | |
| 8017 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8018 | replacements.SetUsernameStr("user2"); |
| 8019 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8020 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8021 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8022 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8023 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8024 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8025 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8026 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8027 | |
| 8028 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8029 | |
| 8030 | // Make sure we sent the cookie in the restarted transaction. |
| 8031 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8032 | != std::string::npos); |
| 8033 | } |
| 8034 | } |
| 8035 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8036 | // Tests that load timing works as expected with auth and the cache. |
| 8037 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8038 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8039 | |
| 8040 | // populate the cache |
| 8041 | { |
| 8042 | TestDelegate d; |
| 8043 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8044 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8045 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8046 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8047 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8048 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8049 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8050 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8051 | |
| 8052 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8053 | |
| 8054 | LoadTimingInfo load_timing_info_before_auth; |
| 8055 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8056 | &load_timing_info_before_auth)); |
| 8057 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8058 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8059 | |
| 8060 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8061 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8062 | // The test server does not support keep alive sockets, so the second |
| 8063 | // request with auth should use a new socket. |
| 8064 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8065 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8066 | load_timing_info.socket_log_id); |
| 8067 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8068 | load_timing_info.connect_timing.connect_start); |
| 8069 | } |
| 8070 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8071 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8072 | // 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] | 8073 | // response should be fetched from the cache. |
| 8074 | { |
| 8075 | TestDelegate d; |
| 8076 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8077 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8078 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8079 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8080 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8081 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8082 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8083 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8084 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8085 | |
| 8086 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8087 | |
| 8088 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8089 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8090 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8091 | // Since there was a request that went over the wire, the load timing |
| 8092 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8093 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8094 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8095 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8096 | } |
| 8097 | } |
| 8098 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8099 | // In this test, we do a POST which the server will 302 redirect. |
| 8100 | // The subsequent transaction should use GET, and should not send the |
| 8101 | // Content-Type header. |
| 8102 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8103 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8104 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8105 | |
| 8106 | const char kData[] = "hello world"; |
| 8107 | |
| 8108 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8109 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8110 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8111 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8112 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8113 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8114 | |
| 8115 | // Set headers (some of which are specific to the POST). |
| 8116 | HttpRequestHeaders headers; |
| 8117 | headers.AddHeadersFromString( |
| 8118 | "Content-Type: multipart/form-data; " |
| 8119 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 8120 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 8121 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 8122 | "Accept-Language: en-US,en\r\n" |
| 8123 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 8124 | "Content-Length: 11\r\n" |
| 8125 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8126 | req->SetExtraRequestHeaders(headers); |
| 8127 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8128 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8129 | |
| 8130 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8131 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8132 | EXPECT_EQ("text/html", mime_type); |
| 8133 | |
| 8134 | const std::string& data = d.data_received(); |
| 8135 | |
| 8136 | // Check that the post-specific headers were stripped: |
| 8137 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8138 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8139 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8140 | |
| 8141 | // These extra request headers should not have been stripped. |
| 8142 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8143 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8144 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8145 | } |
| 8146 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8147 | // The following tests check that we handle mutating the request for HTTP |
| 8148 | // redirects as expected. |
| 8149 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8150 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8151 | |
| 8152 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8153 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8154 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8155 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8156 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8157 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8158 | |
| 8159 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8160 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8161 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8162 | |
| 8163 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8164 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8165 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8166 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8167 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8168 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8169 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8170 | } |
| 8171 | |
| 8172 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8173 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8174 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8175 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8176 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8177 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8178 | |
| 8179 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8180 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8181 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8182 | |
| 8183 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8184 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8185 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8186 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8187 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8188 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8189 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8190 | } |
| 8191 | |
| 8192 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8193 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8194 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8195 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8196 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8197 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8198 | |
| 8199 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8200 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8201 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8202 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8203 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8204 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8205 | std::string()); |
| 8206 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8207 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8208 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8209 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8210 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8211 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8212 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8213 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8214 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8215 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8216 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8217 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8218 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8219 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8220 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8224 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8225 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8226 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8227 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8228 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8229 | |
| 8230 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8231 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8232 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8233 | |
| 8234 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8235 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8236 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8237 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8238 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8239 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8240 | } |
| 8241 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8242 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8243 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8244 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8245 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8246 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8247 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8248 | |
| 8249 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8250 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8251 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8252 | |
| 8253 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8254 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8255 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8256 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8257 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8258 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8259 | } |
| 8260 | |
| 8261 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8262 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8263 | // (Like Google Drive). |
| 8264 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8265 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8266 | |
| 8267 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8268 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8269 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8270 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
| 8271 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8272 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8273 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8274 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8275 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8276 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8277 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8278 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8279 | } |
| 8280 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8281 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8282 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8283 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8284 | GURL original_url( |
| 8285 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8286 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8287 | |
| 8288 | TestDelegate d; |
| 8289 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8290 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8291 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8292 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8293 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8294 | base::RunLoop().Run(); |
| 8295 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8296 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8297 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8298 | EXPECT_EQ(original_url, r->original_url()); |
| 8299 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8300 | } |
| 8301 | } |
| 8302 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8303 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8304 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8305 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8306 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8307 | GURL first_party_url("http://example.com"); |
| 8308 | |
| 8309 | TestDelegate d; |
| 8310 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8311 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8312 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8313 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8314 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8315 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8316 | base::RunLoop().Run(); |
| 8317 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8318 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8319 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8320 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8321 | } |
| 8322 | } |
| 8323 | |
| 8324 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8325 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8326 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8327 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8328 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8329 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8330 | |
| 8331 | TestDelegate d; |
| 8332 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8333 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8334 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8335 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8336 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8337 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8338 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8339 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8340 | base::RunLoop().Run(); |
| 8341 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8342 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8343 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8344 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8345 | } |
| 8346 | } |
| 8347 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8348 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8349 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8350 | |
| 8351 | const char kData[] = "hello world"; |
| 8352 | |
| 8353 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8354 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8355 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8356 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8357 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8358 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8359 | HttpRequestHeaders headers; |
| 8360 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8361 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8362 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8363 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8364 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8365 | req.get(), &default_network_delegate_, |
| 8366 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8367 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8368 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8369 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8370 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8371 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8372 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8373 | } |
| 8374 | |
| 8375 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8376 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8377 | |
| 8378 | const char kData[] = "hello world"; |
| 8379 | |
| 8380 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8381 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8382 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8383 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8384 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8385 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8386 | HttpRequestHeaders headers; |
| 8387 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8388 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8389 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8390 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8391 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8392 | req.get(), &default_network_delegate_, |
| 8393 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8394 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8395 | "Very Good Reason")); |
| 8396 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8397 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8398 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8399 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8400 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8401 | EXPECT_EQ(kData, d.data_received()); |
| 8402 | } |
| 8403 | |
| 8404 | // Check that default A-L header is sent. |
| 8405 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8406 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8407 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8408 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8409 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8410 | TestURLRequestContext context(true); |
| 8411 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8412 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8413 | context.Init(); |
| 8414 | |
| 8415 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8416 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8417 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8418 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8419 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8420 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8421 | EXPECT_EQ("en", d.data_received()); |
| 8422 | } |
| 8423 | |
| 8424 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8425 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8426 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8427 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8428 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8429 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8430 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8431 | TestURLRequestContext context(true); |
| 8432 | context.set_network_delegate(&network_delegate); |
| 8433 | context.Init(); |
| 8434 | // We override the language after initialization because empty entries |
| 8435 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8436 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8437 | |
| 8438 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8439 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8440 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8441 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8442 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8443 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8444 | EXPECT_EQ("None", d.data_received()); |
| 8445 | } |
| 8446 | |
| 8447 | // Check that if request overrides the A-L header, the default is not appended. |
| 8448 | // See http://crbug.com/20894 |
| 8449 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8450 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8451 | |
| 8452 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8453 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8454 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8455 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8456 | HttpRequestHeaders headers; |
| 8457 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8458 | req->SetExtraRequestHeaders(headers); |
| 8459 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8460 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8461 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8462 | } |
| 8463 | |
| 8464 | // Check that default A-E header is sent. |
| 8465 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8466 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8467 | |
| 8468 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8469 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8470 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8471 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8472 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8473 | req->SetExtraRequestHeaders(headers); |
| 8474 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8475 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8476 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8477 | } |
| 8478 | |
| 8479 | // Check that if request overrides the A-E header, the default is not appended. |
| 8480 | // See http://crbug.com/47381 |
| 8481 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8482 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8483 | |
| 8484 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8485 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8486 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8487 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8488 | HttpRequestHeaders headers; |
| 8489 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8490 | req->SetExtraRequestHeaders(headers); |
| 8491 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8492 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8493 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8494 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8495 | } |
| 8496 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8497 | // Check that setting the A-C header sends the proper header. |
| 8498 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8499 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8500 | |
| 8501 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8502 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8503 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8504 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8505 | HttpRequestHeaders headers; |
| 8506 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8507 | req->SetExtraRequestHeaders(headers); |
| 8508 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8509 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8510 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8511 | } |
| 8512 | |
| 8513 | // Check that default User-Agent header is sent. |
| 8514 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8515 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8516 | |
| 8517 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8518 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8519 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8520 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8521 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8522 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8523 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8524 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | // Check that if request overrides the User-Agent header, |
| 8528 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8529 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8530 | #if defined(OS_IOS) |
| 8531 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8532 | #else |
| 8533 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8534 | #endif |
| 8535 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8536 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8537 | |
| 8538 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8539 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8540 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8541 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8542 | HttpRequestHeaders headers; |
| 8543 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8544 | req->SetExtraRequestHeaders(headers); |
| 8545 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8546 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8547 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8548 | } |
| 8549 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8550 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8551 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8552 | // Accept-Charset headers. |
| 8553 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8554 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8555 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8556 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8557 | TestURLRequestContext context(true); |
| 8558 | context.set_network_delegate(&network_delegate); |
| 8559 | context.Init(); |
| 8560 | // We override the HttpUserAgentSettings after initialization because empty |
| 8561 | // entries get overridden by Init(). |
| 8562 | context.set_http_user_agent_settings(NULL); |
| 8563 | |
| 8564 | struct { |
| 8565 | const char* request; |
| 8566 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8567 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8568 | {"/echoheader?Accept-Charset", "None"}, |
| 8569 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8570 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 8571 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8572 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8573 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8574 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8575 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8576 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8577 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8578 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8579 | << " Request = \"" << tests[i].request << "\""; |
| 8580 | } |
| 8581 | } |
| 8582 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8583 | // Make sure that URLRequest passes on its priority updates to |
| 8584 | // newly-created jobs after the first one. |
| 8585 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8586 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8587 | |
| 8588 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8589 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8590 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8591 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8592 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8593 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8594 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8595 | req.get(), &default_network_delegate_, |
| 8596 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8597 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8598 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8599 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8600 | req->SetPriority(LOW); |
| 8601 | req->Start(); |
| 8602 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8603 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8604 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8605 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8606 | req.get(), &default_network_delegate_, &job_priority)); |
| 8607 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8608 | |
| 8609 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8610 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8611 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8612 | } |
| 8613 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8614 | // Check that creating a network request while entering/exiting suspend mode |
| 8615 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8616 | // does not return an HttpTransaction. |
| 8617 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 8618 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8619 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8620 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8621 | network_layer->OnSuspend(); |
| 8622 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8623 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 8624 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 8625 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8626 | |
| 8627 | TestURLRequestContext context(true); |
| 8628 | context.set_http_transaction_factory(&http_cache); |
| 8629 | context.Init(); |
| 8630 | |
| 8631 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8632 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8633 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8634 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8635 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8636 | base::RunLoop().Run(); |
| 8637 | |
| 8638 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8639 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8640 | } |
| 8641 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8642 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8643 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8644 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 8645 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 8646 | public: |
| 8647 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 8648 | : network_session_(network_session) {} |
| 8649 | |
| 8650 | ~FailingHttpTransactionFactory() override {} |
| 8651 | |
| 8652 | // HttpTransactionFactory methods: |
| 8653 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8654 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8655 | return ERR_FAILED; |
| 8656 | } |
| 8657 | |
| 8658 | HttpCache* GetCache() override { return nullptr; } |
| 8659 | |
| 8660 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 8661 | |
| 8662 | private: |
| 8663 | HttpNetworkSession* network_session_; |
| 8664 | |
| 8665 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 8666 | }; |
| 8667 | |
| 8668 | } // namespace |
| 8669 | |
| 8670 | // Check that when a request that fails to create an HttpTransaction can be |
| 8671 | // cancelled while the failure notification is pending, and doesn't send two |
| 8672 | // failure notifications. |
| 8673 | // |
| 8674 | // This currently only happens when in suspend mode and there's no cache, but |
| 8675 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 8676 | // behaviors. |
| 8677 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 8678 | FailingHttpTransactionFactory http_transaction_factory( |
| 8679 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8680 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8681 | context.set_http_transaction_factory(&http_transaction_factory); |
| 8682 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8683 | context.Init(); |
| 8684 | |
| 8685 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8686 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8687 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8688 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8689 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 8690 | // try to create an HttpNetworkTransaction synchronously on start). |
| 8691 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8692 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8693 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8694 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8695 | // Run pending error task, if there is one. |
| 8696 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8697 | |
| 8698 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8699 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8700 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8701 | |
| 8702 | // NetworkDelegate should see the cancellation, but not the error. |
| 8703 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 8704 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8705 | } |
| 8706 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8707 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8708 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8709 | |
| 8710 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8711 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8712 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8713 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8714 | |
| 8715 | req->Start(); |
| 8716 | base::RunLoop().Run(); |
| 8717 | |
| 8718 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8719 | } |
| 8720 | |
| 8721 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8722 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8723 | |
| 8724 | // Populate the cache. |
| 8725 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8726 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8727 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8728 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8729 | req->Start(); |
| 8730 | base::RunLoop().Run(); |
| 8731 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8732 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8733 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8734 | EXPECT_FALSE(req->response_info().was_cached); |
| 8735 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8736 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8737 | DEFAULT_PRIORITY, &d, |
| 8738 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8739 | req->Start(); |
| 8740 | base::RunLoop().Run(); |
| 8741 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8742 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8743 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8744 | EXPECT_TRUE(req->response_info().was_cached); |
| 8745 | } |
| 8746 | |
| 8747 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8748 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8749 | |
| 8750 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8751 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8752 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8753 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 8754 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8755 | |
| 8756 | req->Start(); |
| 8757 | base::RunLoop().Run(); |
| 8758 | |
| 8759 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8760 | } |
| 8761 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8762 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8763 | // correctly in the absence of contention. |
| 8764 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 8765 | ASSERT_TRUE(http_test_server()->Start()); |
| 8766 | |
| 8767 | TestDelegate d; |
| 8768 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8769 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8770 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8771 | req->Start(); |
| 8772 | base::RunLoop().Run(); |
| 8773 | |
| 8774 | EXPECT_TRUE(req->status().is_success()); |
| 8775 | } |
| 8776 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8777 | // A class to hold state for responding to USER_NOTIFY callbacks from |
| 8778 | // BlockingNetworkDelegate. It also accepts a RunLoop that will be |
| 8779 | // signaled via QuitWhenIdle() when any request is blocked. |
| 8780 | // |
| 8781 | class NotificationCallbackHandler { |
| 8782 | public: |
| 8783 | // Default constructed object doesn't block anything. |
| 8784 | NotificationCallbackHandler() : run_loop_(nullptr) {} |
| 8785 | |
| 8786 | void AddURLRequestToBlockList(const URLRequest* request) { |
| 8787 | requests_to_block_.insert(request); |
| 8788 | } |
| 8789 | |
| 8790 | Error ShouldBlockRequest(const CompletionCallback& callback, |
| 8791 | const URLRequest* request) { |
| 8792 | if (requests_to_block_.find(request) == requests_to_block_.end()) { |
| 8793 | return OK; |
| 8794 | } |
| 8795 | |
| 8796 | DCHECK(blocked_callbacks_.find(request) == blocked_callbacks_.end()); |
| 8797 | blocked_callbacks_[request] = callback; |
| 8798 | if (run_loop_ && blocked_callbacks_.size() == requests_to_block_.size()) |
| 8799 | run_loop_->QuitWhenIdle(); |
| 8800 | return ERR_IO_PENDING; |
| 8801 | } |
| 8802 | |
| 8803 | // Erases object's memory of blocked callbacks as a side effect. |
| 8804 | void GetBlockedCallbacks( |
| 8805 | std::map<const URLRequest*, CompletionCallback>* blocked_callbacks) { |
| 8806 | blocked_callbacks_.swap(*blocked_callbacks); |
| 8807 | } |
| 8808 | |
| 8809 | // Set a RunLoop that, if non-null, will be signaled if any request |
| 8810 | // is blocked. It is the callers responsibility to make sure the |
| 8811 | // passed object lives past the destruction of this class or |
| 8812 | // next call to SetRunLoop(). |
| 8813 | void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 8814 | |
| 8815 | private: |
| 8816 | std::set<const URLRequest*> requests_to_block_; |
| 8817 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks_; |
| 8818 | |
| 8819 | base::RunLoop* run_loop_; |
| 8820 | |
| 8821 | DISALLOW_COPY_AND_ASSIGN(NotificationCallbackHandler); |
| 8822 | }; |
| 8823 | |
| 8824 | TEST_F(URLRequestTestHTTP, MultiThrottledPriority) { |
| 8825 | ASSERT_TRUE(http_test_server()->Start()); |
| 8826 | |
| 8827 | base::RunLoop run_until_request_blocked; |
| 8828 | |
| 8829 | NotificationCallbackHandler notification_handler; |
| 8830 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8831 | BlockingNetworkDelegate network_delegate( |
| 8832 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8833 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8834 | network_delegate.set_notification_callback( |
| 8835 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8836 | // Both objects are owned by this function, and |
| 8837 | // |*network_delegate| will be destroyed first, so |
| 8838 | // it's safe to pass it an unretained pointer. |
| 8839 | base::Unretained(¬ification_handler))); |
| 8840 | |
| 8841 | TestURLRequestContext context(true); |
| 8842 | context.set_network_delegate(&network_delegate); |
| 8843 | context.Init(); |
| 8844 | |
| 8845 | // Use different test URLs to make sure all three requests turn into |
| 8846 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8847 | // the requests may be waited on separately. |
| 8848 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8849 | std::unique_ptr<URLRequest> req1( |
| 8850 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8851 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8852 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8853 | |
| 8854 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8855 | std::unique_ptr<URLRequest> req2( |
| 8856 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8857 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8858 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8859 | |
| 8860 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8861 | std::unique_ptr<URLRequest> req3( |
| 8862 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8863 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8864 | req1->Start(); |
| 8865 | req2->Start(); |
| 8866 | req3->Start(); |
| 8867 | run_until_request_blocked.Run(); |
| 8868 | notification_handler.SetRunLoop(nullptr); |
| 8869 | |
| 8870 | // The first two requests should be blocked based on the notification |
| 8871 | // callback, and their status should have blocked the third request |
| 8872 | // through throttling. |
| 8873 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8874 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8875 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8876 | |
| 8877 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8878 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8879 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8880 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8881 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8882 | |
| 8883 | // Unblocking one of the requests blocked on the notification callback |
| 8884 | // should let it complete, which should then let the third request |
| 8885 | // complete. Unblock the second request, then wait for the third |
| 8886 | // request to complete. |
| 8887 | // TODO(rdsmith): Find something to wait on other than the third |
| 8888 | // requests completion; if there's a bug in throttling, that will |
| 8889 | // result in this test hanging rather than failing quickly. |
| 8890 | d1.set_quit_on_complete(false); |
| 8891 | d2.set_quit_on_complete(false); |
| 8892 | d3.set_quit_on_complete(true); |
| 8893 | blocked_callbacks[req2.get()].Run(OK); |
| 8894 | base::RunLoop().Run(); |
| 8895 | |
| 8896 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8897 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8898 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8899 | // req3 is only unblocked after req2 completes, so req2's |
| 8900 | // success is guaranteed at this point in the function. |
| 8901 | EXPECT_EQ(URLRequestStatus::SUCCESS, req2->status().status()); |
| 8902 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8903 | } |
| 8904 | |
| 8905 | // Confirm that failing a request unblocks following requests. |
| 8906 | TEST_F(URLRequestTestHTTP, ThrottledFailure) { |
| 8907 | ASSERT_TRUE(http_test_server()->Start()); |
| 8908 | |
| 8909 | base::RunLoop run_until_request_blocked; |
| 8910 | |
| 8911 | NotificationCallbackHandler notification_handler; |
| 8912 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8913 | BlockingNetworkDelegate network_delegate( |
| 8914 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8915 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8916 | network_delegate.set_notification_callback( |
| 8917 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8918 | // Both objects are owned by this function, and |
| 8919 | // |*network_delegate| will be destroyed first, so |
| 8920 | // it's safe to pass it an unretained pointer. |
| 8921 | base::Unretained(¬ification_handler))); |
| 8922 | |
| 8923 | TestURLRequestContext context(true); |
| 8924 | context.set_network_delegate(&network_delegate); |
| 8925 | context.Init(); |
| 8926 | |
| 8927 | // Use different test URLs to make sure all three requests turn into |
| 8928 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8929 | // the requests may be waited on separately. |
| 8930 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8931 | std::unique_ptr<URLRequest> req1( |
| 8932 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8933 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8934 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8935 | |
| 8936 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8937 | std::unique_ptr<URLRequest> req2( |
| 8938 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8939 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8940 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8941 | |
| 8942 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8943 | std::unique_ptr<URLRequest> req3( |
| 8944 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8945 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8946 | req1->Start(); |
| 8947 | req2->Start(); |
| 8948 | req3->Start(); |
| 8949 | run_until_request_blocked.Run(); |
| 8950 | notification_handler.SetRunLoop(nullptr); |
| 8951 | |
| 8952 | // The first two requests should be blocked based on the notification |
| 8953 | // callback, and their status should have blocked the third request |
| 8954 | // through throttling. |
| 8955 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8956 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8957 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8958 | |
| 8959 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8960 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8961 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8962 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8963 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8964 | |
| 8965 | // Confirm canceling one of the outstanding requests allows the |
| 8966 | // blocked request to complete. |
| 8967 | |
| 8968 | // TODO(rdsmith): Find something to wait on other than the third |
| 8969 | // requests completion; if there's a bug in throttling, that will |
| 8970 | // result in this test hanging rather than failing quickly. |
| 8971 | d1.set_quit_on_complete(false); |
| 8972 | d2.set_quit_on_complete(false); |
| 8973 | d3.set_quit_on_complete(true); |
| 8974 | req2->Cancel(); |
| 8975 | base::RunLoop().Run(); |
| 8976 | |
| 8977 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8978 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8979 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8980 | EXPECT_EQ(URLRequestStatus::CANCELED, req2->status().status()); |
| 8981 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8982 | } |
| 8983 | |
| 8984 | TEST_F(URLRequestTestHTTP, ThrottledRepriUnblock) { |
| 8985 | ASSERT_TRUE(http_test_server()->Start()); |
| 8986 | |
| 8987 | base::RunLoop run_until_request_blocked; |
| 8988 | |
| 8989 | NotificationCallbackHandler notification_handler; |
| 8990 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8991 | BlockingNetworkDelegate network_delegate( |
| 8992 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8993 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8994 | network_delegate.set_notification_callback( |
| 8995 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8996 | // Both objects are owned by this function, and |
| 8997 | // |*network_delegate| will be destroyed first, so |
| 8998 | // it's safe to pass it an unretained pointer. |
| 8999 | base::Unretained(¬ification_handler))); |
| 9000 | |
| 9001 | TestURLRequestContext context(true); |
| 9002 | context.set_network_delegate(&network_delegate); |
| 9003 | context.Init(); |
| 9004 | |
| 9005 | // Use different test URLs to make sure all three requests turn into |
| 9006 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 9007 | // the requests may be waited on separately. |
| 9008 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9009 | std::unique_ptr<URLRequest> req1( |
| 9010 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 9011 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9012 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 9013 | |
| 9014 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9015 | std::unique_ptr<URLRequest> req2( |
| 9016 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 9017 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9018 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 9019 | |
| 9020 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9021 | std::unique_ptr<URLRequest> req3( |
| 9022 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 9023 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9024 | req1->Start(); |
| 9025 | req2->Start(); |
| 9026 | req3->Start(); |
| 9027 | run_until_request_blocked.Run(); |
| 9028 | notification_handler.SetRunLoop(nullptr); |
| 9029 | |
| 9030 | // The first two requests should be blocked based on the notification |
| 9031 | // callback, and their status should have blocked the third request |
| 9032 | // through throttling. |
| 9033 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 9034 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 9035 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 9036 | |
| 9037 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 9038 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 9039 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 9040 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 9041 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 9042 | |
| 9043 | // Confirm raising the priority of the third request allows it to complete. |
| 9044 | |
| 9045 | // TODO(rdsmith): Find something to wait on other than the third |
| 9046 | // requests completion; if there's a bug in throttling, that will |
| 9047 | // result in this test hanging rather than failing quickly. |
| 9048 | d1.set_quit_on_complete(false); |
| 9049 | d2.set_quit_on_complete(false); |
| 9050 | d3.set_quit_on_complete(true); |
| 9051 | req3->SetPriority(IDLE); |
| 9052 | base::RunLoop().Run(); |
| 9053 | |
| 9054 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 9055 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 9056 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 9057 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 9058 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 9059 | } |
| 9060 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9061 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9062 | ASSERT_TRUE(http_test_server()->Start()); |
| 9063 | |
| 9064 | TestDelegate d; |
| 9065 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9066 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9067 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9068 | req->Start(); |
| 9069 | base::RunLoop().Run(); |
| 9070 | |
| 9071 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9072 | } |
| 9073 | |
| 9074 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9075 | ASSERT_TRUE(http_test_server()->Start()); |
| 9076 | |
| 9077 | TestDelegate d; |
| 9078 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9079 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9080 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9081 | req->Start(); |
| 9082 | base::RunLoop().Run(); |
| 9083 | |
| 9084 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9085 | } |
| 9086 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9087 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9088 | // the delegate isn't called back synchronously. |
| 9089 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9090 | ASSERT_TRUE(http_test_server()->Start()); |
| 9091 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9092 | |
| 9093 | TestDelegate d; |
| 9094 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9095 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9096 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9097 | req->Start(); |
| 9098 | DCHECK(!d.response_completed()); |
| 9099 | base::RunLoop().Run(); |
| 9100 | DCHECK(d.response_completed()); |
| 9101 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9102 | } |
| 9103 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9104 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9105 | public: |
| 9106 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9107 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9108 | // tests into the factory tests. |
| 9109 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 9110 | } |
| 9111 | |
| 9112 | void SetUpFactory() override { |
| 9113 | interceptor_ = new MockURLRequestInterceptor(); |
| 9114 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9115 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9116 | } |
| 9117 | |
| 9118 | MockURLRequestInterceptor* interceptor() const { |
| 9119 | return interceptor_; |
| 9120 | } |
| 9121 | |
| 9122 | private: |
| 9123 | MockURLRequestInterceptor* interceptor_; |
| 9124 | }; |
| 9125 | |
| 9126 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9127 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9128 | interceptor()->set_intercept_redirect(true); |
| 9129 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9130 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9131 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9132 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9133 | |
| 9134 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9135 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9136 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9137 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9138 | req->Start(); |
| 9139 | base::RunLoop().Run(); |
| 9140 | |
| 9141 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9142 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9143 | int status = d.request_status(); |
| 9144 | EXPECT_EQ(OK, status); |
| 9145 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9146 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9147 | |
| 9148 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9149 | EXPECT_EQ(1, d.response_started_count()); |
| 9150 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9151 | |
| 9152 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9153 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9154 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9155 | } |
| 9156 | |
| 9157 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9158 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9159 | // Intercept that error and respond with an OK response. |
| 9160 | interceptor()->set_intercept_final_response(true); |
| 9161 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9162 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9163 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9164 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9165 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9166 | |
| 9167 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9168 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9169 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9170 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9171 | req->set_method("GET"); |
| 9172 | req->Start(); |
| 9173 | base::RunLoop().Run(); |
| 9174 | |
| 9175 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9176 | |
| 9177 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9178 | int status = d.request_status(); |
| 9179 | EXPECT_EQ(OK, status); |
| 9180 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9181 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9182 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9183 | EXPECT_EQ(1, d.response_started_count()); |
| 9184 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9185 | |
| 9186 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9187 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9188 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9189 | } |
| 9190 | |
| 9191 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9192 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9193 | // Intercept that error and respond with an OK response. |
| 9194 | interceptor()->set_intercept_final_response(true); |
| 9195 | |
| 9196 | // Intercept with a real URLRequestHttpJob. |
| 9197 | interceptor()->set_use_url_request_http_job(true); |
| 9198 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9199 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9200 | |
| 9201 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9202 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9203 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9204 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9205 | req->set_method("GET"); |
| 9206 | req->Start(); |
| 9207 | base::RunLoop().Run(); |
| 9208 | |
| 9209 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9210 | |
| 9211 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9212 | int status = d.request_status(); |
| 9213 | EXPECT_EQ(OK, status); |
| 9214 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9215 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9216 | EXPECT_EQ("hello", d.data_received()); |
| 9217 | EXPECT_EQ(1, d.response_started_count()); |
| 9218 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9219 | |
| 9220 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9221 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9222 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9223 | } |
| 9224 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9225 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9226 | public: |
| 9227 | URLRequestTestReferrerPolicy() {} |
| 9228 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9229 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9230 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9231 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9232 | origin_server_->AddDefaultHandlers( |
| 9233 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9234 | } else { |
| 9235 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9236 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9237 | ASSERT_TRUE(origin_server_->Start()); |
| 9238 | } |
| 9239 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9240 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9241 | net::EmbeddedTestServer::Type dest_type) { |
| 9242 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9243 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9244 | origin_server_->AddDefaultHandlers( |
| 9245 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9246 | } else { |
| 9247 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9248 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9249 | ASSERT_TRUE(origin_server_->Start()); |
| 9250 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9251 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9252 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9253 | destination_server_->AddDefaultHandlers( |
| 9254 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9255 | } else { |
| 9256 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9257 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9258 | ASSERT_TRUE(destination_server_->Start()); |
| 9259 | } |
| 9260 | |
| 9261 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9262 | const GURL& referrer, |
| 9263 | const GURL& expected) { |
| 9264 | // Create and execute the request: we'll only have a |destination_server_| |
| 9265 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9266 | // |origin_server_| for both endpoints. |
| 9267 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9268 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9269 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9270 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9271 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9272 | |
| 9273 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9274 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9275 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9276 | req->set_referrer_policy(policy); |
| 9277 | req->SetReferrer(referrer.spec()); |
| 9278 | req->Start(); |
| 9279 | base::RunLoop().Run(); |
| 9280 | |
| 9281 | EXPECT_EQ(1, d.response_started_count()); |
| 9282 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9283 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9284 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9285 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9286 | |
| 9287 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9288 | if (expected.is_empty()) |
| 9289 | EXPECT_EQ("None", d.data_received()); |
| 9290 | else |
| 9291 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9292 | } |
| 9293 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9294 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9295 | |
| 9296 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9297 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9298 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9299 | }; |
| 9300 | |
| 9301 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9302 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9303 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9304 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9305 | VerifyReferrerAfterRedirect( |
| 9306 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9307 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9308 | |
| 9309 | VerifyReferrerAfterRedirect( |
| 9310 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9311 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9312 | |
| 9313 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9314 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9315 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9316 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9317 | referrer); |
| 9318 | |
| 9319 | // The original referrer set on the request is expected to obey the referrer |
| 9320 | // policy and already be stripped to the origin; thus this test case just |
| 9321 | // checks that this policy doesn't cause the referrer to change when following |
| 9322 | // a redirect. |
| 9323 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9324 | referrer.GetOrigin()); |
| 9325 | |
| 9326 | VerifyReferrerAfterRedirect( |
| 9327 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9328 | referrer); |
| 9329 | |
| 9330 | // The original referrer set on the request is expected to obey the referrer |
| 9331 | // policy and already be stripped to the origin; thus this test case just |
| 9332 | // checks that this policy doesn't cause the referrer to change when following |
| 9333 | // a redirect. |
| 9334 | VerifyReferrerAfterRedirect( |
| 9335 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9336 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9337 | |
| 9338 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9339 | } |
| 9340 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9341 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9342 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9343 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9344 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9345 | |
| 9346 | VerifyReferrerAfterRedirect( |
| 9347 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9348 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9349 | |
| 9350 | VerifyReferrerAfterRedirect( |
| 9351 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9352 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9353 | |
| 9354 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9355 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9356 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9357 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9358 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9359 | referrer); |
| 9360 | |
| 9361 | // The original referrer set on the request is expected to obey the referrer |
| 9362 | // policy and already be stripped to the origin; thus this test case just |
| 9363 | // checks that this policy doesn't cause the referrer to change when following |
| 9364 | // a redirect. |
| 9365 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9366 | referrer.GetOrigin()); |
| 9367 | |
| 9368 | VerifyReferrerAfterRedirect( |
| 9369 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9370 | |
| 9371 | // The original referrer set on the request is expected to obey the referrer |
| 9372 | // policy and already be stripped to the origin; thus this test case just |
| 9373 | // checks that this policy doesn't cause the referrer to change when following |
| 9374 | // a redirect. |
| 9375 | VerifyReferrerAfterRedirect( |
| 9376 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9377 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9378 | |
| 9379 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9380 | } |
| 9381 | |
| 9382 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9383 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9384 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9385 | |
| 9386 | VerifyReferrerAfterRedirect( |
| 9387 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9388 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9389 | |
| 9390 | VerifyReferrerAfterRedirect( |
| 9391 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9392 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9393 | |
| 9394 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9395 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9396 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9397 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9398 | referrer); |
| 9399 | |
| 9400 | // The original referrer set on the request is expected to obey the referrer |
| 9401 | // policy and already be stripped to the origin; thus this test case just |
| 9402 | // checks that this policy doesn't cause the referrer to change when following |
| 9403 | // a redirect. |
| 9404 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9405 | referrer.GetOrigin()); |
| 9406 | |
| 9407 | VerifyReferrerAfterRedirect( |
| 9408 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9409 | referrer); |
| 9410 | |
| 9411 | // The original referrer set on the request is expected to obey the referrer |
| 9412 | // policy and already be stripped to the origin; thus this test case just |
| 9413 | // checks that this policy doesn't cause the referrer to change when following |
| 9414 | // a redirect. |
| 9415 | VerifyReferrerAfterRedirect( |
| 9416 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9417 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9418 | |
| 9419 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9420 | } |
| 9421 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9422 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9423 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9424 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9425 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9426 | |
| 9427 | VerifyReferrerAfterRedirect( |
| 9428 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9429 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9430 | |
| 9431 | VerifyReferrerAfterRedirect( |
| 9432 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9433 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9434 | |
| 9435 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9436 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9437 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9438 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9439 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9440 | referrer); |
| 9441 | |
| 9442 | // The original referrer set on the request is expected to obey the referrer |
| 9443 | // policy and already be stripped to the origin; thus this test case just |
| 9444 | // checks that this policy doesn't cause the referrer to change when following |
| 9445 | // a redirect. |
| 9446 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9447 | referrer.GetOrigin()); |
| 9448 | |
| 9449 | VerifyReferrerAfterRedirect( |
| 9450 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9451 | |
| 9452 | // The original referrer set on the request is expected to obey the referrer |
| 9453 | // policy and already be stripped to the origin; thus this test case just |
| 9454 | // checks that this policy doesn't cause the referrer to change when following |
| 9455 | // a redirect. |
| 9456 | VerifyReferrerAfterRedirect( |
| 9457 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9458 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9459 | |
| 9460 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9461 | } |
| 9462 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9463 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9464 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9465 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9466 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9467 | |
| 9468 | VerifyReferrerAfterRedirect( |
| 9469 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9470 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9471 | |
| 9472 | VerifyReferrerAfterRedirect( |
| 9473 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9474 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9475 | |
| 9476 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9477 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9478 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9479 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9480 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9481 | referrer); |
| 9482 | |
| 9483 | // The original referrer set on the request is expected to obey the referrer |
| 9484 | // policy and already be stripped to the origin; thus this test case just |
| 9485 | // checks that this policy doesn't cause the referrer to change when following |
| 9486 | // a redirect. |
| 9487 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9488 | referrer.GetOrigin()); |
| 9489 | |
| 9490 | VerifyReferrerAfterRedirect( |
| 9491 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9492 | |
| 9493 | // The original referrer set on the request is expected to obey the referrer |
| 9494 | // policy and already be stripped to the origin; thus this test case just |
| 9495 | // checks that this policy doesn't cause the referrer to change when following |
| 9496 | // a redirect. |
| 9497 | VerifyReferrerAfterRedirect( |
| 9498 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9499 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9500 | |
| 9501 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9502 | } |
| 9503 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9504 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9505 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9506 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9507 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9508 | |
| 9509 | VerifyReferrerAfterRedirect( |
| 9510 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9511 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9512 | |
| 9513 | VerifyReferrerAfterRedirect( |
| 9514 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9515 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9516 | |
| 9517 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9518 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9519 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9520 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9521 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9522 | referrer); |
| 9523 | |
| 9524 | // The original referrer set on the request is expected to obey the referrer |
| 9525 | // policy and already be stripped to the origin; thus this test case just |
| 9526 | // checks that this policy doesn't cause the referrer to change when following |
| 9527 | // a redirect. |
| 9528 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9529 | referrer.GetOrigin()); |
| 9530 | |
| 9531 | VerifyReferrerAfterRedirect( |
| 9532 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9533 | |
| 9534 | // The original referrer set on the request is expected to obey the referrer |
| 9535 | // policy and already be stripped to the origin, though it should be |
| 9536 | // subsequently cleared during the downgrading redirect. |
| 9537 | VerifyReferrerAfterRedirect( |
| 9538 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9539 | referrer.GetOrigin(), GURL()); |
| 9540 | |
| 9541 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9542 | } |
| 9543 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9544 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9545 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9546 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9547 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9548 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9549 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9550 | ~HTTPSRequestTest() override { |
| 9551 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9552 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9553 | |
| 9554 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9555 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9556 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9557 | }; |
| 9558 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9559 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9560 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9561 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9562 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9563 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9564 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9565 | TestDelegate d; |
| 9566 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9567 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9568 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9569 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9570 | r->Start(); |
| 9571 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9572 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9573 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9574 | |
| 9575 | EXPECT_EQ(1, d.response_started_count()); |
| 9576 | EXPECT_FALSE(d.received_data_before_response()); |
| 9577 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9578 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9579 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9580 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9581 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9582 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9583 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9584 | } |
| 9585 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9586 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9587 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9588 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9589 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9590 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9591 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9592 | |
| 9593 | bool err_allowed = true; |
| 9594 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9595 | TestDelegate d; |
| 9596 | { |
| 9597 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9598 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9599 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9600 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9601 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9602 | r->Start(); |
| 9603 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9604 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9605 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9606 | |
| 9607 | EXPECT_EQ(1, d.response_started_count()); |
| 9608 | EXPECT_FALSE(d.received_data_before_response()); |
| 9609 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9610 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9611 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9612 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9613 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9614 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9615 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9616 | } |
| 9617 | } |
| 9618 | } |
| 9619 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9620 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9621 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9622 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9623 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9624 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9625 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9626 | |
| 9627 | // Iterate from false to true, just so that we do the opposite of the |
| 9628 | // previous test in order to increase test coverage. |
| 9629 | bool err_allowed = false; |
| 9630 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9631 | TestDelegate d; |
| 9632 | { |
| 9633 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9634 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9635 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9636 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9637 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9638 | r->Start(); |
| 9639 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9640 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9641 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9642 | |
| 9643 | EXPECT_EQ(1, d.response_started_count()); |
| 9644 | EXPECT_FALSE(d.received_data_before_response()); |
| 9645 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9646 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9647 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9648 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9649 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9650 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9651 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9652 | } |
| 9653 | } |
| 9654 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9655 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9656 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9657 | // security state. (see http://crbug.com/550977). |
| 9658 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9659 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9660 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9661 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9662 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9663 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9664 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9665 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9666 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9667 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9668 | ASSERT_TRUE(test_server.Start()); |
| 9669 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9670 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9671 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9672 | // means that we have to use a MockHostResolver in order to direct |
| 9673 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9674 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9675 | |
| 9676 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9677 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9678 | TestURLRequestContext context(true); |
| 9679 | context.set_network_delegate(&network_delegate); |
| 9680 | context.set_host_resolver(&host_resolver); |
| 9681 | TransportSecurityState transport_security_state; |
| 9682 | context.set_transport_security_state(&transport_security_state); |
| 9683 | context.Init(); |
| 9684 | |
| 9685 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9686 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9687 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9688 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9689 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9690 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9691 | r->Start(); |
| 9692 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9693 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9694 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9695 | |
| 9696 | EXPECT_EQ(1, d.response_started_count()); |
| 9697 | EXPECT_FALSE(d.received_data_before_response()); |
| 9698 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9699 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9700 | } |
| 9701 | |
| 9702 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9703 | // TransportSecurityState. |
| 9704 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9705 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9706 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9707 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9708 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9709 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9710 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9711 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9712 | ASSERT_TRUE(test_server.Start()); |
| 9713 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9714 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9715 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9716 | // means that we have to use a MockHostResolver in order to direct |
| 9717 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9718 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9719 | |
| 9720 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9721 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9722 | TestURLRequestContext context(true); |
| 9723 | context.set_network_delegate(&network_delegate); |
| 9724 | context.set_host_resolver(&host_resolver); |
| 9725 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9726 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9727 | TransportSecurityState::STSState static_sts_state; |
| 9728 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9729 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9730 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9731 | context.set_transport_security_state(&transport_security_state); |
| 9732 | context.Init(); |
| 9733 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9734 | TransportSecurityState::STSState dynamic_sts_state; |
| 9735 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9736 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9737 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 9738 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9739 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9740 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9741 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9742 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9743 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9744 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9745 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9746 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9747 | r->Start(); |
| 9748 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9749 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9750 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9751 | |
| 9752 | EXPECT_EQ(1, d.response_started_count()); |
| 9753 | EXPECT_FALSE(d.received_data_before_response()); |
| 9754 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9755 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9756 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9757 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9758 | TransportSecurityState::STSState new_static_sts_state; |
| 9759 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9760 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9761 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 9762 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9763 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9764 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9765 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9766 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9767 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9768 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9769 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9770 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9771 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9772 | static_sts_state.include_subdomains); |
| 9773 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9774 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9775 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9776 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9777 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9778 | } |
| 9779 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9780 | // Make sure HSTS preserves a POST request's method and body. |
| 9781 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9782 | static const char kData[] = "hello world"; |
| 9783 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9784 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9785 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9786 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9787 | ASSERT_TRUE(test_server.Start()); |
| 9788 | |
| 9789 | |
| 9790 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9791 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9792 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9793 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9794 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9795 | |
| 9796 | // Force https for www.somewhere.com. |
| 9797 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9798 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9799 | bool include_subdomains = false; |
| 9800 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9801 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9802 | |
| 9803 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9804 | |
| 9805 | TestURLRequestContext context(true); |
| 9806 | context.set_host_resolver(&host_resolver); |
| 9807 | context.set_transport_security_state(&transport_security_state); |
| 9808 | context.set_network_delegate(&network_delegate); |
| 9809 | context.Init(); |
| 9810 | |
| 9811 | TestDelegate d; |
| 9812 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9813 | // cause a certificate error. Ignore the error. |
| 9814 | d.set_allow_certificate_errors(true); |
| 9815 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9816 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9817 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9818 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9819 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9820 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9821 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9822 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9823 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9824 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9825 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9826 | EXPECT_EQ("https", req->url().scheme()); |
| 9827 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9828 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9829 | |
| 9830 | LoadTimingInfo load_timing_info; |
| 9831 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9832 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9833 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9834 | } |
| 9835 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9836 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9837 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9838 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9839 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9840 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9841 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9842 | ASSERT_TRUE(test_server.Start()); |
| 9843 | |
| 9844 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9845 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9846 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9847 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9848 | MockHostResolver host_resolver; |
| 9849 | |
| 9850 | TransportSecurityState transport_security_state; |
| 9851 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9852 | bool include_subdomains = false; |
| 9853 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9854 | |
| 9855 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9856 | |
| 9857 | MockCertVerifier cert_verifier; |
| 9858 | cert_verifier.set_default_result(OK); |
| 9859 | |
| 9860 | TestURLRequestContext context(true); |
| 9861 | context.set_host_resolver(&host_resolver); |
| 9862 | context.set_transport_security_state(&transport_security_state); |
| 9863 | context.set_network_delegate(&network_delegate); |
| 9864 | context.set_cert_verifier(&cert_verifier); |
| 9865 | context.Init(); |
| 9866 | |
| 9867 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9868 | test_server.host_port_pair().port())); |
| 9869 | url::Replacements<char> replacements; |
| 9870 | const char kNewScheme[] = "https"; |
| 9871 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9872 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9873 | |
| 9874 | TestDelegate d; |
| 9875 | // Quit on redirect to allow response header inspection upon redirect. |
| 9876 | d.set_quit_on_redirect(true); |
| 9877 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9878 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9879 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9880 | // Set Origin header to simulate a cross-origin request. |
| 9881 | HttpRequestHeaders request_headers; |
| 9882 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9883 | req->SetExtraRequestHeaders(request_headers); |
| 9884 | |
| 9885 | req->Start(); |
| 9886 | base::RunLoop().Run(); |
| 9887 | |
| 9888 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9889 | |
| 9890 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9891 | std::string redirect_location; |
| 9892 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 9893 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9894 | |
| 9895 | std::string received_cors_header; |
| 9896 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 9897 | &received_cors_header)); |
| 9898 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9899 | } |
| 9900 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9901 | namespace { |
| 9902 | |
| 9903 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 9904 | public: |
| 9905 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 9906 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9907 | void OnCertificateRequested(URLRequest* request, |
| 9908 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9909 | on_certificate_requested_count_++; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 9910 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9911 | } |
| 9912 | int on_certificate_requested_count() { |
| 9913 | return on_certificate_requested_count_; |
| 9914 | } |
| 9915 | private: |
| 9916 | int on_certificate_requested_count_; |
| 9917 | }; |
| 9918 | |
| 9919 | } // namespace |
| 9920 | |
| 9921 | // TODO(davidben): Test the rest of the code. Specifically, |
| 9922 | // - Filtering which certificates to select. |
| 9923 | // - Sending a certificate back. |
| 9924 | // - Getting a certificate request in an SSL renegotiation sending the |
| 9925 | // HTTP request. |
| 9926 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9927 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9928 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 9929 | ssl_config.client_cert_type = |
| 9930 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9931 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 9932 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9933 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9934 | ASSERT_TRUE(test_server.Start()); |
| 9935 | |
| 9936 | SSLClientAuthTestDelegate d; |
| 9937 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9938 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9939 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9940 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9941 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9942 | r->Start(); |
| 9943 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9944 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9945 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9946 | |
| 9947 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9948 | EXPECT_FALSE(d.received_data_before_response()); |
| 9949 | EXPECT_EQ(0, d.bytes_received()); |
| 9950 | |
| 9951 | // Send no certificate. |
| 9952 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 9953 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 9954 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9955 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9956 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9957 | |
| 9958 | EXPECT_EQ(1, d.response_started_count()); |
| 9959 | EXPECT_FALSE(d.received_data_before_response()); |
| 9960 | EXPECT_NE(0, d.bytes_received()); |
| 9961 | } |
| 9962 | } |
| 9963 | |
| 9964 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 9965 | // Test that we attempt a session resume when making two connections to the |
| 9966 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9967 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9968 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9969 | SpawnedTestServer test_server( |
| 9970 | SpawnedTestServer::TYPE_HTTPS, |
| 9971 | ssl_options, |
| 9972 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9973 | ASSERT_TRUE(test_server.Start()); |
| 9974 | |
| 9975 | SSLClientSocket::ClearSessionCache(); |
| 9976 | |
| 9977 | { |
| 9978 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9979 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9980 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9981 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9982 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9983 | r->Start(); |
| 9984 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9985 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9986 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9987 | |
| 9988 | EXPECT_EQ(1, d.response_started_count()); |
| 9989 | } |
| 9990 | |
| 9991 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 9992 | CloseAllConnections(); |
| 9993 | |
| 9994 | { |
| 9995 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9996 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9997 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9998 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9999 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10000 | r->Start(); |
| 10001 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10002 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10003 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10004 | |
| 10005 | // The response will look like; |
| 10006 | // insert abc |
| 10007 | // lookup abc |
| 10008 | // insert xyz |
| 10009 | // |
| 10010 | // With a newline at the end which makes the split think that there are |
| 10011 | // four lines. |
| 10012 | |
| 10013 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10014 | std::vector<std::string> lines = base::SplitString( |
| 10015 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10016 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 10017 | |
| 10018 | std::string session_id; |
| 10019 | |
| 10020 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10021 | std::vector<std::string> parts = base::SplitString( |
| 10022 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10023 | ASSERT_EQ(2u, parts.size()); |
| 10024 | if (i == 0) { |
| 10025 | EXPECT_EQ("insert", parts[0]); |
| 10026 | session_id = parts[1]; |
| 10027 | } else { |
| 10028 | EXPECT_EQ("lookup", parts[0]); |
| 10029 | EXPECT_EQ(session_id, parts[1]); |
| 10030 | } |
| 10031 | } |
| 10032 | } |
| 10033 | } |
| 10034 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10035 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 10036 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 10037 | // that exactly two different sessions were inserted, with no lookups etc. |
| 10038 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10039 | std::vector<std::string> lines = base::SplitString( |
| 10040 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10041 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 10042 | |
| 10043 | std::string session_id; |
| 10044 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10045 | std::vector<std::string> parts = base::SplitString( |
| 10046 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10047 | ASSERT_EQ(2u, parts.size()); |
| 10048 | EXPECT_EQ("insert", parts[0]); |
| 10049 | if (i == 0) { |
| 10050 | session_id = parts[1]; |
| 10051 | } else { |
| 10052 | EXPECT_NE(session_id, parts[1]); |
| 10053 | } |
| 10054 | } |
| 10055 | } |
| 10056 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10057 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10058 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10059 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10060 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10061 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10062 | SpawnedTestServer test_server( |
| 10063 | SpawnedTestServer::TYPE_HTTPS, |
| 10064 | ssl_options, |
| 10065 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10066 | ASSERT_TRUE(test_server.Start()); |
| 10067 | |
| 10068 | SSLClientSocket::ClearSessionCache(); |
| 10069 | |
| 10070 | { |
| 10071 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10072 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10073 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10074 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10075 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10076 | r->Start(); |
| 10077 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10078 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10079 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10080 | |
| 10081 | EXPECT_EQ(1, d.response_started_count()); |
| 10082 | } |
| 10083 | |
| 10084 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10085 | HttpNetworkSession::Context session_context; |
| 10086 | session_context.host_resolver = default_context_.host_resolver(); |
| 10087 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10088 | session_context.transport_security_state = |
| 10089 | default_context_.transport_security_state(); |
| 10090 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10091 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10092 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
| 10093 | session_context.proxy_service = default_context_.proxy_service(); |
| 10094 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10095 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10096 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10097 | session_context.http_server_properties = |
| 10098 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10099 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10100 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10101 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10102 | std::unique_ptr<HttpCache> cache( |
| 10103 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10104 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10105 | |
| 10106 | default_context_.set_http_transaction_factory(cache.get()); |
| 10107 | |
| 10108 | { |
| 10109 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10110 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10111 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10112 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10113 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10114 | r->Start(); |
| 10115 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10116 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10117 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10118 | |
| 10119 | // The response will look like; |
| 10120 | // insert abc |
| 10121 | // insert xyz |
| 10122 | // |
| 10123 | // With a newline at the end which makes the split think that there are |
| 10124 | // three lines. |
| 10125 | |
| 10126 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10127 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10128 | } |
| 10129 | } |
| 10130 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10131 | class HTTPSFallbackTest : public testing::Test { |
| 10132 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10133 | HTTPSFallbackTest() : context_(true) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10134 | ssl_config_service_ = new TestSSLConfigService( |
| 10135 | true /* check for EV */, false /* online revocation checking */, |
| 10136 | false /* require rev. checking for local anchors */, |
| 10137 | false /* token binding enabled */); |
| 10138 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10139 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10140 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10141 | |
| 10142 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10143 | TestSSLConfigService* ssl_config_service() { |
| 10144 | return ssl_config_service_.get(); |
| 10145 | } |
| 10146 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10147 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10148 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10149 | context_.Init(); |
| 10150 | delegate_.set_allow_certificate_errors(true); |
| 10151 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10152 | SpawnedTestServer test_server( |
| 10153 | SpawnedTestServer::TYPE_HTTPS, |
| 10154 | ssl_options, |
| 10155 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10156 | ASSERT_TRUE(test_server.Start()); |
| 10157 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10158 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10159 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10160 | request_->Start(); |
| 10161 | |
| 10162 | base::RunLoop().Run(); |
| 10163 | } |
| 10164 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10165 | void ExpectConnection(int version) { |
| 10166 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10167 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10168 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10169 | request_->ssl_info().connection_status)); |
| 10170 | } |
| 10171 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10172 | void ExpectFailure(int error) { |
| 10173 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10174 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10175 | } |
| 10176 | |
| 10177 | private: |
| 10178 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10179 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10180 | std::unique_ptr<URLRequest> request_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10181 | scoped_refptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10182 | }; |
| 10183 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10184 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10185 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10186 | SpawnedTestServer::SSLOptions ssl_options( |
| 10187 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10188 | ssl_options.tls_intolerant = |
| 10189 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10190 | |
| 10191 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10192 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10193 | } |
| 10194 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10195 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10196 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10197 | SpawnedTestServer::SSLOptions ssl_options( |
| 10198 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10199 | ssl_options.tls_intolerant = |
| 10200 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10201 | |
| 10202 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10203 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10204 | } |
| 10205 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10206 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10207 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10208 | SpawnedTestServer::SSLOptions ssl_options( |
| 10209 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10210 | ssl_options.tls_intolerant = |
| 10211 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10212 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10213 | |
| 10214 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10215 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10216 | } |
| 10217 | |
| 10218 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10219 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10220 | SpawnedTestServer::SSLOptions ssl_options( |
| 10221 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10222 | ssl_options.tls_intolerant = |
| 10223 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10224 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10225 | |
| 10226 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10227 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10228 | } |
| 10229 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10230 | class HTTPSSessionTest : public testing::Test { |
| 10231 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10232 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10233 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10234 | |
| 10235 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10236 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10237 | default_context_.Init(); |
| 10238 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10239 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10240 | |
| 10241 | protected: |
| 10242 | MockCertVerifier cert_verifier_; |
| 10243 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10244 | TestURLRequestContext default_context_; |
| 10245 | }; |
| 10246 | |
| 10247 | // Tests that session resumption is not attempted if an invalid certificate |
| 10248 | // is presented. |
| 10249 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10250 | SpawnedTestServer::SSLOptions ssl_options; |
| 10251 | ssl_options.record_resume = true; |
| 10252 | SpawnedTestServer test_server( |
| 10253 | SpawnedTestServer::TYPE_HTTPS, |
| 10254 | ssl_options, |
| 10255 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10256 | ASSERT_TRUE(test_server.Start()); |
| 10257 | |
| 10258 | SSLClientSocket::ClearSessionCache(); |
| 10259 | |
| 10260 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10261 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10262 | { |
| 10263 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10264 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10265 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10266 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10267 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10268 | r->Start(); |
| 10269 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10270 | |
| 10271 | base::RunLoop().Run(); |
| 10272 | |
| 10273 | EXPECT_EQ(1, d.response_started_count()); |
| 10274 | } |
| 10275 | |
| 10276 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10277 | CloseAllConnections(); |
| 10278 | |
| 10279 | // Now change the certificate to be acceptable (so that the response is |
| 10280 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10281 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10282 | { |
| 10283 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10284 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10285 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10286 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10287 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10288 | r->Start(); |
| 10289 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10290 | |
| 10291 | base::RunLoop().Run(); |
| 10292 | |
| 10293 | // The response will look like; |
| 10294 | // insert abc |
| 10295 | // insert xyz |
| 10296 | // |
| 10297 | // With a newline at the end which makes the split think that there are |
| 10298 | // three lines. |
| 10299 | // |
| 10300 | // If a session was presented (eg: a bug), then the response would look |
| 10301 | // like; |
| 10302 | // insert abc |
| 10303 | // lookup abc |
| 10304 | // insert xyz |
| 10305 | |
| 10306 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10307 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10308 | } |
| 10309 | } |
| 10310 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10311 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10312 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10313 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10314 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10315 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10316 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10317 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10318 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10319 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10320 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10321 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10322 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10323 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10324 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10325 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10326 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10327 | // This is the policy OID contained in the certificates that testserver |
| 10328 | // generates. |
| 10329 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10330 | |
| 10331 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10332 | public: |
| 10333 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10334 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10335 | ev_test_policy_( |
| 10336 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10337 | kOCSPTestCertFingerprint, |
| 10338 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10339 | } |
| 10340 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10341 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10342 | context_.SetCTPolicyEnforcer( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10343 | std::make_unique<AllowAnyCertCTPolicyEnforcer>()); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10344 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10345 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10346 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10347 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10348 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10349 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10350 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10351 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10352 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10353 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10354 | #endif |
| 10355 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10356 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10357 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10358 | EnsureNSSHttpIOInit(); |
| 10359 | #endif |
| 10360 | } |
| 10361 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10362 | void DoConnectionWithDelegate( |
| 10363 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10364 | TestDelegate* delegate, |
| 10365 | SSLInfo* out_ssl_info) { |
| 10366 | // Always overwrite |out_ssl_info|. |
| 10367 | out_ssl_info->Reset(); |
| 10368 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10369 | SpawnedTestServer test_server( |
| 10370 | SpawnedTestServer::TYPE_HTTPS, |
| 10371 | ssl_options, |
| 10372 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10373 | ASSERT_TRUE(test_server.Start()); |
| 10374 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10375 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10376 | std::unique_ptr<URLRequest> r( |
| 10377 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10378 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10379 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10380 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10381 | base::RunLoop().Run(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10382 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10383 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10384 | *out_ssl_info = r->ssl_info(); |
| 10385 | } |
| 10386 | |
| 10387 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10388 | CertStatus* out_cert_status) { |
| 10389 | // Always overwrite |out_cert_status|. |
| 10390 | *out_cert_status = 0; |
| 10391 | |
| 10392 | TestDelegate d; |
| 10393 | SSLInfo ssl_info; |
| 10394 | ASSERT_NO_FATAL_FAILURE( |
| 10395 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10396 | |
| 10397 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10398 | } |
| 10399 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10400 | ~HTTPSOCSPTest() override { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10401 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10402 | ShutdownGlobalCertNetFetcher(); |
| 10403 | #endif |
| 10404 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10405 | #if defined(USE_NSS_CERTS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10406 | ShutdownNSSHttpIO(); |
| 10407 | #endif |
| 10408 | } |
| 10409 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10410 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10411 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 10412 | public: |
| 10413 | AllowAnyCertCTPolicyEnforcer() = default; |
| 10414 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 10415 | |
| 10416 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 10417 | X509Certificate* cert, |
| 10418 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10419 | const NetLogWithSource& net_log) override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10420 | return ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS; |
| 10421 | } |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10422 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10423 | // SetupContext configures the URLRequestContext that will be used for making |
| 10424 | // connetions to testserver. This can be overridden in test subclasses for |
| 10425 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10426 | virtual void SetupContext() { |
| 10427 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 10428 | true /* check for EV */, true /* online revocation checking */, |
| 10429 | false /* require rev. checking for local |
| 10430 | anchors */, |
| 10431 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10432 | } |
| 10433 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10434 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10435 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10436 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10437 | }; |
| 10438 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10439 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10440 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10441 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10442 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10443 | // TODO(eroman): Should this also be the return value for |
| 10444 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10445 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10446 | #else |
| 10447 | return 0; |
| 10448 | #endif |
| 10449 | } |
| 10450 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10451 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10452 | // operating system supports revocation checking and can distinguish between |
| 10453 | // situations where a given certificate lacks any revocation information (eg: |
| 10454 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10455 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10456 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10457 | // skipped. |
| 10458 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10459 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10460 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10461 | return true; |
| 10462 | #else |
| 10463 | return false; |
| 10464 | #endif |
| 10465 | } |
| 10466 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10467 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10468 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10469 | // several tests are effected because our testing EV certificate won't be |
| 10470 | // recognised as EV. |
| 10471 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10472 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10473 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10474 | #else |
| 10475 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10476 | #endif |
| 10477 | } |
| 10478 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10479 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10480 | // check failure as a result of checking a test EV cert, which will not |
| 10481 | // actually trigger an online revocation check on some platforms. |
| 10482 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10483 | if (SystemUsesChromiumEVMetadata()) { |
| 10484 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10485 | } else { |
| 10486 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10487 | // be enabled, and thus there will not be a revocation check to fail. |
| 10488 | return 0u; |
| 10489 | } |
| 10490 | } |
| 10491 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10492 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | 033501e5 | 2017-10-19 22:29:21 | [diff] [blame] | 10493 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10494 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
Sergey Ulanov | 033501e5 | 2017-10-19 22:29:21 | [diff] [blame] | 10495 | // TODO(crbug.com/776575): OCSP tests currently fail on Fuchsia. |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10496 | return false; |
| 10497 | #else |
| 10498 | return true; |
| 10499 | #endif |
| 10500 | } |
| 10501 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10502 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10503 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 10504 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10505 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10506 | #else |
| 10507 | return false; |
| 10508 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10509 | } |
| 10510 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10511 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10512 | if (!SystemSupportsOCSP()) { |
| 10513 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10514 | return; |
| 10515 | } |
| 10516 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10517 | SpawnedTestServer::SSLOptions ssl_options( |
| 10518 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10519 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10520 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10521 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10522 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10523 | |
| 10524 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10525 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10526 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10527 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10528 | |
| 10529 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10530 | } |
| 10531 | |
| 10532 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10533 | if (!SystemSupportsOCSP()) { |
| 10534 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10535 | return; |
| 10536 | } |
| 10537 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10538 | SpawnedTestServer::SSLOptions ssl_options( |
| 10539 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10540 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10541 | |
| 10542 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10543 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10544 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10545 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10546 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10547 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10548 | } |
| 10549 | |
| 10550 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10551 | if (!SystemSupportsOCSP()) { |
| 10552 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10553 | return; |
| 10554 | } |
| 10555 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10556 | SpawnedTestServer::SSLOptions ssl_options( |
| 10557 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10558 | ssl_options.ocsp_status = |
| 10559 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10560 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10561 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10562 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10563 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10564 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10565 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 10566 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 10567 | // doesn't consider this a candidate for soft-fail (only considers |
| 10568 | // network-level failures as skippable). |
| 10569 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10570 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10571 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10572 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10573 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10574 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10575 | |
| 10576 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10577 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10578 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10579 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10580 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10581 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10582 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10583 | LOG(WARNING) |
| 10584 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10585 | return; |
| 10586 | } |
| 10587 | |
| 10588 | SpawnedTestServer::SSLOptions ssl_options( |
| 10589 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10590 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10591 | ssl_options.staple_ocsp_response = true; |
| 10592 | ssl_options.ocsp_server_unavailable = true; |
| 10593 | |
| 10594 | CertStatus cert_status; |
| 10595 | DoConnection(ssl_options, &cert_status); |
| 10596 | |
| 10597 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10598 | |
| 10599 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10600 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10601 | |
| 10602 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10603 | } |
| 10604 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10605 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 10606 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10607 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 10608 | #else |
| 10609 | #define MAYBE_RevokedStapled RevokedStapled |
| 10610 | #endif |
| 10611 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10612 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10613 | LOG(WARNING) |
| 10614 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10615 | return; |
| 10616 | } |
| 10617 | |
| 10618 | SpawnedTestServer::SSLOptions ssl_options( |
| 10619 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10620 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10621 | ssl_options.staple_ocsp_response = true; |
| 10622 | ssl_options.ocsp_server_unavailable = true; |
| 10623 | |
| 10624 | CertStatus cert_status; |
| 10625 | DoConnection(ssl_options, &cert_status); |
| 10626 | |
| 10627 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10628 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10629 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10630 | } |
| 10631 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10632 | TEST_F(HTTPSOCSPTest, ExpectStapleReportSentOnMissing) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10633 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10634 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10635 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10636 | https_test_server.SetSSLConfig( |
| 10637 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10638 | https_test_server.ServeFilesFromSourceDirectory( |
| 10639 | base::FilePath(kTestFilePath)); |
| 10640 | ASSERT_TRUE(https_test_server.Start()); |
| 10641 | |
| 10642 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10643 | // but not provide any OCSP information. |
| 10644 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10645 | ASSERT_TRUE(cert); |
| 10646 | MockCertVerifier cert_verifier; |
| 10647 | CertVerifyResult verify_result; |
| 10648 | verify_result.verified_cert = cert; |
| 10649 | verify_result.is_issued_by_known_root = true; |
| 10650 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10651 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10652 | |
| 10653 | // Catch the Expect-Staple report. |
| 10654 | TransportSecurityState transport_security_state; |
| 10655 | MockCertificateReportSender mock_report_sender; |
| 10656 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10657 | |
| 10658 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10659 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10660 | MockHostResolver host_resolver; |
| 10661 | TestNetworkDelegate network_delegate; |
| 10662 | TestURLRequestContext context(true); |
| 10663 | context.set_host_resolver(&host_resolver); |
| 10664 | context.set_transport_security_state(&transport_security_state); |
| 10665 | context.set_network_delegate(&network_delegate); |
| 10666 | context.set_cert_verifier(&cert_verifier); |
| 10667 | context.Init(); |
| 10668 | |
| 10669 | // Now send a request to trigger the violation. |
| 10670 | TestDelegate d; |
| 10671 | GURL url = https_test_server.GetURL("/"); |
| 10672 | GURL::Replacements replace_host; |
| 10673 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10674 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10675 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10676 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10677 | violating_request->Start(); |
| 10678 | base::RunLoop().Run(); |
| 10679 | |
| 10680 | // Confirm a report was sent. |
| 10681 | EXPECT_FALSE(mock_report_sender.latest_report().empty()); |
| 10682 | EXPECT_EQ(GURL(kExpectStapleReportURI), |
| 10683 | mock_report_sender.latest_report_uri()); |
| 10684 | } |
| 10685 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10686 | // Tests that Expect-Staple reports are not sent for connections on which there |
| 10687 | // is a certificate error. |
| 10688 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnMissingWithCertError) { |
| 10689 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10690 | https_test_server.SetSSLConfig( |
| 10691 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10692 | https_test_server.ServeFilesFromSourceDirectory( |
| 10693 | base::FilePath(kTestFilePath)); |
| 10694 | ASSERT_TRUE(https_test_server.Start()); |
| 10695 | |
| 10696 | // Set up a MockCertVerifier to report an error for the certificate |
| 10697 | // and indicate that there was no stapled OCSP response. |
| 10698 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10699 | ASSERT_TRUE(cert); |
| 10700 | MockCertVerifier cert_verifier; |
| 10701 | CertVerifyResult verify_result; |
| 10702 | verify_result.cert_status = CERT_STATUS_DATE_INVALID; |
| 10703 | verify_result.verified_cert = cert; |
| 10704 | verify_result.is_issued_by_known_root = true; |
| 10705 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10706 | cert_verifier.AddResultForCert(cert.get(), verify_result, |
| 10707 | ERR_CERT_DATE_INVALID); |
| 10708 | |
| 10709 | // Set up a mock report sender so that the test can check that an |
| 10710 | // Expect-Staple report is not sent. |
| 10711 | TransportSecurityState transport_security_state; |
| 10712 | MockCertificateReportSender mock_report_sender; |
| 10713 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10714 | |
| 10715 | TestNetworkDelegate network_delegate; |
| 10716 | TestURLRequestContext context(true); |
| 10717 | |
| 10718 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10719 | MockHostResolver host_resolver; |
| 10720 | context.set_host_resolver(&host_resolver); |
| 10721 | |
| 10722 | context.set_transport_security_state(&transport_security_state); |
| 10723 | context.set_network_delegate(&network_delegate); |
| 10724 | context.set_cert_verifier(&cert_verifier); |
| 10725 | context.Init(); |
| 10726 | |
| 10727 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10728 | // |verify_result| used with the |cert_verifier| will indicate a certificate |
| 10729 | // error, an Expect-Staple report should not be sent. |
| 10730 | TestDelegate d; |
| 10731 | GURL url = https_test_server.GetURL("/"); |
| 10732 | GURL::Replacements replace_host; |
| 10733 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10734 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10735 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10736 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10737 | violating_request->Start(); |
| 10738 | base::RunLoop().Run(); |
| 10739 | |
| 10740 | // Confirm a report was not sent. |
| 10741 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10742 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10743 | } |
| 10744 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10745 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnValid) { |
| 10746 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10747 | https_test_server.SetSSLConfig( |
| 10748 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10749 | https_test_server.ServeFilesFromSourceDirectory( |
| 10750 | base::FilePath(kTestFilePath)); |
| 10751 | ASSERT_TRUE(https_test_server.Start()); |
| 10752 | |
| 10753 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10754 | // and provide GOOD revocation status. |
| 10755 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10756 | ASSERT_TRUE(cert); |
| 10757 | MockCertVerifier cert_verifier; |
| 10758 | CertVerifyResult verify_result; |
| 10759 | verify_result.verified_cert = cert; |
| 10760 | verify_result.is_issued_by_known_root = true; |
| 10761 | verify_result.ocsp_result.response_status = OCSPVerifyResult::PROVIDED; |
| 10762 | verify_result.ocsp_result.revocation_status = OCSPRevocationStatus::GOOD; |
| 10763 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10764 | |
| 10765 | // Catch the Expect-Staple report. |
| 10766 | TransportSecurityState transport_security_state; |
| 10767 | MockCertificateReportSender mock_report_sender; |
| 10768 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10769 | |
| 10770 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10771 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10772 | MockHostResolver host_resolver; |
| 10773 | TestNetworkDelegate network_delegate; |
| 10774 | TestURLRequestContext context(true); |
| 10775 | context.set_host_resolver(&host_resolver); |
| 10776 | context.set_transport_security_state(&transport_security_state); |
| 10777 | context.set_network_delegate(&network_delegate); |
| 10778 | context.set_cert_verifier(&cert_verifier); |
| 10779 | context.Init(); |
| 10780 | |
| 10781 | // This request should not not trigger an Expect-Staple violation. |
| 10782 | TestDelegate d; |
| 10783 | GURL url = https_test_server.GetURL("/"); |
| 10784 | GURL::Replacements replace_host; |
| 10785 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10786 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10787 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10788 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10789 | ok_request->Start(); |
| 10790 | base::RunLoop().Run(); |
| 10791 | |
| 10792 | // Check that no report was sent. |
| 10793 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10794 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10795 | } |
| 10796 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10797 | // Tests that an Expect-Staple report is not sent when OCSP details are not |
| 10798 | // checked on the connection. |
| 10799 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnNotChecked) { |
| 10800 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10801 | https_test_server.SetSSLConfig( |
| 10802 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10803 | https_test_server.ServeFilesFromSourceDirectory( |
| 10804 | base::FilePath(kTestFilePath)); |
| 10805 | ASSERT_TRUE(https_test_server.Start()); |
| 10806 | |
| 10807 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10808 | // and set |ocsp_result| to indicate that OCSP stapling details were not |
| 10809 | // checked on the connection. |
| 10810 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10811 | ASSERT_TRUE(cert); |
| 10812 | MockCertVerifier cert_verifier; |
| 10813 | CertVerifyResult verify_result; |
| 10814 | verify_result.verified_cert = cert; |
| 10815 | verify_result.is_issued_by_known_root = true; |
| 10816 | verify_result.ocsp_result.response_status = OCSPVerifyResult::NOT_CHECKED; |
| 10817 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10818 | |
| 10819 | // Set up a mock report sender so that the test can check that an |
| 10820 | // Expect-Staple report is not sent. |
| 10821 | TransportSecurityState transport_security_state; |
| 10822 | MockCertificateReportSender mock_report_sender; |
| 10823 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10824 | |
| 10825 | TestNetworkDelegate network_delegate; |
| 10826 | TestURLRequestContext context(true); |
| 10827 | |
| 10828 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10829 | MockHostResolver host_resolver; |
| 10830 | context.set_host_resolver(&host_resolver); |
| 10831 | |
| 10832 | context.set_transport_security_state(&transport_security_state); |
| 10833 | context.set_network_delegate(&network_delegate); |
| 10834 | context.set_cert_verifier(&cert_verifier); |
| 10835 | context.Init(); |
| 10836 | |
| 10837 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10838 | // |verify_result| used with the |cert_verifier| will indicate that OCSP |
| 10839 | // stapling details were not checked on the connection, an Expect-Staple |
| 10840 | // report should not be sent. |
| 10841 | TestDelegate d; |
| 10842 | GURL url = https_test_server.GetURL("/"); |
| 10843 | GURL::Replacements replace_host; |
| 10844 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10845 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10846 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10847 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10848 | ok_request->Start(); |
| 10849 | base::RunLoop().Run(); |
| 10850 | |
| 10851 | // Check that no report was sent. |
| 10852 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10853 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10854 | } |
| 10855 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10856 | static const struct OCSPVerifyTestData { |
| 10857 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 10858 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 10859 | OCSPVerifyResult::ResponseStatus response_status; |
| 10860 | bool has_revocation_status; |
| 10861 | OCSPRevocationStatus cert_status; |
| 10862 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10863 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10864 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10865 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10866 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10867 | OCSPVerifyResult::PROVIDED, |
| 10868 | true, |
| 10869 | OCSPRevocationStatus::GOOD}, |
| 10870 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10871 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10872 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10873 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10874 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10875 | OCSPVerifyResult::INVALID_DATE, |
| 10876 | false, |
| 10877 | OCSPRevocationStatus::UNKNOWN}, |
| 10878 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10879 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10880 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10881 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10882 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10883 | OCSPVerifyResult::INVALID_DATE, |
| 10884 | false, |
| 10885 | OCSPRevocationStatus::UNKNOWN}, |
| 10886 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10887 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10888 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10889 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10890 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10891 | OCSPVerifyResult::INVALID_DATE, |
| 10892 | false, |
| 10893 | OCSPRevocationStatus::UNKNOWN}, |
| 10894 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10895 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10896 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10897 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10898 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10899 | OCSPVerifyResult::INVALID_DATE, |
| 10900 | false, |
| 10901 | OCSPRevocationStatus::UNKNOWN}, |
| 10902 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10903 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10904 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 10905 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10906 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10907 | OCSPVerifyResult::ERROR_RESPONSE, |
| 10908 | false, |
| 10909 | OCSPRevocationStatus::UNKNOWN}, |
| 10910 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10911 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10912 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 10913 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10914 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10915 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 10916 | false, |
| 10917 | OCSPRevocationStatus::UNKNOWN}, |
| 10918 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10919 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10920 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 10921 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10922 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10923 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 10924 | false, |
| 10925 | OCSPRevocationStatus::UNKNOWN}, |
| 10926 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10927 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10928 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10929 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10930 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10931 | OCSPVerifyResult::INVALID_DATE, |
| 10932 | false, |
| 10933 | OCSPRevocationStatus::UNKNOWN}, |
| 10934 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10935 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10936 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10937 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10938 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10939 | OCSPVerifyResult::PROVIDED, |
| 10940 | true, |
| 10941 | OCSPRevocationStatus::UNKNOWN}, |
| 10942 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10943 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10944 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10945 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10946 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10947 | OCSPVerifyResult::INVALID_DATE, |
| 10948 | false, |
| 10949 | OCSPRevocationStatus::UNKNOWN}, |
| 10950 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10951 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10952 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10953 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10954 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10955 | OCSPVerifyResult::INVALID_DATE, |
| 10956 | false, |
| 10957 | OCSPRevocationStatus::UNKNOWN}, |
| 10958 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10959 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10960 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10961 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10962 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 10963 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10964 | false, |
| 10965 | OCSPRevocationStatus::UNKNOWN}, |
| 10966 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10967 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10968 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10969 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10970 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10971 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10972 | false, |
| 10973 | OCSPRevocationStatus::UNKNOWN}, |
| 10974 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10975 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10976 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10977 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10978 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10979 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10980 | false, |
| 10981 | OCSPRevocationStatus::UNKNOWN}, |
| 10982 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10983 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10984 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10985 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10986 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10987 | OCSPVerifyResult::PROVIDED, |
| 10988 | true, |
| 10989 | OCSPRevocationStatus::GOOD}, |
| 10990 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10991 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10992 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10993 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10994 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10995 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10996 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10997 | OCSPVerifyResult::PROVIDED, |
| 10998 | true, |
| 10999 | OCSPRevocationStatus::GOOD}, |
| 11000 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11001 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11002 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11003 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11004 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11005 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11006 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11007 | OCSPVerifyResult::PROVIDED, |
| 11008 | true, |
| 11009 | OCSPRevocationStatus::GOOD}, |
| 11010 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11011 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11012 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11013 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11014 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11015 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11016 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11017 | OCSPVerifyResult::PROVIDED, |
| 11018 | true, |
| 11019 | OCSPRevocationStatus::GOOD}, |
| 11020 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11021 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11022 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11023 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11024 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11025 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11026 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11027 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11028 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11029 | OCSPVerifyResult::INVALID_DATE, |
| 11030 | false, |
| 11031 | OCSPRevocationStatus::UNKNOWN}, |
| 11032 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11033 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11034 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11035 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11036 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11037 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11038 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11039 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11040 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11041 | OCSPVerifyResult::PROVIDED, |
| 11042 | true, |
| 11043 | OCSPRevocationStatus::REVOKED}, |
| 11044 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11045 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11046 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11047 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11048 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11049 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11050 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11051 | OCSPVerifyResult::PROVIDED, |
| 11052 | true, |
| 11053 | OCSPRevocationStatus::UNKNOWN}, |
| 11054 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11055 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11056 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11057 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11058 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11059 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11060 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11061 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11062 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11063 | OCSPVerifyResult::PROVIDED, |
| 11064 | true, |
| 11065 | OCSPRevocationStatus::UNKNOWN}, |
| 11066 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11067 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11068 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11069 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11070 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11071 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11072 | false, |
| 11073 | OCSPRevocationStatus::UNKNOWN}, |
| 11074 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11075 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11076 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11077 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11078 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11079 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11080 | false, |
| 11081 | OCSPRevocationStatus::UNKNOWN}, |
| 11082 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11083 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11084 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11085 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11086 | #if !defined(USE_NSS_CERTS) |
| 11087 | // 25 |
| 11088 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11089 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11090 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11091 | OCSPVerifyResult::PROVIDED, |
| 11092 | true, |
| 11093 | OCSPRevocationStatus::REVOKED}, |
| 11094 | |
| 11095 | // 26 |
| 11096 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11097 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11098 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11099 | OCSPVerifyResult::INVALID_DATE, |
| 11100 | false, |
| 11101 | OCSPRevocationStatus::UNKNOWN}, |
| 11102 | |
| 11103 | // 27 |
| 11104 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11105 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11106 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11107 | OCSPVerifyResult::INVALID_DATE, |
| 11108 | false, |
| 11109 | OCSPRevocationStatus::UNKNOWN}, |
| 11110 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11111 | }; |
| 11112 | |
| 11113 | class HTTPSOCSPVerifyTest |
| 11114 | : public HTTPSOCSPTest, |
| 11115 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11116 | |
| 11117 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 11118 | SpawnedTestServer::SSLOptions ssl_options( |
| 11119 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11120 | OCSPVerifyTestData test = GetParam(); |
| 11121 | |
| 11122 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11123 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11124 | ssl_options.staple_ocsp_response = true; |
| 11125 | |
| 11126 | SSLInfo ssl_info; |
| 11127 | OCSPErrorTestDelegate delegate; |
| 11128 | ASSERT_NO_FATAL_FAILURE( |
| 11129 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11130 | |
| 11131 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11132 | // URLRequest caches certificate errors. |
| 11133 | if (delegate.have_certificate_errors()) { |
| 11134 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11135 | ssl_info = delegate.ssl_info(); |
| 11136 | } |
| 11137 | |
| 11138 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11139 | |
| 11140 | if (test.has_revocation_status) |
| 11141 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11142 | } |
| 11143 | |
| 11144 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 11145 | HTTPSOCSPVerifyTest, |
| 11146 | testing::ValuesIn(kOCSPVerifyData)); |
| 11147 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11148 | static bool SystemSupportsAIA() { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11149 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Sergey Ulanov | 884169a0 | 2017-09-06 18:58:02 | [diff] [blame] | 11150 | // TODO(crbug.com/762380): Enable on Fuchsia once it's implemented. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11151 | return false; |
| 11152 | #else |
| 11153 | return true; |
| 11154 | #endif |
| 11155 | } |
| 11156 | |
| 11157 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11158 | public: |
| 11159 | void SetupContext() override { |
| 11160 | context_.set_ssl_config_service(new TestSSLConfigService( |
| 11161 | false /* check for EV */, false /* online revocation checking */, |
| 11162 | false /* require rev. checking for local anchors */, |
| 11163 | false /* token binding enabled */)); |
| 11164 | } |
| 11165 | }; |
| 11166 | |
| 11167 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11168 | SpawnedTestServer::SSLOptions ssl_options( |
| 11169 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11170 | SpawnedTestServer test_server( |
| 11171 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11172 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11173 | ASSERT_TRUE(test_server.Start()); |
| 11174 | |
| 11175 | TestDelegate d; |
| 11176 | d.set_allow_certificate_errors(true); |
| 11177 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11178 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11179 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11180 | |
| 11181 | r->Start(); |
| 11182 | EXPECT_TRUE(r->is_pending()); |
| 11183 | |
| 11184 | base::RunLoop().Run(); |
| 11185 | |
| 11186 | EXPECT_EQ(1, d.response_started_count()); |
| 11187 | |
| 11188 | CertStatus cert_status = r->ssl_info().cert_status; |
| 11189 | if (SystemSupportsAIA()) { |
| 11190 | EXPECT_EQ(OK, d.request_status()); |
| 11191 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11192 | ASSERT_TRUE(r->ssl_info().cert); |
| 11193 | EXPECT_EQ(2u, r->ssl_info().cert->GetIntermediateCertificates().size()); |
| 11194 | } else { |
| 11195 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, |
| 11196 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11197 | } |
| 11198 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
| 11199 | EXPECT_EQ( |
| 11200 | 0u, r->ssl_info().unverified_cert->GetIntermediateCertificates().size()); |
| 11201 | } |
| 11202 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11203 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11204 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11205 | void SetupContext() override { |
| 11206 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11207 | false /* check for EV */, false /* online revocation checking */, |
| 11208 | true /* require rev. checking for local |
| 11209 | anchors */, |
| 11210 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11211 | } |
| 11212 | }; |
| 11213 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11214 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11215 | if (!SystemSupportsOCSP()) { |
| 11216 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11217 | return; |
| 11218 | } |
| 11219 | |
| 11220 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11221 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11222 | << "revocation checking"; |
| 11223 | return; |
| 11224 | } |
| 11225 | |
| 11226 | SpawnedTestServer::SSLOptions ssl_options( |
| 11227 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11228 | ssl_options.ocsp_status = |
| 11229 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11230 | |
| 11231 | CertStatus cert_status; |
| 11232 | DoConnection(ssl_options, &cert_status); |
| 11233 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11234 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11235 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11236 | // affirmatively revoked? |
| 11237 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11238 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11239 | #else |
| 11240 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11241 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11242 | |
| 11243 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11244 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11245 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11246 | } |
| 11247 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11248 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11249 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11250 | void SetupContext() override { |
| 11251 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11252 | true /* check for EV */, false /* online revocation checking */, |
| 11253 | false /* require rev. checking for local |
| 11254 | anchors */, |
| 11255 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11256 | } |
| 11257 | }; |
| 11258 | |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11259 | // Helper class to set the global CRLSet, and on destruction restore the |
| 11260 | // previously set one. |
| 11261 | class ScopedSetCRLSet { |
| 11262 | public: |
| 11263 | ScopedSetCRLSet(scoped_refptr<CRLSet> crl_set) { |
| 11264 | prev_crl_set_ = SSLConfigService::GetCRLSet(); |
| 11265 | SSLConfigService::SetCRLSetForTesting(std::move(crl_set)); |
| 11266 | } |
| 11267 | |
| 11268 | ~ScopedSetCRLSet() { |
| 11269 | SSLConfigService::SetCRLSetForTesting(std::move(prev_crl_set_)); |
| 11270 | } |
| 11271 | |
| 11272 | private: |
| 11273 | scoped_refptr<CRLSet> prev_crl_set_; |
| 11274 | }; |
| 11275 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11276 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11277 | if (!SystemSupportsOCSP()) { |
| 11278 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11279 | return; |
| 11280 | } |
| 11281 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11282 | SpawnedTestServer::SSLOptions ssl_options( |
| 11283 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11284 | ssl_options.ocsp_status = |
| 11285 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11286 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11287 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11288 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11289 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11290 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11291 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11292 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11293 | |
| 11294 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11295 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11296 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11297 | } |
| 11298 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11299 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11300 | if (!SystemSupportsOCSP()) { |
| 11301 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11302 | return; |
| 11303 | } |
| 11304 | |
| 11305 | SpawnedTestServer::SSLOptions ssl_options( |
| 11306 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11307 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11308 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11309 | |
| 11310 | CertStatus cert_status; |
| 11311 | DoConnection(ssl_options, &cert_status); |
| 11312 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11313 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11314 | // possible to determine whether the check failed because of actual |
| 11315 | // revocation or because there was an OCSP failure. |
| 11316 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11317 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11318 | #else |
| 11319 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11320 | #endif |
| 11321 | |
| 11322 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11323 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11324 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11325 | } |
| 11326 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11327 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11328 | if (!SystemSupportsOCSP()) { |
| 11329 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11330 | return; |
| 11331 | } |
| 11332 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11333 | SpawnedTestServer::SSLOptions ssl_options( |
| 11334 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11335 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11336 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11337 | |
| 11338 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11339 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11340 | |
| 11341 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11342 | |
| 11343 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11344 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11345 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11346 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11347 | } |
| 11348 | |
| 11349 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11350 | if (!SystemSupportsOCSP()) { |
| 11351 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11352 | return; |
| 11353 | } |
| 11354 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11355 | SpawnedTestServer::SSLOptions ssl_options( |
| 11356 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11357 | ssl_options.ocsp_status = |
| 11358 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11359 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11360 | |
| 11361 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11362 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11363 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11364 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11365 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11366 | |
| 11367 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11368 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11369 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11370 | } |
| 11371 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11372 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11373 | if (!SystemSupportsOCSP()) { |
| 11374 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11375 | return; |
| 11376 | } |
| 11377 | |
| 11378 | SpawnedTestServer::SSLOptions ssl_options( |
| 11379 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11380 | ssl_options.ocsp_status = |
| 11381 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11382 | ScopedSetCRLSet set_crlset(CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "")); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11383 | |
| 11384 | CertStatus cert_status; |
| 11385 | DoConnection(ssl_options, &cert_status); |
| 11386 | |
| 11387 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11388 | // revocation check for EV. |
| 11389 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11390 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11391 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11392 | EXPECT_FALSE( |
| 11393 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11394 | } |
| 11395 | |
| 11396 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11397 | if (!SystemSupportsOCSP()) { |
| 11398 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11399 | return; |
| 11400 | } |
| 11401 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11402 | SpawnedTestServer::SSLOptions ssl_options( |
| 11403 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11404 | ssl_options.ocsp_status = |
| 11405 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11406 | ScopedSetCRLSet set_crlset(CRLSet::EmptyCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11407 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11408 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11409 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11410 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11411 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11412 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11413 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11414 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11415 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11416 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11417 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11418 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11419 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11420 | } |
| 11421 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11422 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 11423 | // Test that when EV verification is requested, but online revocation |
| 11424 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 11425 | // no revocation checking actually happens. |
| 11426 | if (!SystemSupportsOCSP()) { |
| 11427 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11428 | return; |
| 11429 | } |
| 11430 | |
| 11431 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11432 | // checking (as per the user preference) |
| 11433 | ev_test_policy_.reset(); |
| 11434 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11435 | SpawnedTestServer::SSLOptions ssl_options( |
| 11436 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11437 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11438 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11439 | |
| 11440 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11441 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11442 | |
| 11443 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11444 | |
| 11445 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11446 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11447 | } |
| 11448 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11449 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11450 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11451 | void SetupContext() override { |
| 11452 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11453 | false /* check for EV */, false /* online revocation checking */, |
| 11454 | false /* require rev. checking for local |
| 11455 | anchors */, |
| 11456 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11457 | } |
| 11458 | }; |
| 11459 | |
| 11460 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11461 | SpawnedTestServer::SSLOptions ssl_options( |
| 11462 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11463 | ssl_options.ocsp_status = |
| 11464 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11465 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11466 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11467 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11468 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11469 | |
| 11470 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11471 | // we don't fall back to online revocation checks. |
| 11472 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11473 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11474 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11475 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11476 | |
| 11477 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11478 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11479 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11480 | return; |
| 11481 | #endif |
| 11482 | |
| 11483 | SpawnedTestServer::SSLOptions ssl_options( |
| 11484 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11485 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11486 | ssl_options.cert_serial = 10; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11487 | ScopedSetCRLSet set_crlset( |
| 11488 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a")); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11489 | |
| 11490 | CertStatus cert_status = 0; |
| 11491 | DoConnection(ssl_options, &cert_status); |
| 11492 | |
| 11493 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11494 | // reflected without online revocation checking. |
| 11495 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11496 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11497 | EXPECT_FALSE( |
| 11498 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11499 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11500 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11501 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11502 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11503 | !defined(OS_FUCHSIA) |
| 11504 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11505 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11506 | // connections reliably. FTP tests are disabled on platforms that use |
| 11507 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11508 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11509 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11510 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11511 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11512 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11513 | // Can't use |default_context_|'s HostResolver to set up the |
| 11514 | // FTPTransactionFactory because it hasn't been created yet. |
| 11515 | default_context_.set_host_resolver(&host_resolver_); |
| 11516 | } |
| 11517 | |
| 11518 | // URLRequestTest interface: |
| 11519 | void SetUpFactory() override { |
| 11520 | // Add FTP support to the default URLRequestContext. |
| 11521 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11522 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11523 | } |
| 11524 | |
| 11525 | std::string GetTestFileContents() { |
| 11526 | base::FilePath path; |
| 11527 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 11528 | path = path.Append(kTestFilePath); |
| 11529 | path = path.AppendASCII(kFtpTestFile); |
| 11530 | std::string contents; |
| 11531 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11532 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11533 | } |
| 11534 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11535 | protected: |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11536 | // Note that this is destroyed before the FtpProtocolHandler that references |
| 11537 | // it, which is owned by the parent class. Since no requests are made during |
| 11538 | // teardown, this works, though it's not great. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11539 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11540 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11541 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11542 | }; |
| 11543 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11544 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11545 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11546 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11547 | |
| 11548 | TestDelegate d; |
| 11549 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11550 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11551 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11552 | r->Start(); |
| 11553 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11554 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11555 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11556 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11557 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11558 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11559 | } |
| 11560 | } |
| 11561 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11562 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11563 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11564 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11565 | TestDelegate d; |
| 11566 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11567 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11568 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11569 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11570 | r->Start(); |
| 11571 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11572 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11573 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11574 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11575 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11576 | EXPECT_EQ(1, d.response_started_count()); |
| 11577 | EXPECT_FALSE(d.received_data_before_response()); |
| 11578 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11579 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11580 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11581 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11582 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11583 | } |
| 11584 | } |
| 11585 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11586 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11587 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11588 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11589 | TestDelegate d; |
| 11590 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11591 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11592 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11593 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11594 | r->Start(); |
| 11595 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11596 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11597 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11598 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11599 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11600 | EXPECT_EQ(1, d.response_started_count()); |
| 11601 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11602 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11603 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11604 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11605 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11606 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11607 | } |
| 11608 | } |
| 11609 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11610 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11611 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11612 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11613 | TestDelegate d; |
| 11614 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11615 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11616 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11617 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11618 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11619 | r->Start(); |
| 11620 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11621 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11622 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11623 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11624 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11625 | EXPECT_EQ(1, d.response_started_count()); |
| 11626 | EXPECT_FALSE(d.received_data_before_response()); |
| 11627 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11628 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11629 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11630 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11631 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11632 | |
| 11633 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11634 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11635 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11636 | } |
| 11637 | } |
| 11638 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11639 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11640 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11641 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11642 | TestDelegate d; |
| 11643 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11644 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11645 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11646 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11647 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11648 | r->Start(); |
| 11649 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11650 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11651 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11652 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11653 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11654 | EXPECT_EQ(1, d.response_started_count()); |
| 11655 | EXPECT_FALSE(d.received_data_before_response()); |
| 11656 | EXPECT_EQ(d.bytes_received(), 0); |
| 11657 | } |
| 11658 | } |
| 11659 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11660 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11661 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11662 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11663 | TestDelegate d; |
| 11664 | // Set correct login credentials. The delegate will be asked for them when |
| 11665 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11666 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11667 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11668 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11669 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11670 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11671 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11672 | r->Start(); |
| 11673 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11674 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11675 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11676 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11677 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11678 | EXPECT_EQ(1, d.response_started_count()); |
| 11679 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11680 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11681 | } |
| 11682 | } |
| 11683 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11684 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11685 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11686 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11687 | TestDelegate d; |
| 11688 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11689 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11690 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11691 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11692 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11693 | r->Start(); |
| 11694 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11695 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11696 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11697 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11698 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11699 | EXPECT_EQ(1, d.response_started_count()); |
| 11700 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11701 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11702 | } |
| 11703 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11704 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11705 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11706 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11707 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11708 | TestDelegate d; |
| 11709 | // Set correct login credentials. The delegate will be asked for them when |
| 11710 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11711 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11712 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11713 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11714 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11715 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11716 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11717 | r->Start(); |
| 11718 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11719 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11720 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11722 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11723 | EXPECT_EQ(1, d.response_started_count()); |
| 11724 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11725 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11726 | } |
| 11727 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11728 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11729 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11730 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11731 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11732 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11733 | { |
| 11734 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11735 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11736 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11737 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11738 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11739 | r->Start(); |
| 11740 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11741 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11742 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11743 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11744 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11745 | EXPECT_EQ(1, d->response_started_count()); |
| 11746 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11747 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11748 | } |
| 11749 | |
| 11750 | d.reset(new TestDelegate); |
| 11751 | { |
| 11752 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11753 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11754 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11755 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11756 | r->Start(); |
| 11757 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11758 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11759 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11760 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11761 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11762 | EXPECT_EQ(1, d->response_started_count()); |
| 11763 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11764 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11765 | } |
| 11766 | } |
| 11767 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11768 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11769 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11770 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11771 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11772 | // Set correct login credentials. The delegate will be asked for them when |
| 11773 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11774 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11775 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11776 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11777 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11778 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11779 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11780 | r->Start(); |
| 11781 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11782 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11783 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11784 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11785 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11786 | EXPECT_EQ(1, d->response_started_count()); |
| 11787 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11788 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11789 | } |
| 11790 | |
| 11791 | // Use a new delegate without explicit credentials. The cached ones should be |
| 11792 | // used. |
| 11793 | d.reset(new TestDelegate); |
| 11794 | { |
| 11795 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 11796 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11797 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11798 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11799 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11800 | r->Start(); |
| 11801 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11802 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11803 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11804 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11805 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11806 | EXPECT_EQ(1, d->response_started_count()); |
| 11807 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11808 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11809 | } |
| 11810 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11811 | |
| 11812 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 11813 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11814 | |
| 11815 | TestDelegate d; |
| 11816 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11817 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 11818 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11819 | req->Start(); |
| 11820 | base::RunLoop().Run(); |
| 11821 | |
| 11822 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 11823 | } |
| 11824 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11825 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11826 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11827 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 11828 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11829 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 11830 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11831 | |
| 11832 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11833 | |
| 11834 | req->Start(); |
| 11835 | base::RunLoop().Run(); |
| 11836 | |
| 11837 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 11838 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11839 | } |
| 11840 | |
| 11841 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 11842 | MockHostResolver host_resolver; |
| 11843 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 11844 | TestURLRequestContext context(true); |
| 11845 | context.set_network_delegate(&network_delegate); |
| 11846 | context.set_host_resolver(&host_resolver); |
| 11847 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 11848 | context.Init(); |
| 11849 | |
| 11850 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11851 | std::unique_ptr<URLRequest> req( |
| 11852 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 11853 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11854 | |
| 11855 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11856 | |
| 11857 | req->Start(); |
| 11858 | base::RunLoop().Run(); |
| 11859 | EXPECT_TRUE(req->response_info().network_accessed); |
| 11860 | } |
| 11861 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11862 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11863 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11864 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11865 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11866 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11867 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 11868 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11869 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11870 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11871 | req.get(), &default_network_delegate_, |
| 11872 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11873 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 11874 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11875 | |
| 11876 | req->Start(); |
| 11877 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11878 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11879 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11880 | EXPECT_EQ(0, d.received_redirect_count()); |
| 11881 | } |
| 11882 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11883 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11884 | ASSERT_TRUE(http_test_server()->Start()); |
| 11885 | TestURLRequestContext context; |
| 11886 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 11887 | TestDelegate delegate; |
| 11888 | HttpRequestHeaders extra_headers; |
| 11889 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11890 | |
| 11891 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11892 | HttpRawRequestHeaders raw_req_headers; |
| 11893 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11894 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11895 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11896 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11897 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11898 | r->SetRequestHeadersCallback(base::Bind( |
| 11899 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 11900 | r->SetResponseHeadersCallback(base::Bind( |
| 11901 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11902 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11903 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11904 | r->Start(); |
| 11905 | while (!delegate.response_started_count()) |
| 11906 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11907 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11908 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11909 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11910 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11911 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11912 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11913 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 11914 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 11915 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 11916 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11917 | } |
| 11918 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11919 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11920 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11921 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11922 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11923 | FAIL() << "Callback should not be called unless request is sent"; |
| 11924 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11925 | r->SetResponseHeadersCallback( |
| 11926 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 11927 | FAIL() << "Callback should not be called unless request is sent"; |
| 11928 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11929 | r->Start(); |
| 11930 | base::RunLoop().Run(); |
| 11931 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11932 | } |
| 11933 | } |
| 11934 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11935 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11936 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11937 | HttpRawRequestHeaders raw_req_headers; |
| 11938 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11939 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11940 | TestURLRequestContext context; |
| 11941 | TestDelegate delegate; |
| 11942 | HttpRequestHeaders extra_headers; |
| 11943 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11944 | delegate.set_quit_on_redirect(true); |
| 11945 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
| 11946 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11947 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11948 | r->SetExtraRequestHeaders(extra_headers); |
| 11949 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11950 | base::Unretained(&raw_req_headers))); |
| 11951 | r->SetResponseHeadersCallback(base::Bind( |
| 11952 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11953 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11954 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11955 | r->Start(); |
| 11956 | base::RunLoop().Run(); |
| 11957 | |
| 11958 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 11959 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11960 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11961 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11962 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11963 | EXPECT_EQ("gzip, deflate", value); |
| 11964 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11965 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 11966 | raw_req_headers.request_line()); |
| 11967 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 11968 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 11969 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11970 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11971 | raw_req_headers = HttpRawRequestHeaders(); |
| 11972 | raw_resp_headers = nullptr; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11973 | r->FollowDeferredRedirect(); |
| 11974 | base::RunLoop().Run(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11975 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11976 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11977 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11978 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11979 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 11980 | raw_req_headers.request_line()); |
| 11981 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11982 | } |
| 11983 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11984 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11985 | TestDelegate request_delegate; |
| 11986 | |
| 11987 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11988 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 11989 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11990 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11991 | FAIL() << "Callback should not be called unless request is sent"; |
| 11992 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11993 | r->SetResponseHeadersCallback( |
| 11994 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 11995 | FAIL() << "Callback should not be called unless request is sent"; |
| 11996 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11997 | r->Start(); |
| 11998 | base::RunLoop().Run(); |
| 11999 | EXPECT_FALSE(r->is_pending()); |
| 12000 | } |
| 12001 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12002 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12003 | ASSERT_TRUE(http_test_server()->Start()); |
| 12004 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12005 | |
| 12006 | TestURLRequestContext context; |
| 12007 | TestDelegate delegate; |
| 12008 | |
| 12009 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12010 | HttpRequestHeaders extra_headers; |
| 12011 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12012 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12013 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12014 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12015 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12016 | using RespHeadersVector = |
| 12017 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12018 | RespHeadersVector raw_resp_headers; |
| 12019 | |
| 12020 | auto req_headers_callback = base::Bind( |
| 12021 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12022 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12023 | }, |
| 12024 | &raw_req_headers); |
| 12025 | auto resp_headers_callback = base::Bind( |
| 12026 | [](RespHeadersVector* vec, |
| 12027 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12028 | vec->push_back(headers); |
| 12029 | }, |
| 12030 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12031 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12032 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12033 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12034 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12035 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12036 | r->Start(); |
| 12037 | base::RunLoop().Run(); |
| 12038 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12039 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12040 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12041 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12042 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12043 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12044 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12045 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12046 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12047 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12048 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12049 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12050 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12051 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12052 | |
| 12053 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12054 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12055 | r2->SetExtraRequestHeaders(extra_headers); |
| 12056 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12057 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12058 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12059 | r2->Start(); |
| 12060 | base::RunLoop().Run(); |
| 12061 | EXPECT_FALSE(r2->is_pending()); |
| 12062 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12063 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12064 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12065 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12066 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12067 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12068 | } |
| 12069 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12070 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12071 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12072 | TestDelegate d; |
| 12073 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12074 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12075 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12076 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12077 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12078 | r->SetResponseHeadersCallback( |
| 12079 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12080 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12081 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12082 | r->Start(); |
| 12083 | base::RunLoop().Run(); |
| 12084 | EXPECT_FALSE(r->is_pending()); |
| 12085 | } |
| 12086 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12087 | } // namespace net |