[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 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5 | #include <memory> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 6 | #include <utility> |
| 7 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 8 | // This must be before Windows headers |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 11 | #if defined(OS_WIN) |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 12 | #include <windows.h> |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 13 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 14 | #include <shlobj.h> |
[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" |
eroman | ed744f3 | 2015-04-09 06:35:49 | [diff] [blame] | 73 | #include "net/cert_net/nss_ocsp.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 74 | #include "net/cookies/cookie_monster.h" |
| 75 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 76 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 77 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 78 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | #include "net/http/http_cache.h" |
| 80 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 81 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 82 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 83 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 84 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 85 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 86 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 87 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 88 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 89 | #include "net/log/test_net_log_entry.h" |
| 90 | #include "net/log/test_net_log_util.h" |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 91 | #include "net/net_features.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 92 | #include "net/nqe/external_estimate_provider.h" |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 93 | #include "net/proxy/proxy_server.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 94 | #include "net/proxy/proxy_service.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 95 | #include "net/quic/chromium/mock_crypto_client_stream_factory.h" |
| 96 | #include "net/quic/chromium/quic_server_info.h" |
| 97 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 98 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 99 | #include "net/ssl/channel_id_service.h" |
| 100 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 101 | #include "net/ssl/ssl_connection_status_flags.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 102 | #include "net/ssl/ssl_server_config.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 103 | #include "net/ssl/token_binding.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 104 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 105 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 106 | #include "net/test/embedded_test_server/http_request.h" |
| 107 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 108 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 109 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 110 | #include "net/test/test_data_directory.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 111 | #include "net/test/url_request/url_request_failed_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 112 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 113 | #include "net/url_request/data_protocol_handler.h" |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 114 | #include "net/url_request/network_error_logging_delegate.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 115 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 116 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 117 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 118 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 119 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 120 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 121 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 122 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 123 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 124 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 125 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 127 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 128 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 129 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 130 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 131 | #include "net/url_request/file_protocol_handler.h" |
| 132 | #include "net/url_request/url_request_file_dir_job.h" |
| 133 | #endif |
| 134 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 135 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 136 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 137 | #include "net/url_request/ftp_protocol_handler.h" |
| 138 | #endif |
| 139 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 140 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 141 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 142 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 143 | #endif |
| 144 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 145 | #if BUILDFLAG(ENABLE_REPORTING) |
| 146 | #include "net/reporting/reporting_service.h" |
| 147 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 148 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 149 | using net::test::IsError; |
| 150 | using net::test::IsOk; |
| 151 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 152 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 153 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 154 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 155 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 156 | namespace net { |
| 157 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 158 | namespace { |
| 159 | |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 160 | namespace test0 { |
| 161 | #include "net/http/transport_security_state_static_unittest0.h" |
| 162 | } |
| 163 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 164 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 165 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 166 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 167 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 168 | const base::FilePath::CharType kTestFilePath[] = |
| 169 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 170 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 171 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 172 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 173 | // Test file used in most FTP tests. |
| 174 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 175 | #endif |
| 176 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 177 | // Tests load timing information in the case a fresh connection was used, with |
| 178 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 179 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 180 | int connect_timing_flags) { |
| 181 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 182 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 183 | |
| 184 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 185 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 186 | |
| 187 | EXPECT_LE(load_timing_info.request_start, |
| 188 | load_timing_info.connect_timing.connect_start); |
| 189 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 190 | connect_timing_flags); |
| 191 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 192 | load_timing_info.send_start); |
| 193 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 194 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 195 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 196 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 197 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 198 | } |
| 199 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 200 | // Same as above, but with proxy times. |
| 201 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 202 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 203 | int connect_timing_flags) { |
| 204 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 205 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 206 | |
| 207 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 208 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 209 | |
| 210 | EXPECT_LE(load_timing_info.request_start, |
| 211 | load_timing_info.proxy_resolve_start); |
| 212 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 213 | load_timing_info.proxy_resolve_end); |
| 214 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 215 | load_timing_info.connect_timing.connect_start); |
| 216 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 217 | connect_timing_flags); |
| 218 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 219 | load_timing_info.send_start); |
| 220 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 221 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 222 | } |
| 223 | |
| 224 | // Same as above, but with a reused socket and proxy times. |
| 225 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 226 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 227 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 228 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 229 | |
| 230 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 231 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 232 | |
| 233 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 234 | |
| 235 | EXPECT_LE(load_timing_info.request_start, |
| 236 | load_timing_info.proxy_resolve_start); |
| 237 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 238 | load_timing_info.proxy_resolve_end); |
| 239 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 240 | load_timing_info.send_start); |
| 241 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 242 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 243 | } |
| 244 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 245 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 246 | // Tests load timing information in the case of a cache hit, when no cache |
| 247 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 248 | base::StringPiece TestNetResourceProvider(int key) { |
| 249 | return "header"; |
| 250 | } |
| 251 | |
| 252 | void FillBuffer(char* buffer, size_t len) { |
| 253 | static bool called = false; |
| 254 | if (!called) { |
| 255 | called = true; |
| 256 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 257 | srand(seed); |
| 258 | } |
| 259 | |
| 260 | for (size_t i = 0; i < len; i++) { |
| 261 | buffer[i] = static_cast<char>(rand()); |
| 262 | if (!buffer[i]) |
| 263 | buffer[i] = 'g'; |
| 264 | } |
| 265 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 266 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 267 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 268 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 269 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 270 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 271 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 272 | |
| 273 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 274 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 275 | |
| 276 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 277 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 278 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 279 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 280 | |
| 281 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 282 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 283 | } |
| 284 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 285 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 286 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 287 | // Tests load timing in the case that there is no HTTP response. This can be |
| 288 | // used to test in the case of errors or non-HTTP requests. |
| 289 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 290 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 291 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 292 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 293 | |
| 294 | // Only the request times should be non-null. |
| 295 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 296 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 297 | |
| 298 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 299 | |
| 300 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 301 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 302 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 303 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 304 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 305 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 306 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 307 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 308 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 309 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 310 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 311 | public: |
| 312 | TestPowerMonitorSource() {} |
| 313 | ~TestPowerMonitorSource() override {} |
| 314 | |
| 315 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 316 | |
| 317 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 318 | |
| 319 | bool IsOnBatteryPowerImpl() override { return false; } |
| 320 | |
| 321 | private: |
| 322 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 323 | }; |
| 324 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 325 | // Job that allows monitoring of its priority. |
| 326 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 327 | public: |
| 328 | // The latest priority of the job is always written to |request_priority_|. |
| 329 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 330 | NetworkDelegate* network_delegate, |
| 331 | RequestPriority* request_priority) |
| 332 | : URLRequestTestJob(request, network_delegate), |
| 333 | request_priority_(request_priority) { |
| 334 | *request_priority_ = DEFAULT_PRIORITY; |
| 335 | } |
| 336 | |
| 337 | void SetPriority(RequestPriority priority) override { |
| 338 | *request_priority_ = priority; |
| 339 | URLRequestTestJob::SetPriority(priority); |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | RequestPriority* const request_priority_; |
| 344 | }; |
| 345 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 346 | // Do a case-insensitive search through |haystack| for |needle|. |
| 347 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 348 | std::string::const_iterator it = std::search( |
| 349 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 350 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 351 | return it != haystack.end(); |
| 352 | } |
| 353 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 354 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 355 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 356 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 357 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 360 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 361 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 362 | // -1 means unknown. 0 means no encryption. |
| 363 | EXPECT_GT(ssl_info.security_bits, 0); |
| 364 | |
| 365 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 366 | uint16_t cipher_suite = |
| 367 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 368 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 371 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 372 | const GURL& host_url) { |
| 373 | std::string sent_value; |
| 374 | |
| 375 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 376 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 377 | |
| 378 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 379 | EXPECT_EQ("keep-alive", sent_value); |
| 380 | } |
| 381 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 382 | // A network delegate that allows the user to choose a subset of request stages |
| 383 | // to block in. When blocking, the delegate can do one of the following: |
| 384 | // * synchronously return a pre-specified error code, or |
| 385 | // * asynchronously return that value via an automatically called callback, |
| 386 | // or |
| 387 | // * block and wait for the user to do a callback. |
| 388 | // Additionally, the user may also specify a redirect URL -- then each request |
| 389 | // with the current URL different from the redirect target will be redirected |
| 390 | // to that target, in the on-before-URL-request stage, independent of whether |
| 391 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 392 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 393 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 394 | // Stages in which the delegate can block. |
| 395 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 396 | NOT_BLOCKED = 0, |
| 397 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 398 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 399 | ON_HEADERS_RECEIVED = 1 << 2, |
| 400 | ON_AUTH_REQUIRED = 1 << 3 |
| 401 | }; |
| 402 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 403 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 404 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 405 | enum BlockMode { |
| 406 | SYNCHRONOUS, // No callback, returns specified return values. |
| 407 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 408 | // specified return codes. |
| 409 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 410 | // |auth_retval_| are ignored. In every blocking stage the |
| 411 | // message loop is quit. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 412 | USER_NOTIFY, // User is notified by a provided callback of the |
| 413 | // blocking, and synchronously returns instructions |
| 414 | // for handling it. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 415 | }; |
| 416 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 417 | using NotificationCallback = |
| 418 | base::Callback<Error(const CompletionCallback&, const URLRequest*)>; |
| 419 | |
| 420 | using NotificationAuthCallback = |
| 421 | base::Callback<NetworkDelegate::AuthRequiredResponse(const AuthCallback&, |
| 422 | const URLRequest*)>; |
| 423 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 424 | // Creates a delegate which does not block at all. |
| 425 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 426 | |
| 427 | // For users to trigger a callback returning |response|. |
| 428 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 429 | // Only call if |block_mode_| == USER_CALLBACK. |
| 430 | void DoCallback(int response); |
| 431 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 432 | |
| 433 | // Setters. |
| 434 | void set_retval(int retval) { |
| 435 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 436 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 437 | ASSERT_NE(OK, retval); |
| 438 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 441 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 442 | // |auth_credentials_| will be passed with the response. |
| 443 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 444 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 445 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 446 | auth_retval_ = auth_retval; |
| 447 | } |
| 448 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 449 | auth_credentials_ = auth_credentials; |
[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 | void set_redirect_url(const GURL& url) { |
| 453 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 456 | void set_block_on(int block_on) { |
| 457 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 458 | } |
| 459 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 460 | // Only valid if |block_mode_| == USER_NOTIFY |
| 461 | void set_notification_callback( |
| 462 | const NotificationCallback& notification_callback) { |
| 463 | notification_callback_ = notification_callback; |
| 464 | } |
| 465 | |
| 466 | void set_notification_auth_callback( |
| 467 | const NotificationAuthCallback& notification_auth_callback) { |
| 468 | notification_auth_callback_ = notification_auth_callback; |
| 469 | } |
| 470 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 471 | // Allows the user to check in which state did we block. |
| 472 | Stage stage_blocked_for_callback() const { |
| 473 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 474 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 478 | void RunCallback(int response, const CompletionCallback& callback); |
| 479 | void RunAuthCallback(AuthRequiredResponse response, |
| 480 | const AuthCallback& callback); |
| 481 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 482 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 483 | int OnBeforeURLRequest(URLRequest* request, |
| 484 | const CompletionCallback& callback, |
| 485 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 486 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 487 | int OnBeforeStartTransaction(URLRequest* request, |
| 488 | const CompletionCallback& callback, |
| 489 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 490 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 491 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 492 | URLRequest* request, |
| 493 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 494 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 495 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 496 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 497 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 498 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 499 | URLRequest* request, |
| 500 | const AuthChallengeInfo& auth_info, |
| 501 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 502 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 503 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 504 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 505 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 506 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 507 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 508 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 509 | int MaybeBlockStage(Stage stage, |
| 510 | const URLRequest* request, |
| 511 | const CompletionCallback& callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 512 | |
| 513 | // Configuration parameters, can be adjusted by public methods: |
| 514 | const BlockMode block_mode_; |
| 515 | |
| 516 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 517 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 518 | int retval_; // To be returned in non-auth stages. |
| 519 | AuthRequiredResponse auth_retval_; |
| 520 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 521 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 522 | int block_on_; // Bit mask: in which stages to block. |
| 523 | |
| 524 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 525 | // callback. |
| 526 | AuthCredentials auth_credentials_; |
| 527 | AuthCredentials* target_auth_credentials_; |
| 528 | |
| 529 | // Internal variables, not set by not the user: |
| 530 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 531 | // USER_CALLBACK). |
| 532 | Stage stage_blocked_for_callback_; |
| 533 | |
| 534 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 535 | CompletionCallback callback_; |
| 536 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 537 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 538 | // Callback to request user instructions for blocking. |
| 539 | NotificationCallback notification_callback_; |
| 540 | NotificationAuthCallback notification_auth_callback_; |
| 541 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 542 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 543 | |
| 544 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 545 | }; |
| 546 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 547 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 548 | : block_mode_(block_mode), |
| 549 | retval_(OK), |
| 550 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 551 | block_on_(0), |
| 552 | target_auth_credentials_(NULL), |
| 553 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 554 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 558 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 559 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 560 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 561 | CompletionCallback callback = callback_; |
| 562 | Reset(); |
| 563 | RunCallback(response, callback); |
| 564 | } |
| 565 | |
| 566 | void BlockingNetworkDelegate::DoAuthCallback( |
| 567 | NetworkDelegate::AuthRequiredResponse response) { |
| 568 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 569 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 570 | AuthCallback auth_callback = auth_callback_; |
| 571 | Reset(); |
| 572 | RunAuthCallback(response, auth_callback); |
| 573 | } |
| 574 | |
| 575 | void BlockingNetworkDelegate::RunCallback(int response, |
| 576 | const CompletionCallback& callback) { |
| 577 | callback.Run(response); |
| 578 | } |
| 579 | |
| 580 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 581 | const AuthCallback& callback) { |
| 582 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 583 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 584 | *target_auth_credentials_ = auth_credentials_; |
| 585 | } |
| 586 | callback.Run(response); |
| 587 | } |
| 588 | |
| 589 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 590 | URLRequest* request, |
| 591 | const CompletionCallback& callback, |
| 592 | GURL* new_url) { |
| 593 | if (redirect_url_ == request->url()) |
| 594 | return OK; // We've already seen this request and redirected elsewhere. |
| 595 | |
| 596 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 597 | |
| 598 | if (!redirect_url_.is_empty()) |
| 599 | *new_url = redirect_url_; |
| 600 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 601 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 602 | } |
| 603 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 604 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 605 | URLRequest* request, |
| 606 | const CompletionCallback& callback, |
| 607 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 608 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 609 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 610 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 614 | URLRequest* request, |
| 615 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 616 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 617 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 618 | GURL* allowed_unsafe_redirect_url) { |
| 619 | TestNetworkDelegate::OnHeadersReceived(request, |
| 620 | callback, |
| 621 | original_response_headers, |
| 622 | override_response_headers, |
| 623 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 624 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 625 | return MaybeBlockStage(ON_HEADERS_RECEIVED, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 629 | URLRequest* request, |
| 630 | const AuthChallengeInfo& auth_info, |
| 631 | const AuthCallback& callback, |
| 632 | AuthCredentials* credentials) { |
| 633 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 634 | credentials); |
| 635 | // Check that the user has provided callback for the previous blocked stage. |
| 636 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 637 | |
| 638 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 639 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 640 | } |
| 641 | |
| 642 | target_auth_credentials_ = credentials; |
| 643 | |
| 644 | switch (block_mode_) { |
| 645 | case SYNCHRONOUS: |
| 646 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 647 | *target_auth_credentials_ = auth_credentials_; |
| 648 | return auth_retval_; |
| 649 | |
| 650 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 651 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 652 | FROM_HERE, |
| 653 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 654 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 655 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 656 | |
| 657 | case USER_CALLBACK: |
| 658 | auth_callback_ = callback; |
| 659 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 660 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 661 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 662 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 663 | |
| 664 | case USER_NOTIFY: |
| 665 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 666 | // responsibility for running the callback in the future. |
| 667 | return notification_auth_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 668 | } |
| 669 | NOTREACHED(); |
| 670 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 671 | } |
| 672 | |
| 673 | void BlockingNetworkDelegate::Reset() { |
| 674 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 675 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 676 | callback_.Reset(); |
| 677 | auth_callback_.Reset(); |
| 678 | } |
| 679 | |
| 680 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 681 | BlockingNetworkDelegate::Stage stage, |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 682 | const URLRequest* request, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 683 | const CompletionCallback& callback) { |
| 684 | // Check that the user has provided callback for the previous blocked stage. |
| 685 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 686 | |
| 687 | if ((block_on_ & stage) == 0) { |
| 688 | return OK; |
| 689 | } |
| 690 | |
| 691 | switch (block_mode_) { |
| 692 | case SYNCHRONOUS: |
| 693 | EXPECT_NE(OK, retval_); |
| 694 | return retval_; |
| 695 | |
| 696 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 697 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 698 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 699 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 700 | return ERR_IO_PENDING; |
| 701 | |
| 702 | case USER_CALLBACK: |
| 703 | callback_ = callback; |
| 704 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 705 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 706 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 707 | return ERR_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 708 | |
| 709 | case USER_NOTIFY: |
| 710 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 711 | // responsibility for running the callback in the future. |
| 712 | return notification_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 713 | } |
| 714 | NOTREACHED(); |
| 715 | return 0; |
| 716 | } |
| 717 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 718 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 719 | public: |
| 720 | // Does not own |delegate|. |
| 721 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 722 | NetworkDelegate* delegate) |
| 723 | : TestURLRequestContext(true) { |
| 724 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 725 | set_network_delegate(delegate); |
| 726 | Init(); |
| 727 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 728 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 729 | }; |
| 730 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 731 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 732 | // URI and report to be sent. |
| 733 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 734 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 735 | public: |
| 736 | MockCertificateReportSender() {} |
| 737 | ~MockCertificateReportSender() override {} |
| 738 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 739 | void Send(const GURL& report_uri, |
| 740 | base::StringPiece content_type, |
| 741 | base::StringPiece report, |
| 742 | const base::Callback<void()>& success_callback, |
| 743 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 744 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 745 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 746 | report.CopyToString(&latest_report_); |
| 747 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 748 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 749 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 750 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 751 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 752 | |
| 753 | private: |
| 754 | GURL latest_report_uri_; |
| 755 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 756 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 757 | }; |
| 758 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 759 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 760 | public: |
| 761 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 762 | }; |
| 763 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 764 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 765 | // This is needed because after the certificate failure, the URLRequest will |
| 766 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 767 | // The partial SSLInfo does not have the OCSP information filled out. |
| 768 | class OCSPErrorTestDelegate : public TestDelegate { |
| 769 | public: |
| 770 | void OnSSLCertificateError(URLRequest* request, |
| 771 | const SSLInfo& ssl_info, |
| 772 | bool fatal) override { |
| 773 | ssl_info_ = ssl_info; |
| 774 | on_ssl_certificate_error_called_ = true; |
| 775 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 776 | } |
| 777 | |
| 778 | bool on_ssl_certificate_error_called() { |
| 779 | return on_ssl_certificate_error_called_; |
| 780 | } |
| 781 | |
| 782 | SSLInfo ssl_info() { return ssl_info_; } |
| 783 | |
| 784 | private: |
| 785 | bool on_ssl_certificate_error_called_ = false; |
| 786 | SSLInfo ssl_info_; |
| 787 | }; |
| 788 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 789 | } // namespace |
| 790 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 791 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 792 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 793 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 794 | URLRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 795 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 796 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 797 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 798 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 799 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 800 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 801 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 802 | // URLRequestJobs may post clean-up tasks on destruction. |
| 803 | base::RunLoop().RunUntilIdle(); |
| 804 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 805 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 806 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 807 | SetUpFactory(); |
| 808 | default_context_.set_job_factory(job_factory_.get()); |
| 809 | default_context_.Init(); |
| 810 | PlatformTest::SetUp(); |
| 811 | } |
| 812 | |
| 813 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 814 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 815 | "data", base::WrapUnique(new DataProtocolHandler)); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 816 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 817 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 818 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 819 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 820 | #endif |
| 821 | } |
| 822 | |
| 823 | TestNetworkDelegate* default_network_delegate() { |
| 824 | return &default_network_delegate_; |
| 825 | } |
| 826 | |
| 827 | const TestURLRequestContext& default_context() const { |
| 828 | return default_context_; |
| 829 | } |
| 830 | |
| 831 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 832 | // Adds the TestJobInterceptor to the default context. |
| 833 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 834 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 835 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 836 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 837 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 838 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 839 | } |
| 840 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 841 | // Creates a temp test file and writes |data| to the file. The file will be |
| 842 | // deleted after the test completes. |
| 843 | void CreateTestFile(const char* data, |
| 844 | size_t data_size, |
| 845 | base::FilePath* test_file) { |
| 846 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 847 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 848 | // now, Mac and Android bots return a path with a symbolic link. |
| 849 | base::FilePath absolute_temp_dir = |
| 850 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 851 | |
| 852 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 853 | ASSERT_EQ(static_cast<int>(data_size), |
| 854 | base::WriteFile(*test_file, data, data_size)); |
| 855 | } |
| 856 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 857 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 858 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 859 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 860 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 861 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 862 | TestURLRequestContext default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 863 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 864 | }; |
| 865 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 866 | // This NetworkDelegate is picky about what files are accessible. Only |
| 867 | // whitelisted files are allowed. |
| 868 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 869 | public: |
| 870 | CookieBlockingNetworkDelegate(){}; |
| 871 | |
| 872 | // Adds |directory| to the access white list. |
| 873 | void AddToWhitelist(const base::FilePath& directory) { |
| 874 | whitelist_.insert(directory); |
| 875 | } |
| 876 | |
| 877 | private: |
| 878 | // Returns true if |path| matches the white list. |
| 879 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 880 | for (const auto& directory : whitelist_) { |
| 881 | if (directory == path || directory.IsParent(path)) |
| 882 | return true; |
| 883 | } |
| 884 | return false; |
| 885 | } |
| 886 | |
| 887 | // Returns true only if both |original_path| and |absolute_path| match the |
| 888 | // white list. |
| 889 | bool OnCanAccessFile(const URLRequest& request, |
| 890 | const base::FilePath& original_path, |
| 891 | const base::FilePath& absolute_path) const override { |
| 892 | return (OnCanAccessFileInternal(original_path) && |
| 893 | OnCanAccessFileInternal(absolute_path)); |
| 894 | } |
| 895 | |
| 896 | std::set<base::FilePath> whitelist_; |
| 897 | |
| 898 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 899 | }; |
| 900 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 901 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 902 | TestDelegate d; |
| 903 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 904 | std::unique_ptr<URLRequest> r( |
| 905 | default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 906 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 907 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 908 | r->Start(); |
| 909 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 910 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 911 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 912 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 913 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 914 | EXPECT_FALSE(d.received_data_before_response()); |
| 915 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 916 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 917 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 918 | |
| 919 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 920 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
| 924 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 925 | TestDelegate d; |
| 926 | { |
| 927 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 928 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 929 | GURL("data:image/png;base64," |
| 930 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 931 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 932 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 933 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 934 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 935 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 936 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 937 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 938 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 939 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 940 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 941 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 942 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 943 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 944 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 945 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 946 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 947 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 948 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 949 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 950 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 951 | r->Start(); |
| 952 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 953 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 954 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 955 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 956 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | EXPECT_FALSE(d.received_data_before_response()); |
| 958 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 959 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 960 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 961 | |
| 962 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 963 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 967 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 969 | const char kTestFileContent[] = "Hello"; |
| 970 | base::FilePath test_file; |
| 971 | ASSERT_NO_FATAL_FAILURE( |
| 972 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 973 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 974 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | |
| 976 | TestDelegate d; |
| 977 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 978 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 979 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 980 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 981 | r->Start(); |
| 982 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 983 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 984 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 985 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 986 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 987 | EXPECT_EQ(1, d.response_started_count()); |
| 988 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 989 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 990 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 991 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 992 | |
| 993 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 994 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 998 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 999 | const char kTestFileContent[] = "Hello"; |
| 1000 | base::FilePath test_file; |
| 1001 | ASSERT_NO_FATAL_FAILURE( |
| 1002 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1003 | |
| 1004 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1005 | |
| 1006 | TestDelegate d; |
| 1007 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1008 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1009 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1010 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1011 | r->Start(); |
| 1012 | EXPECT_TRUE(r->is_pending()); |
| 1013 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1014 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1015 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1016 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1017 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1018 | } |
| 1019 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1020 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1021 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1022 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1023 | FillBuffer(buffer.get(), buffer_size); |
| 1024 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1025 | base::FilePath test_file; |
| 1026 | ASSERT_NO_FATAL_FAILURE( |
| 1027 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1028 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1029 | |
| 1030 | const size_t first_byte_position = 500; |
| 1031 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1032 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1033 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1034 | buffer.get() + last_byte_position + 1); |
| 1035 | |
| 1036 | TestDelegate d; |
| 1037 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1038 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1039 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1040 | |
| 1041 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1042 | headers.SetHeader( |
| 1043 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1044 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1045 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1046 | r->SetExtraRequestHeaders(headers); |
| 1047 | r->Start(); |
| 1048 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1049 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1050 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1051 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1052 | EXPECT_EQ(1, d.response_started_count()); |
| 1053 | EXPECT_FALSE(d.received_data_before_response()); |
| 1054 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1055 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1056 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1057 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1061 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1062 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1063 | FillBuffer(buffer.get(), buffer_size); |
| 1064 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1065 | base::FilePath test_file; |
| 1066 | ASSERT_NO_FATAL_FAILURE( |
| 1067 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1068 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1069 | |
| 1070 | const size_t first_byte_position = 500; |
| 1071 | const size_t last_byte_position = buffer_size - 1; |
| 1072 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1073 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1074 | buffer.get() + last_byte_position + 1); |
| 1075 | |
| 1076 | TestDelegate d; |
| 1077 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1078 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1079 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1080 | |
| 1081 | HttpRequestHeaders headers; |
| 1082 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1083 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1084 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1085 | r->SetExtraRequestHeaders(headers); |
| 1086 | r->Start(); |
| 1087 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1088 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1089 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1090 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1091 | EXPECT_EQ(1, d.response_started_count()); |
| 1092 | EXPECT_FALSE(d.received_data_before_response()); |
| 1093 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1094 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1095 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1096 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1100 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1101 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1102 | FillBuffer(buffer.get(), buffer_size); |
| 1103 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1104 | base::FilePath test_file; |
| 1105 | ASSERT_NO_FATAL_FAILURE( |
| 1106 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1107 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1108 | |
| 1109 | TestDelegate d; |
| 1110 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1111 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1112 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1113 | |
| 1114 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1115 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1116 | r->SetExtraRequestHeaders(headers); |
| 1117 | r->Start(); |
| 1118 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1119 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1120 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | EXPECT_TRUE(d.request_failed()); |
| 1122 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1123 | } |
| 1124 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1125 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1126 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1127 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1128 | ASSERT_NO_FATAL_FAILURE( |
| 1129 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1130 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1131 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1132 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1133 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1134 | // Hence the test should use the directory name of |test_file|, rather than |
| 1135 | // |absolute_temp_dir|, for whitelisting. |
| 1136 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1137 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1138 | { |
| 1139 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1140 | CookieBlockingNetworkDelegate network_delegate; |
| 1141 | network_delegate.AddToWhitelist(real_temp_dir); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1142 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1143 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1144 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1145 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1146 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1147 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1148 | EXPECT_FALSE(d.request_failed()); |
| 1149 | EXPECT_EQ(test_data, d.data_received()); |
| 1150 | } |
| 1151 | |
| 1152 | { |
| 1153 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1154 | CookieBlockingNetworkDelegate network_delegate; |
[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 rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1161 | EXPECT_TRUE(d.request_failed()); |
| 1162 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1163 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1167 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1168 | |
| 1169 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1170 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1171 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1172 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1173 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1174 | |
| 1175 | // Create a good directory (will be whitelisted) and a good file. |
| 1176 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1177 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1178 | base::FilePath good_file; |
| 1179 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1180 | std::string good_data("good"); |
| 1181 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1182 | // See the comment in AllowFileURLs() for why this is done. |
| 1183 | base::FilePath real_good_dir = good_file.DirName(); |
| 1184 | |
| 1185 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1186 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1187 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1188 | base::FilePath bad_file; |
| 1189 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1190 | std::string bad_data("bad"); |
| 1191 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1192 | |
| 1193 | // This symlink will point to the good file. Access to the symlink will be |
| 1194 | // allowed as both the symlink and the destination file are in the same |
| 1195 | // good directory. |
| 1196 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1197 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1198 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1199 | // This symlink will point to the bad file. Even though the symlink is in |
| 1200 | // the good directory, access to the symlink will be rejected since it |
| 1201 | // points to the bad file. |
| 1202 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1203 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1204 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1205 | |
| 1206 | CookieBlockingNetworkDelegate network_delegate; |
| 1207 | network_delegate.AddToWhitelist(real_good_dir); |
| 1208 | { |
| 1209 | TestDelegate d; |
| 1210 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1211 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1212 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1213 | r->Start(); |
| 1214 | base::RunLoop().Run(); |
| 1215 | // good_file_url should be allowed. |
| 1216 | EXPECT_FALSE(d.request_failed()); |
| 1217 | EXPECT_EQ(good_data, d.data_received()); |
| 1218 | } |
| 1219 | |
| 1220 | { |
| 1221 | TestDelegate d; |
| 1222 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1223 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1224 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1225 | r->Start(); |
| 1226 | base::RunLoop().Run(); |
| 1227 | // bad_file_url should be rejected. |
| 1228 | EXPECT_TRUE(d.request_failed()); |
| 1229 | EXPECT_EQ("", d.data_received()); |
| 1230 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1235 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1236 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1237 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame^] | 1238 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1239 | |
| 1240 | // Create a good directory (will be whitelisted). |
| 1241 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1242 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1243 | |
| 1244 | // Create a bad directory (will not be whitelisted). |
| 1245 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1246 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1247 | |
| 1248 | // This symlink will point to the good directory. Access to the symlink |
| 1249 | // will be allowed as the symlink is in the good dir that'll be white |
| 1250 | // listed. |
| 1251 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1252 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1253 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1254 | // This symlink will point to the bad directory. Even though the symlink is |
| 1255 | // in the good directory, access to the symlink will be rejected since it |
| 1256 | // points to the bad directory. |
| 1257 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1258 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1259 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1260 | |
| 1261 | CookieBlockingNetworkDelegate network_delegate; |
| 1262 | network_delegate.AddToWhitelist(good_dir); |
| 1263 | { |
| 1264 | TestDelegate d; |
| 1265 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1266 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1267 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1268 | r->Start(); |
| 1269 | base::RunLoop().Run(); |
| 1270 | // good_file_url should be allowed. |
| 1271 | EXPECT_FALSE(d.request_failed()); |
| 1272 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1273 | } |
| 1274 | |
| 1275 | { |
| 1276 | TestDelegate d; |
| 1277 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1278 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1279 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1280 | r->Start(); |
| 1281 | base::RunLoop().Run(); |
| 1282 | // bad_file_url should be rejected. |
| 1283 | EXPECT_TRUE(d.request_failed()); |
| 1284 | EXPECT_EQ("", d.data_received()); |
| 1285 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1286 | } |
| 1287 | } |
| 1288 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1289 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1290 | |
| 1291 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1292 | // Put in mock resource provider. |
| 1293 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1294 | |
| 1295 | TestDelegate d; |
| 1296 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1297 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1298 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1299 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1300 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1301 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1302 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1303 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1304 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1305 | req->Start(); |
| 1306 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1307 | |
| 1308 | d.set_cancel_in_received_data_pending(true); |
| 1309 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1310 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | // Take out mock resource provider. |
| 1314 | NetModule::SetResourceProvider(NULL); |
| 1315 | } |
| 1316 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1317 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1318 | // Verify the general sanity of the the output of the file: |
| 1319 | // directory lister by checking for the output of a known existing |
| 1320 | // file. |
| 1321 | const char sentinel_name[] = "filedir-sentinel"; |
| 1322 | |
| 1323 | base::FilePath path; |
| 1324 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1325 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1326 | |
| 1327 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1328 | std::unique_ptr<URLRequest> req( |
| 1329 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1330 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1331 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1332 | base::RunLoop().Run(); |
| 1333 | |
| 1334 | // Generate entry for the sentinel file. |
| 1335 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1336 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1337 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1338 | EXPECT_GT(info.size, 0); |
| 1339 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1340 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1341 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1342 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1343 | info.last_modified); |
| 1344 | |
| 1345 | ASSERT_LT(0, d.bytes_received()); |
| 1346 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1347 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1348 | // Check for the entry generated for the "sentinel" file. |
| 1349 | const std::string& data = d.data_received(); |
| 1350 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1351 | } |
| 1352 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1353 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1354 | // There is an implicit redirect when loading a file path that matches a |
| 1355 | // directory and does not end with a slash. Ensure that following such |
| 1356 | // redirects does not crash. See http://crbug.com/18686. |
| 1357 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1358 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1359 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1360 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1361 | |
| 1362 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1363 | std::unique_ptr<URLRequest> req( |
| 1364 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1365 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1366 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1367 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1368 | |
| 1369 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1370 | ASSERT_LT(0, d.bytes_received()); |
| 1371 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1372 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | #if defined(OS_WIN) |
| 1376 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1377 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1378 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1379 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1380 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1381 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1382 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1383 | |
| 1384 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1385 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1386 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1387 | #endif // defined(OS_WIN) |
| 1388 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1389 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1390 | |
| 1391 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1392 | TestDelegate d; |
| 1393 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1394 | std::unique_ptr<URLRequest> r( |
| 1395 | default_context_.CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1396 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1397 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1398 | r->Start(); |
| 1399 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1400 | |
| 1401 | base::RunLoop().Run(); |
| 1402 | EXPECT_TRUE(d.request_failed()); |
| 1403 | } |
| 1404 | } |
| 1405 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1406 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1407 | TestURLRequestContext context; |
| 1408 | TestNetworkDelegate network_delegate; |
| 1409 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1410 | context.set_network_delegate(&network_delegate); |
| 1411 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1412 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1413 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1414 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1415 | req->SetReferrer("https://somewhere.com/"); |
| 1416 | |
| 1417 | req->Start(); |
| 1418 | base::RunLoop().Run(); |
| 1419 | EXPECT_TRUE(d.request_failed()); |
| 1420 | } |
| 1421 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1422 | #if defined(OS_WIN) |
| 1423 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1424 | base::FilePath app_path; |
| 1425 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1426 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1427 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1428 | |
| 1429 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1430 | |
| 1431 | base::win::ScopedCOMInitializer com_initializer; |
| 1432 | |
| 1433 | // Temporarily create a shortcut for test |
| 1434 | { |
| 1435 | base::win::ScopedComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1436 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1437 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1438 | base::win::ScopedComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1439 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1440 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1441 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1442 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1443 | } |
| 1444 | |
| 1445 | TestDelegate d; |
| 1446 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1447 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1448 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1449 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1451 | r->Start(); |
| 1452 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1453 | |
| 1454 | base::RunLoop().Run(); |
| 1455 | |
| 1456 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1457 | GetFileAttributesEx(app_path.value().c_str(), |
| 1458 | GetFileExInfoStandard, &data); |
| 1459 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1460 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1461 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1462 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1463 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1464 | DWORD read_size; |
| 1465 | BOOL result; |
| 1466 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1467 | &read_size, NULL); |
| 1468 | std::string content(buffer.get(), read_size); |
| 1469 | CloseHandle(file); |
| 1470 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1471 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1472 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1473 | EXPECT_EQ(content, d.data_received()); |
| 1474 | } |
| 1475 | |
| 1476 | // Clean the shortcut |
| 1477 | DeleteFile(lnk_path.c_str()); |
| 1478 | } |
| 1479 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1480 | |
| 1481 | // Custom URLRequestJobs for use with interceptor tests |
| 1482 | class RestartTestJob : public URLRequestTestJob { |
| 1483 | public: |
| 1484 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1485 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1486 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1487 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1488 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1489 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1490 | }; |
| 1491 | |
| 1492 | class CancelTestJob : public URLRequestTestJob { |
| 1493 | public: |
| 1494 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1495 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1496 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1497 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1498 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1499 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1500 | }; |
| 1501 | |
| 1502 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1503 | public: |
| 1504 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1505 | NetworkDelegate* network_delegate) |
| 1506 | : URLRequestTestJob(request, network_delegate, true) { |
| 1507 | } |
| 1508 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1509 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1510 | request_->Cancel(); |
| 1511 | this->NotifyRestartRequired(); |
| 1512 | } |
| 1513 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1514 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1515 | }; |
| 1516 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1517 | // An Interceptor for use with interceptor tests. |
| 1518 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1519 | public: |
| 1520 | // Static getters for canned response header and data strings. |
| 1521 | static std::string ok_data() { |
| 1522 | return URLRequestTestJob::test_data_1(); |
| 1523 | } |
| 1524 | |
| 1525 | static std::string ok_headers() { |
| 1526 | return URLRequestTestJob::test_headers(); |
| 1527 | } |
| 1528 | |
| 1529 | static std::string redirect_data() { |
| 1530 | return std::string(); |
| 1531 | } |
| 1532 | |
| 1533 | static std::string redirect_headers() { |
| 1534 | return URLRequestTestJob::test_redirect_headers(); |
| 1535 | } |
| 1536 | |
| 1537 | static std::string error_data() { |
| 1538 | return std::string("ohhh nooooo mr. bill!"); |
| 1539 | } |
| 1540 | |
| 1541 | static std::string error_headers() { |
| 1542 | return URLRequestTestJob::test_error_headers(); |
| 1543 | } |
| 1544 | |
| 1545 | MockURLRequestInterceptor() |
| 1546 | : intercept_main_request_(false), restart_main_request_(false), |
| 1547 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1548 | simulate_main_network_error_(false), |
| 1549 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1550 | intercept_final_response_(false), cancel_final_request_(false), |
| 1551 | use_url_request_http_job_(false), |
| 1552 | did_intercept_main_(false), did_restart_main_(false), |
| 1553 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1554 | did_simulate_error_main_(false), |
| 1555 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1556 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1557 | } |
| 1558 | |
| 1559 | ~MockURLRequestInterceptor() override { |
| 1560 | } |
| 1561 | |
| 1562 | // URLRequestInterceptor implementation: |
| 1563 | URLRequestJob* MaybeInterceptRequest( |
| 1564 | URLRequest* request, |
| 1565 | NetworkDelegate* network_delegate) const override { |
| 1566 | if (restart_main_request_) { |
| 1567 | restart_main_request_ = false; |
| 1568 | did_restart_main_ = true; |
| 1569 | return new RestartTestJob(request, network_delegate); |
| 1570 | } |
| 1571 | if (cancel_main_request_) { |
| 1572 | cancel_main_request_ = false; |
| 1573 | did_cancel_main_ = true; |
| 1574 | return new CancelTestJob(request, network_delegate); |
| 1575 | } |
| 1576 | if (cancel_then_restart_main_request_) { |
| 1577 | cancel_then_restart_main_request_ = false; |
| 1578 | did_cancel_then_restart_main_ = true; |
| 1579 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1580 | } |
| 1581 | if (simulate_main_network_error_) { |
| 1582 | simulate_main_network_error_ = false; |
| 1583 | did_simulate_error_main_ = true; |
| 1584 | if (use_url_request_http_job_) { |
| 1585 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1586 | } |
| 1587 | // This job will result in error since the requested URL is not one of the |
| 1588 | // URLs supported by these tests. |
| 1589 | return new URLRequestTestJob(request, network_delegate, true); |
| 1590 | } |
| 1591 | if (!intercept_main_request_) |
| 1592 | return nullptr; |
| 1593 | intercept_main_request_ = false; |
| 1594 | did_intercept_main_ = true; |
| 1595 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1596 | network_delegate, |
| 1597 | main_headers_, |
| 1598 | main_data_, |
| 1599 | true); |
| 1600 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1601 | return job; |
| 1602 | } |
| 1603 | |
| 1604 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1605 | NetworkDelegate* network_delegate, |
| 1606 | const GURL& location) const override { |
| 1607 | if (cancel_redirect_request_) { |
| 1608 | cancel_redirect_request_ = false; |
| 1609 | did_cancel_redirect_ = true; |
| 1610 | return new CancelTestJob(request, network_delegate); |
| 1611 | } |
| 1612 | if (!intercept_redirect_) |
| 1613 | return nullptr; |
| 1614 | intercept_redirect_ = false; |
| 1615 | did_intercept_redirect_ = true; |
| 1616 | if (use_url_request_http_job_) { |
| 1617 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1618 | } |
| 1619 | return new URLRequestTestJob(request, |
| 1620 | network_delegate, |
| 1621 | redirect_headers_, |
| 1622 | redirect_data_, |
| 1623 | true); |
| 1624 | } |
| 1625 | |
| 1626 | URLRequestJob* MaybeInterceptResponse( |
| 1627 | URLRequest* request, |
| 1628 | NetworkDelegate* network_delegate) const override { |
| 1629 | if (cancel_final_request_) { |
| 1630 | cancel_final_request_ = false; |
| 1631 | did_cancel_final_ = true; |
| 1632 | return new CancelTestJob(request, network_delegate); |
| 1633 | } |
| 1634 | if (!intercept_final_response_) |
| 1635 | return nullptr; |
| 1636 | intercept_final_response_ = false; |
| 1637 | did_intercept_final_ = true; |
| 1638 | if (use_url_request_http_job_) { |
| 1639 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1640 | } |
| 1641 | return new URLRequestTestJob(request, |
| 1642 | network_delegate, |
| 1643 | final_headers_, |
| 1644 | final_data_, |
| 1645 | true); |
| 1646 | } |
| 1647 | |
| 1648 | void set_intercept_main_request(bool intercept_main_request) { |
| 1649 | intercept_main_request_ = intercept_main_request; |
| 1650 | } |
| 1651 | |
| 1652 | void set_main_headers(const std::string& main_headers) { |
| 1653 | main_headers_ = main_headers; |
| 1654 | } |
| 1655 | |
| 1656 | void set_main_data(const std::string& main_data) { |
| 1657 | main_data_ = main_data; |
| 1658 | } |
| 1659 | |
| 1660 | void set_main_request_load_timing_info( |
| 1661 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1662 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1663 | } |
| 1664 | |
| 1665 | void set_restart_main_request(bool restart_main_request) { |
| 1666 | restart_main_request_ = restart_main_request; |
| 1667 | } |
| 1668 | |
| 1669 | void set_cancel_main_request(bool cancel_main_request) { |
| 1670 | cancel_main_request_ = cancel_main_request; |
| 1671 | } |
| 1672 | |
| 1673 | void set_cancel_then_restart_main_request( |
| 1674 | bool cancel_then_restart_main_request) { |
| 1675 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1676 | } |
| 1677 | |
| 1678 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1679 | simulate_main_network_error_ = simulate_main_network_error; |
| 1680 | } |
| 1681 | |
| 1682 | void set_intercept_redirect(bool intercept_redirect) { |
| 1683 | intercept_redirect_ = intercept_redirect; |
| 1684 | } |
| 1685 | |
| 1686 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1687 | redirect_headers_ = redirect_headers; |
| 1688 | } |
| 1689 | |
| 1690 | void set_redirect_data(const std::string& redirect_data) { |
| 1691 | redirect_data_ = redirect_data; |
| 1692 | } |
| 1693 | |
| 1694 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1695 | cancel_redirect_request_ = cancel_redirect_request; |
| 1696 | } |
| 1697 | |
| 1698 | void set_intercept_final_response(bool intercept_final_response) { |
| 1699 | intercept_final_response_ = intercept_final_response; |
| 1700 | } |
| 1701 | |
| 1702 | void set_final_headers(const std::string& final_headers) { |
| 1703 | final_headers_ = final_headers; |
| 1704 | } |
| 1705 | |
| 1706 | void set_final_data(const std::string& final_data) { |
| 1707 | final_data_ = final_data; |
| 1708 | } |
| 1709 | |
| 1710 | void set_cancel_final_request(bool cancel_final_request) { |
| 1711 | cancel_final_request_ = cancel_final_request; |
| 1712 | } |
| 1713 | |
| 1714 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1715 | use_url_request_http_job_ = use_url_request_http_job; |
| 1716 | } |
| 1717 | |
| 1718 | bool did_intercept_main() const { |
| 1719 | return did_intercept_main_; |
| 1720 | } |
| 1721 | |
| 1722 | bool did_restart_main() const { |
| 1723 | return did_restart_main_; |
| 1724 | } |
| 1725 | |
| 1726 | bool did_cancel_main() const { |
| 1727 | return did_cancel_main_; |
| 1728 | } |
| 1729 | |
| 1730 | bool did_cancel_then_restart_main() const { |
| 1731 | return did_cancel_then_restart_main_; |
| 1732 | } |
| 1733 | |
| 1734 | bool did_simulate_error_main() const { |
| 1735 | return did_simulate_error_main_; |
| 1736 | } |
| 1737 | |
| 1738 | bool did_intercept_redirect() const { |
| 1739 | return did_intercept_redirect_; |
| 1740 | } |
| 1741 | |
| 1742 | bool did_cancel_redirect() const { |
| 1743 | return did_cancel_redirect_; |
| 1744 | } |
| 1745 | |
| 1746 | bool did_intercept_final() const { |
| 1747 | return did_intercept_final_; |
| 1748 | } |
| 1749 | |
| 1750 | bool did_cancel_final() const { |
| 1751 | return did_cancel_final_; |
| 1752 | } |
| 1753 | |
| 1754 | private: |
| 1755 | // Indicate whether to intercept the main request, and if so specify the |
| 1756 | // response to return and the LoadTimingInfo to use. |
| 1757 | mutable bool intercept_main_request_; |
| 1758 | mutable std::string main_headers_; |
| 1759 | mutable std::string main_data_; |
| 1760 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1761 | |
| 1762 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1763 | mutable bool restart_main_request_; |
| 1764 | mutable bool cancel_main_request_; |
| 1765 | mutable bool cancel_then_restart_main_request_; |
| 1766 | mutable bool simulate_main_network_error_; |
| 1767 | |
| 1768 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1769 | // return. |
| 1770 | mutable bool intercept_redirect_; |
| 1771 | mutable std::string redirect_headers_; |
| 1772 | mutable std::string redirect_data_; |
| 1773 | |
| 1774 | // Cancel the request within MaybeInterceptRedirect. |
| 1775 | mutable bool cancel_redirect_request_; |
| 1776 | |
| 1777 | // Indicate whether to intercept the final response, and if so specify the |
| 1778 | // response to return. |
| 1779 | mutable bool intercept_final_response_; |
| 1780 | mutable std::string final_headers_; |
| 1781 | mutable std::string final_data_; |
| 1782 | |
| 1783 | // Cancel the final request within MaybeInterceptResponse. |
| 1784 | mutable bool cancel_final_request_; |
| 1785 | |
| 1786 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1787 | mutable bool use_url_request_http_job_; |
| 1788 | |
| 1789 | // These indicate if the interceptor did something or not. |
| 1790 | mutable bool did_intercept_main_; |
| 1791 | mutable bool did_restart_main_; |
| 1792 | mutable bool did_cancel_main_; |
| 1793 | mutable bool did_cancel_then_restart_main_; |
| 1794 | mutable bool did_simulate_error_main_; |
| 1795 | mutable bool did_intercept_redirect_; |
| 1796 | mutable bool did_cancel_redirect_; |
| 1797 | mutable bool did_intercept_final_; |
| 1798 | mutable bool did_cancel_final_; |
| 1799 | }; |
| 1800 | |
| 1801 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1802 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1803 | public: |
| 1804 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1805 | } |
| 1806 | |
| 1807 | ~URLRequestInterceptorTest() override { |
| 1808 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1809 | base::RunLoop().RunUntilIdle(); |
| 1810 | } |
| 1811 | |
| 1812 | void SetUpFactory() override { |
| 1813 | interceptor_ = new MockURLRequestInterceptor(); |
| 1814 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1815 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | MockURLRequestInterceptor* interceptor() const { |
| 1819 | return interceptor_; |
| 1820 | } |
| 1821 | |
| 1822 | private: |
| 1823 | MockURLRequestInterceptor* interceptor_; |
| 1824 | }; |
| 1825 | |
| 1826 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1827 | // Intercept the main request and respond with a simple response. |
| 1828 | interceptor()->set_intercept_main_request(true); |
| 1829 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1830 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1831 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1832 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1833 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1834 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1835 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1836 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1837 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1838 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1839 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1840 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1841 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1842 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1843 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1844 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1845 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1846 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1847 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1848 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1849 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1850 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1851 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1852 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1853 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1854 | EXPECT_EQ(1, d.response_started_count()); |
| 1855 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1856 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1857 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1858 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1859 | // Intercept the main request and respond with a redirect. |
| 1860 | interceptor()->set_intercept_main_request(true); |
| 1861 | interceptor()->set_main_headers( |
| 1862 | MockURLRequestInterceptor::redirect_headers()); |
| 1863 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1864 | |
| 1865 | // Intercept that redirect and respond with a final OK response. |
| 1866 | interceptor()->set_intercept_redirect(true); |
| 1867 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1868 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1869 | |
| 1870 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1871 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1872 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1873 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1874 | req->set_method("GET"); |
| 1875 | req->Start(); |
| 1876 | base::RunLoop().Run(); |
| 1877 | |
| 1878 | // Check that the interceptor got called as expected. |
| 1879 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1880 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1881 | |
| 1882 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1883 | int status = d.request_status(); |
| 1884 | EXPECT_EQ(OK, status); |
| 1885 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1886 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1887 | |
| 1888 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1889 | EXPECT_EQ(1, d.response_started_count()); |
| 1890 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1891 | } |
| 1892 | |
| 1893 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1894 | // Intercept the main request to generate a server error response. |
| 1895 | interceptor()->set_intercept_main_request(true); |
| 1896 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1897 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1898 | |
| 1899 | // Intercept that error and respond with an OK response. |
| 1900 | interceptor()->set_intercept_final_response(true); |
| 1901 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1902 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1903 | |
| 1904 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1905 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1906 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1907 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1908 | req->set_method("GET"); |
| 1909 | req->Start(); |
| 1910 | base::RunLoop().Run(); |
| 1911 | |
| 1912 | // Check that the interceptor got called as expected. |
| 1913 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1914 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1915 | |
| 1916 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1917 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1918 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1919 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1920 | EXPECT_EQ(1, d.response_started_count()); |
| 1921 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1922 | } |
| 1923 | |
| 1924 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1925 | // Intercept the main request to simulate a network error. |
| 1926 | interceptor()->set_simulate_main_network_error(true); |
| 1927 | |
| 1928 | // Intercept that error and respond with an OK response. |
| 1929 | interceptor()->set_intercept_final_response(true); |
| 1930 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1931 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1932 | |
| 1933 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1934 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1935 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1936 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1937 | req->set_method("GET"); |
| 1938 | req->Start(); |
| 1939 | base::RunLoop().Run(); |
| 1940 | |
| 1941 | // Check that the interceptor got called as expected. |
| 1942 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1943 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1944 | |
| 1945 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1946 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1947 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1948 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1949 | EXPECT_EQ(1, d.response_started_count()); |
| 1950 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1951 | } |
| 1952 | |
| 1953 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1954 | // Restart the main request. |
| 1955 | interceptor()->set_restart_main_request(true); |
| 1956 | |
| 1957 | // then intercept the new main request and respond with an OK response |
| 1958 | interceptor()->set_intercept_main_request(true); |
| 1959 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1960 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1961 | |
| 1962 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1963 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1964 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1965 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1966 | req->set_method("GET"); |
| 1967 | req->Start(); |
| 1968 | base::RunLoop().Run(); |
| 1969 | |
| 1970 | // Check that the interceptor got called as expected. |
| 1971 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1972 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1973 | |
| 1974 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1975 | int status = d.request_status(); |
| 1976 | EXPECT_EQ(OK, status); |
| 1977 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1978 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1979 | |
| 1980 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1981 | EXPECT_EQ(1, d.response_started_count()); |
| 1982 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1983 | } |
| 1984 | |
| 1985 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1986 | // Intercept the main request and cancel from within the restarted job. |
| 1987 | interceptor()->set_cancel_main_request(true); |
| 1988 | |
| 1989 | // Set up to intercept the final response and override it with an OK response. |
| 1990 | interceptor()->set_intercept_final_response(true); |
| 1991 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1992 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1993 | |
| 1994 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1995 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1996 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1997 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1998 | req->set_method("GET"); |
| 1999 | req->Start(); |
| 2000 | base::RunLoop().Run(); |
| 2001 | |
| 2002 | // Check that the interceptor got called as expected. |
| 2003 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2004 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2005 | |
| 2006 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2007 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2011 | // Intercept the main request and respond with a redirect. |
| 2012 | interceptor()->set_intercept_main_request(true); |
| 2013 | interceptor()->set_main_headers( |
| 2014 | MockURLRequestInterceptor::redirect_headers()); |
| 2015 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2016 | |
| 2017 | // Intercept the redirect and cancel from within that job. |
| 2018 | interceptor()->set_cancel_redirect_request(true); |
| 2019 | |
| 2020 | // Set up to intercept the final response and override it with an OK response. |
| 2021 | interceptor()->set_intercept_final_response(true); |
| 2022 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2023 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2024 | |
| 2025 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2026 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2027 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2028 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2029 | req->set_method("GET"); |
| 2030 | req->Start(); |
| 2031 | base::RunLoop().Run(); |
| 2032 | |
| 2033 | // Check that the interceptor got called as expected. |
| 2034 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2035 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2036 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2037 | |
| 2038 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2039 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2043 | // Intercept the main request to simulate a network error. |
| 2044 | interceptor()->set_simulate_main_network_error(true); |
| 2045 | |
| 2046 | // Set up to intercept final the response and cancel from within that job. |
| 2047 | interceptor()->set_cancel_final_request(true); |
| 2048 | |
| 2049 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2050 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2051 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2052 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2053 | req->set_method("GET"); |
| 2054 | req->Start(); |
| 2055 | base::RunLoop().Run(); |
| 2056 | |
| 2057 | // Check that the interceptor got called as expected. |
| 2058 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2059 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2060 | |
| 2061 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2062 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2066 | // Intercept the main request and cancel then restart from within that job. |
| 2067 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2068 | |
| 2069 | // Set up to intercept the final response and override it with an OK response. |
| 2070 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2071 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2072 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2073 | |
| 2074 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2075 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2076 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2077 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2078 | req->set_method("GET"); |
| 2079 | req->Start(); |
| 2080 | base::RunLoop().Run(); |
| 2081 | |
| 2082 | // Check that the interceptor got called as expected. |
| 2083 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2084 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2085 | |
| 2086 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2087 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2091 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2092 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2093 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2094 | int connect_time_flags, |
| 2095 | bool used_proxy) { |
| 2096 | LoadTimingInfo load_timing; |
| 2097 | load_timing.socket_log_id = 1; |
| 2098 | |
| 2099 | if (used_proxy) { |
| 2100 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2101 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2102 | } |
| 2103 | |
| 2104 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2105 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2106 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2107 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2108 | } |
| 2109 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2110 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2111 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2112 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2113 | } |
| 2114 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2115 | |
| 2116 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2117 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2118 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2119 | return load_timing; |
| 2120 | } |
| 2121 | |
| 2122 | // Same as above, but in the case of a reused socket. |
| 2123 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2124 | bool used_proxy) { |
| 2125 | LoadTimingInfo load_timing; |
| 2126 | load_timing.socket_log_id = 1; |
| 2127 | load_timing.socket_reused = true; |
| 2128 | |
| 2129 | if (used_proxy) { |
| 2130 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2131 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2132 | } |
| 2133 | |
| 2134 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2135 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2136 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2137 | return load_timing; |
| 2138 | } |
| 2139 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2140 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2141 | const LoadTimingInfo& job_load_timing, |
| 2142 | const URLRequestContext& context, |
| 2143 | MockURLRequestInterceptor* interceptor) { |
| 2144 | interceptor->set_intercept_main_request(true); |
| 2145 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2146 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2147 | std::unique_ptr<URLRequest> req( |
| 2148 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2149 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2150 | req->Start(); |
| 2151 | base::RunLoop().Run(); |
| 2152 | |
| 2153 | LoadTimingInfo resulting_load_timing; |
| 2154 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2155 | |
| 2156 | // None of these should be modified by the URLRequest. |
| 2157 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2158 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2159 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2160 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 2161 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2162 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2163 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2164 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2165 | |
| 2166 | return resulting_load_timing; |
| 2167 | } |
| 2168 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2169 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2170 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2171 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2172 | LoadTimingInfo job_load_timing = |
| 2173 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2174 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2175 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2176 | RunURLRequestInterceptorLoadTimingTest( |
| 2177 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2178 | |
| 2179 | // Nothing should have been changed by the URLRequest. |
| 2180 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2181 | load_timing_result.proxy_resolve_start); |
| 2182 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2183 | load_timing_result.proxy_resolve_end); |
| 2184 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2185 | load_timing_result.connect_timing.dns_start); |
| 2186 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2187 | load_timing_result.connect_timing.dns_end); |
| 2188 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2189 | load_timing_result.connect_timing.connect_start); |
| 2190 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2191 | load_timing_result.connect_timing.connect_end); |
| 2192 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2193 | load_timing_result.connect_timing.ssl_start); |
| 2194 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2195 | load_timing_result.connect_timing.ssl_end); |
| 2196 | |
| 2197 | // Redundant sanity check. |
| 2198 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2199 | } |
| 2200 | |
| 2201 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2202 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2203 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2204 | LoadTimingInfo job_load_timing = |
| 2205 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2206 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2207 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2208 | RunURLRequestInterceptorLoadTimingTest( |
| 2209 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2210 | |
| 2211 | // Nothing should have been changed by the URLRequest. |
| 2212 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2213 | load_timing_result.proxy_resolve_start); |
| 2214 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2215 | load_timing_result.proxy_resolve_end); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2217 | load_timing_result.connect_timing.dns_start); |
| 2218 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2219 | load_timing_result.connect_timing.dns_end); |
| 2220 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2221 | load_timing_result.connect_timing.connect_start); |
| 2222 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2223 | load_timing_result.connect_timing.connect_end); |
| 2224 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2225 | load_timing_result.connect_timing.ssl_start); |
| 2226 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2227 | load_timing_result.connect_timing.ssl_end); |
| 2228 | |
| 2229 | // Redundant sanity check. |
| 2230 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2231 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2232 | } |
| 2233 | |
| 2234 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2235 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2236 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2237 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2238 | // |
| 2239 | // 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] | 2240 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2241 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2242 | LoadTimingInfo job_load_timing = |
| 2243 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2244 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2245 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2246 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2247 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2248 | job_load_timing.connect_timing.connect_start = |
| 2249 | now - base::TimeDelta::FromDays(2); |
| 2250 | job_load_timing.connect_timing.connect_end = |
| 2251 | now - base::TimeDelta::FromDays(1); |
| 2252 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2253 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2254 | RunURLRequestInterceptorLoadTimingTest( |
| 2255 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2256 | |
| 2257 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2258 | // request_start. |
| 2259 | EXPECT_EQ(load_timing_result.request_start, |
| 2260 | load_timing_result.proxy_resolve_start); |
| 2261 | EXPECT_EQ(load_timing_result.request_start, |
| 2262 | load_timing_result.proxy_resolve_end); |
| 2263 | EXPECT_EQ(load_timing_result.request_start, |
| 2264 | load_timing_result.connect_timing.dns_start); |
| 2265 | EXPECT_EQ(load_timing_result.request_start, |
| 2266 | load_timing_result.connect_timing.dns_end); |
| 2267 | EXPECT_EQ(load_timing_result.request_start, |
| 2268 | load_timing_result.connect_timing.connect_start); |
| 2269 | EXPECT_EQ(load_timing_result.request_start, |
| 2270 | load_timing_result.connect_timing.connect_end); |
| 2271 | |
| 2272 | // Other times should have been left null. |
| 2273 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2274 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2275 | } |
| 2276 | |
| 2277 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2278 | TEST_F(URLRequestInterceptorTest, |
| 2279 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2280 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2281 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2282 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2283 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2284 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2285 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2286 | RunURLRequestInterceptorLoadTimingTest( |
| 2287 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2288 | |
| 2289 | // Proxy times and connect times should all be replaced with request_start. |
| 2290 | EXPECT_EQ(load_timing_result.request_start, |
| 2291 | load_timing_result.proxy_resolve_start); |
| 2292 | EXPECT_EQ(load_timing_result.request_start, |
| 2293 | load_timing_result.proxy_resolve_end); |
| 2294 | |
| 2295 | // Other times should have been left null. |
| 2296 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2297 | } |
| 2298 | |
| 2299 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2300 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2301 | // not considered reused in this test (May be a preconnect). |
| 2302 | // |
| 2303 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2304 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2305 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2306 | LoadTimingInfo job_load_timing = |
| 2307 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2308 | job_load_timing.connect_timing.connect_start = |
| 2309 | now - base::TimeDelta::FromDays(1); |
| 2310 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2311 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2312 | job_load_timing.connect_timing.connect_end = |
| 2313 | now - base::TimeDelta::FromDays(4); |
| 2314 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2315 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2316 | RunURLRequestInterceptorLoadTimingTest( |
| 2317 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2318 | |
| 2319 | // Connect times, and SSL times should be replaced with request_start. |
| 2320 | EXPECT_EQ(load_timing_result.request_start, |
| 2321 | load_timing_result.connect_timing.connect_start); |
| 2322 | EXPECT_EQ(load_timing_result.request_start, |
| 2323 | load_timing_result.connect_timing.ssl_start); |
| 2324 | EXPECT_EQ(load_timing_result.request_start, |
| 2325 | load_timing_result.connect_timing.ssl_end); |
| 2326 | EXPECT_EQ(load_timing_result.request_start, |
| 2327 | load_timing_result.connect_timing.connect_end); |
| 2328 | |
| 2329 | // Other times should have been left null. |
| 2330 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2331 | } |
| 2332 | |
| 2333 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2334 | // |request_start|, due to reusing a connected socket in the case that there |
| 2335 | // are also proxy times. The connected socket is not considered reused in this |
| 2336 | // test (May be a preconnect). |
| 2337 | // |
| 2338 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2339 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2340 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2341 | LoadTimingInfo job_load_timing = |
| 2342 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2343 | job_load_timing.connect_timing.connect_start = |
| 2344 | now - base::TimeDelta::FromDays(1); |
| 2345 | job_load_timing.connect_timing.connect_end = |
| 2346 | now - base::TimeDelta::FromDays(2); |
| 2347 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2348 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2349 | RunURLRequestInterceptorLoadTimingTest( |
| 2350 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2351 | |
| 2352 | // Connect times should be replaced with proxy_resolve_end. |
| 2353 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2354 | load_timing_result.connect_timing.connect_start); |
| 2355 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2356 | load_timing_result.connect_timing.connect_end); |
| 2357 | |
| 2358 | // Other times should have been left null. |
| 2359 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2360 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2361 | } |
| 2362 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2363 | // Check that two different URL requests have different identifiers. |
| 2364 | TEST_F(URLRequestTest, Identifiers) { |
| 2365 | TestDelegate d; |
| 2366 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2367 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2368 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2369 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2370 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2371 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2372 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2373 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2374 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2375 | } |
| 2376 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2377 | #if defined(OS_IOS) |
| 2378 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2379 | // the network delegate. crbug.com/496743 |
| 2380 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2381 | #else |
| 2382 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2383 | #endif |
| 2384 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2385 | MockHostResolver host_resolver; |
| 2386 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2387 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2388 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2389 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2390 | |
| 2391 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2392 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2393 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2394 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2395 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2396 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2397 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2398 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2399 | |
| 2400 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2401 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2402 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2403 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2404 | |
| 2405 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2406 | EXPECT_THAT(network_delegate.last_error(), |
| 2407 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2408 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2409 | } |
| 2410 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2411 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2412 | // content is empty. |
| 2413 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2414 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2415 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2416 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2417 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2418 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2419 | EXPECT_EQ("", d.data_received()); |
| 2420 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2421 | } |
| 2422 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2423 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2424 | // correctly, both before and after start. |
| 2425 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2426 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2427 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2428 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2429 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2430 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2432 | req->SetPriority(LOW); |
| 2433 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2434 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2435 | req->Start(); |
| 2436 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2437 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2438 | req->SetPriority(MEDIUM); |
| 2439 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2443 | // Start on it. |
| 2444 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2445 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2446 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2447 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2448 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2449 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2450 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2451 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2452 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2453 | req.get(), &default_network_delegate_, &job_priority)); |
| 2454 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2455 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2457 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2458 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2459 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2460 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | // Make sure that URLRequest passes on its priority updates to its |
| 2464 | // job. |
| 2465 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2466 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2467 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2468 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2469 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2470 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2471 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2472 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2473 | req.get(), &default_network_delegate_, &job_priority)); |
| 2474 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2476 | req->SetPriority(LOW); |
| 2477 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2478 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2479 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2480 | req->SetPriority(MEDIUM); |
| 2481 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2482 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2483 | } |
| 2484 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2485 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2486 | // is MAXIMUM_PRIORITY. |
| 2487 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2488 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2489 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2490 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2491 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2492 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2493 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2494 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2495 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2496 | req.get(), &default_network_delegate_, &job_priority)); |
| 2497 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2499 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2500 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2501 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2502 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2503 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2504 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2505 | req->Start(); |
| 2506 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2507 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2508 | } |
| 2509 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2510 | namespace { |
| 2511 | |
| 2512 | // Less verbose way of running a simple testserver for the tests below. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2513 | class LocalHttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2514 | public: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2515 | explicit LocalHttpTestServer(const base::FilePath& document_root) { |
| 2516 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2517 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2518 | |
| 2519 | LocalHttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
| 2520 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2521 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2522 | } // namespace |
| 2523 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2524 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2525 | LocalHttpTestServer test_server; |
| 2526 | ASSERT_TRUE(test_server.Start()); |
| 2527 | |
| 2528 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2529 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2530 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2531 | |
| 2532 | // Set up a cookie. |
| 2533 | { |
| 2534 | TestNetworkDelegate network_delegate; |
| 2535 | context.set_network_delegate(&network_delegate); |
| 2536 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2537 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2538 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2539 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2540 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2541 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2542 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2543 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2544 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2545 | } |
| 2546 | |
| 2547 | // Verify that the cookie is set. |
| 2548 | { |
| 2549 | TestNetworkDelegate network_delegate; |
| 2550 | context.set_network_delegate(&network_delegate); |
| 2551 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2552 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2553 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2554 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2555 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2556 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2557 | |
| 2558 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2559 | != std::string::npos); |
| 2560 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2561 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2562 | } |
| 2563 | } |
| 2564 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2565 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2566 | LocalHttpTestServer test_server; |
| 2567 | ASSERT_TRUE(test_server.Start()); |
| 2568 | |
| 2569 | // Set up a cookie. |
| 2570 | { |
| 2571 | TestNetworkDelegate network_delegate; |
| 2572 | default_context_.set_network_delegate(&network_delegate); |
| 2573 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2574 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2575 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2576 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2577 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2578 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2579 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2580 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2581 | } |
| 2582 | |
| 2583 | // Verify that the cookie is set. |
| 2584 | { |
| 2585 | TestNetworkDelegate network_delegate; |
| 2586 | default_context_.set_network_delegate(&network_delegate); |
| 2587 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2588 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2589 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2590 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2591 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2592 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2593 | |
| 2594 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2595 | != std::string::npos); |
| 2596 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2597 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2598 | } |
| 2599 | |
| 2600 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2601 | { |
| 2602 | TestNetworkDelegate network_delegate; |
| 2603 | default_context_.set_network_delegate(&network_delegate); |
| 2604 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2605 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2606 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2607 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2608 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2609 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2610 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2611 | |
| 2612 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2613 | == std::string::npos); |
| 2614 | |
| 2615 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2616 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2617 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2622 | LocalHttpTestServer test_server; |
| 2623 | ASSERT_TRUE(test_server.Start()); |
| 2624 | |
| 2625 | // Set up a cookie. |
| 2626 | { |
| 2627 | TestNetworkDelegate network_delegate; |
| 2628 | default_context_.set_network_delegate(&network_delegate); |
| 2629 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2630 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2631 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2632 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2633 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2634 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2635 | |
| 2636 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2637 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2638 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2639 | } |
| 2640 | |
| 2641 | // Try to set-up another cookie and update the previous cookie. |
| 2642 | { |
| 2643 | TestNetworkDelegate network_delegate; |
| 2644 | default_context_.set_network_delegate(&network_delegate); |
| 2645 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2646 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2647 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2648 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2649 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2650 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2651 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2652 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2653 | |
| 2654 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2655 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2656 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2657 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2658 | } |
| 2659 | |
| 2660 | // Verify the cookies weren't saved or updated. |
| 2661 | { |
| 2662 | TestNetworkDelegate network_delegate; |
| 2663 | default_context_.set_network_delegate(&network_delegate); |
| 2664 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2665 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2666 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2667 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2668 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2669 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2670 | |
| 2671 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2672 | == std::string::npos); |
| 2673 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2674 | != std::string::npos); |
| 2675 | |
| 2676 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2677 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2678 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2683 | LocalHttpTestServer test_server; |
| 2684 | ASSERT_TRUE(test_server.Start()); |
| 2685 | |
| 2686 | // Set up a cookie. |
| 2687 | { |
| 2688 | TestNetworkDelegate network_delegate; |
| 2689 | default_context_.set_network_delegate(&network_delegate); |
| 2690 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2691 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2692 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2693 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2694 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2695 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2696 | |
| 2697 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2698 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2699 | } |
| 2700 | |
| 2701 | // Verify that the cookie is set. |
| 2702 | { |
| 2703 | TestNetworkDelegate network_delegate; |
| 2704 | default_context_.set_network_delegate(&network_delegate); |
| 2705 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2706 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2707 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2708 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2709 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2710 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2711 | |
| 2712 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2713 | != std::string::npos); |
| 2714 | |
| 2715 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2716 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2717 | } |
| 2718 | |
| 2719 | // Verify that the cookie isn't sent. |
| 2720 | { |
| 2721 | TestNetworkDelegate network_delegate; |
| 2722 | default_context_.set_network_delegate(&network_delegate); |
| 2723 | TestDelegate d; |
| 2724 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2725 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2726 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2727 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2728 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2729 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2730 | |
| 2731 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2732 | == std::string::npos); |
| 2733 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2734 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2735 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2736 | } |
| 2737 | } |
| 2738 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2739 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2740 | #if defined(OS_IOS) |
| 2741 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2742 | #else |
| 2743 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2744 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2745 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2746 | LocalHttpTestServer test_server; |
| 2747 | ASSERT_TRUE(test_server.Start()); |
| 2748 | |
| 2749 | // Set up a cookie. |
| 2750 | { |
| 2751 | TestNetworkDelegate network_delegate; |
| 2752 | default_context_.set_network_delegate(&network_delegate); |
| 2753 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2754 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2755 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2756 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2757 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2758 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2759 | |
| 2760 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2761 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2762 | } |
| 2763 | |
| 2764 | // Try to set-up another cookie and update the previous cookie. |
| 2765 | { |
| 2766 | TestNetworkDelegate network_delegate; |
| 2767 | default_context_.set_network_delegate(&network_delegate); |
| 2768 | TestDelegate d; |
| 2769 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2770 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2771 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2772 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2773 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2774 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2775 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2776 | |
| 2777 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2778 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2779 | } |
| 2780 | |
| 2781 | // Verify the cookies weren't saved or updated. |
| 2782 | { |
| 2783 | TestNetworkDelegate network_delegate; |
| 2784 | default_context_.set_network_delegate(&network_delegate); |
| 2785 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2786 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2787 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2788 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2789 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2790 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2791 | |
| 2792 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2793 | == std::string::npos); |
| 2794 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2795 | != std::string::npos); |
| 2796 | |
| 2797 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2798 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2799 | } |
| 2800 | } |
| 2801 | |
| 2802 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2803 | LocalHttpTestServer test_server; |
| 2804 | ASSERT_TRUE(test_server.Start()); |
| 2805 | |
| 2806 | // Set up an empty cookie. |
| 2807 | { |
| 2808 | TestNetworkDelegate network_delegate; |
| 2809 | default_context_.set_network_delegate(&network_delegate); |
| 2810 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2811 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2812 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2813 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2814 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2815 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2816 | |
| 2817 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2818 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2819 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2824 | LocalHttpTestServer test_server; |
| 2825 | ASSERT_TRUE(test_server.Start()); |
| 2826 | |
| 2827 | // Set up a cookie. |
| 2828 | { |
| 2829 | TestNetworkDelegate network_delegate; |
| 2830 | default_context_.set_network_delegate(&network_delegate); |
| 2831 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2832 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2833 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2834 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2835 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2836 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2837 | |
| 2838 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2839 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2840 | } |
| 2841 | |
| 2842 | // Verify that the cookie is set. |
| 2843 | { |
| 2844 | TestNetworkDelegate network_delegate; |
| 2845 | default_context_.set_network_delegate(&network_delegate); |
| 2846 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2847 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2848 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2849 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2850 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2851 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2852 | |
| 2853 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2854 | != std::string::npos); |
| 2855 | |
| 2856 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2857 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2858 | } |
| 2859 | |
| 2860 | // Verify that the cookie isn't sent. |
| 2861 | { |
| 2862 | TestNetworkDelegate network_delegate; |
| 2863 | default_context_.set_network_delegate(&network_delegate); |
| 2864 | TestDelegate d; |
| 2865 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2866 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2867 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2868 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2869 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2870 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2871 | |
| 2872 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2873 | == std::string::npos); |
| 2874 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2875 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2876 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2881 | LocalHttpTestServer test_server; |
| 2882 | ASSERT_TRUE(test_server.Start()); |
| 2883 | |
| 2884 | // Set up a cookie. |
| 2885 | { |
| 2886 | TestNetworkDelegate network_delegate; |
| 2887 | default_context_.set_network_delegate(&network_delegate); |
| 2888 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2889 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2890 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2891 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2892 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2893 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2894 | |
| 2895 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2896 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2897 | } |
| 2898 | |
| 2899 | // Try to set-up another cookie and update the previous cookie. |
| 2900 | { |
| 2901 | TestNetworkDelegate network_delegate; |
| 2902 | default_context_.set_network_delegate(&network_delegate); |
| 2903 | TestDelegate d; |
| 2904 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2905 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2906 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2907 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2908 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2909 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2910 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2911 | |
| 2912 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2913 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2914 | } |
| 2915 | |
| 2916 | // Verify the cookies weren't saved or updated. |
| 2917 | { |
| 2918 | TestNetworkDelegate network_delegate; |
| 2919 | default_context_.set_network_delegate(&network_delegate); |
| 2920 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2921 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2922 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2923 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2924 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2925 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2926 | |
| 2927 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2928 | == std::string::npos); |
| 2929 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2930 | != std::string::npos); |
| 2931 | |
| 2932 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2933 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2934 | } |
| 2935 | } |
| 2936 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2937 | TEST_F(URLRequestTest, SameSiteCookies) { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2938 | LocalHttpTestServer test_server; |
| 2939 | ASSERT_TRUE(test_server.Start()); |
| 2940 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2941 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2942 | default_context_.set_network_delegate(&network_delegate); |
| 2943 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2944 | const std::string kHost = "example.test"; |
| 2945 | const std::string kSubHost = "subdomain.example.test"; |
| 2946 | const std::string kCrossHost = "cross-origin.test"; |
| 2947 | |
| 2948 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2949 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2950 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2951 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2952 | test_server.GetURL(kHost, |
| 2953 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2954 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2955 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2956 | req->Start(); |
| 2957 | base::RunLoop().Run(); |
| 2958 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2959 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2960 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2961 | } |
| 2962 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2963 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2964 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2965 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2966 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2967 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2968 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2969 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2970 | req->set_initiator(url::Origin(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2971 | req->Start(); |
| 2972 | base::RunLoop().Run(); |
| 2973 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2974 | EXPECT_NE(std::string::npos, |
| 2975 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2976 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2977 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2978 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2979 | } |
| 2980 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2981 | // Verify that both cookies are sent when the request has no initiator (can |
| 2982 | // happen for main frame browser-initiated navigations). |
| 2983 | { |
| 2984 | TestDelegate d; |
| 2985 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2986 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2987 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2988 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2989 | req->Start(); |
| 2990 | base::RunLoop().Run(); |
| 2991 | |
| 2992 | EXPECT_NE(std::string::npos, |
| 2993 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2994 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 2995 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2996 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2997 | } |
| 2998 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2999 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3000 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3001 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3002 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3003 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3004 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3005 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3006 | req->set_initiator(url::Origin(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3007 | req->Start(); |
| 3008 | base::RunLoop().Run(); |
| 3009 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3010 | EXPECT_NE(std::string::npos, |
| 3011 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3012 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3013 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3014 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3015 | } |
| 3016 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3017 | // Verify that neither cookie is not sent for cross-site requests. |
| 3018 | { |
| 3019 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3020 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3021 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3022 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3023 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3024 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 3025 | req->Start(); |
| 3026 | base::RunLoop().Run(); |
| 3027 | |
| 3028 | EXPECT_EQ(std::string::npos, |
| 3029 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3030 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3031 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3032 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3033 | } |
| 3034 | |
| 3035 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3036 | // method is "safe". |
| 3037 | { |
| 3038 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3039 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3040 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3041 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3042 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3043 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 3044 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3045 | req->Start(); |
| 3046 | base::RunLoop().Run(); |
| 3047 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3048 | EXPECT_EQ(std::string::npos, |
| 3049 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3050 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3051 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3052 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3053 | } |
| 3054 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3055 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3056 | // method is unsafe (e.g. POST). |
| 3057 | { |
| 3058 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3059 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3060 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3061 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3062 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3063 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3064 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3065 | req->Start(); |
| 3066 | base::RunLoop().Run(); |
| 3067 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3068 | EXPECT_EQ(std::string::npos, |
| 3069 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3070 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3071 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3072 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3073 | } |
| 3074 | } |
| 3075 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3076 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3077 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3078 | EmbeddedTestServer http_server; |
| 3079 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3080 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3081 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3082 | https_server.AddDefaultHandlers( |
| 3083 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3084 | ASSERT_TRUE(http_server.Start()); |
| 3085 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3086 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3087 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3088 | TestURLRequestContext context(true); |
| 3089 | context.set_network_delegate(&network_delegate); |
| 3090 | context.Init(); |
| 3091 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3092 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3093 | { |
| 3094 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3095 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3096 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3097 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3098 | req->Start(); |
| 3099 | base::RunLoop().Run(); |
| 3100 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3101 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3102 | } |
| 3103 | |
| 3104 | // Verify that the cookie is not set. |
| 3105 | { |
| 3106 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3107 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3108 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3109 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3110 | req->Start(); |
| 3111 | base::RunLoop().Run(); |
| 3112 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3113 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3114 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3115 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3116 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3117 | } |
| 3118 | } |
| 3119 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3120 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3121 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3122 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3123 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3124 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3125 | |
| 3126 | TestNetworkDelegate network_delegate; |
| 3127 | TestURLRequestContext context(true); |
| 3128 | context.set_network_delegate(&network_delegate); |
| 3129 | context.Init(); |
| 3130 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3131 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3132 | { |
| 3133 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3134 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3135 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3136 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3137 | req->Start(); |
| 3138 | base::RunLoop().Run(); |
| 3139 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3140 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3141 | } |
| 3142 | |
| 3143 | // Verify that the cookie is not set. |
| 3144 | { |
| 3145 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3146 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3147 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3148 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3149 | req->Start(); |
| 3150 | base::RunLoop().Run(); |
| 3151 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3152 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3153 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3154 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3155 | } |
| 3156 | } |
| 3157 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3158 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3159 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3160 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3161 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3162 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3163 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3164 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3165 | TestURLRequestContext context(true); |
| 3166 | context.set_network_delegate(&network_delegate); |
| 3167 | context.Init(); |
| 3168 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3169 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3170 | { |
| 3171 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3172 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3173 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3174 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3175 | req->Start(); |
| 3176 | base::RunLoop().Run(); |
| 3177 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3178 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3179 | } |
| 3180 | |
| 3181 | // Verify that the cookie is set. |
| 3182 | { |
| 3183 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3184 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3185 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3186 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3187 | req->Start(); |
| 3188 | base::RunLoop().Run(); |
| 3189 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3190 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3191 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3192 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3197 | // cookies are enabled. |
| 3198 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3199 | EmbeddedTestServer http_server; |
| 3200 | http_server.AddDefaultHandlers( |
| 3201 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3202 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3203 | https_server.AddDefaultHandlers( |
| 3204 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3205 | ASSERT_TRUE(http_server.Start()); |
| 3206 | ASSERT_TRUE(https_server.Start()); |
| 3207 | |
| 3208 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3209 | TestURLRequestContext context(true); |
| 3210 | context.set_network_delegate(&network_delegate); |
| 3211 | context.Init(); |
| 3212 | |
| 3213 | // Try to set a Secure cookie, with experimental features enabled. |
| 3214 | { |
| 3215 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3216 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3217 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3218 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3219 | req->Start(); |
| 3220 | base::RunLoop().Run(); |
| 3221 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3222 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3223 | } |
| 3224 | |
| 3225 | // Verify that the cookie is not set. |
| 3226 | { |
| 3227 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3228 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3229 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3230 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3231 | req->Start(); |
| 3232 | base::RunLoop().Run(); |
| 3233 | |
| 3234 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3235 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3236 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3237 | } |
| 3238 | } |
| 3239 | |
| 3240 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 3241 | // cookies are enabled. |
| 3242 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 3243 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3244 | https_server.AddDefaultHandlers( |
| 3245 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3246 | ASSERT_TRUE(https_server.Start()); |
| 3247 | |
| 3248 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3249 | TestURLRequestContext context(true); |
| 3250 | context.set_network_delegate(&network_delegate); |
| 3251 | context.Init(); |
| 3252 | |
| 3253 | // Try to set a Secure cookie, with experimental features enabled. |
| 3254 | { |
| 3255 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3256 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3257 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3258 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3259 | req->Start(); |
| 3260 | base::RunLoop().Run(); |
| 3261 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3262 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3263 | } |
| 3264 | |
| 3265 | // Verify that the cookie is not set. |
| 3266 | { |
| 3267 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3268 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3269 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3270 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3271 | req->Start(); |
| 3272 | base::RunLoop().Run(); |
| 3273 | |
| 3274 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3275 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3276 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3277 | } |
| 3278 | } |
| 3279 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3280 | TEST_F(URLRequestTest, CookieAgeMetrics) { |
| 3281 | EmbeddedTestServer http_server; |
| 3282 | http_server.AddDefaultHandlers( |
| 3283 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3284 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3285 | https_server.AddDefaultHandlers( |
| 3286 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3287 | ASSERT_TRUE(http_server.Start()); |
| 3288 | ASSERT_TRUE(https_server.Start()); |
| 3289 | |
| 3290 | TestNetworkDelegate network_delegate; |
| 3291 | default_context_.set_network_delegate(&network_delegate); |
| 3292 | base::HistogramTester histograms; |
| 3293 | |
| 3294 | const std::string kHost = "example.test"; |
| 3295 | const std::string kCrossHost = "cross-origin.test"; |
| 3296 | |
| 3297 | // Set a test cookie. |
| 3298 | { |
| 3299 | TestDelegate d; |
| 3300 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3301 | http_server.GetURL(kHost, "/set-cookie?cookie=value"), DEFAULT_PRIORITY, |
| 3302 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3303 | req->Start(); |
| 3304 | base::RunLoop().Run(); |
| 3305 | ASSERT_EQ(1, network_delegate.set_cookie_count()); |
| 3306 | } |
| 3307 | |
| 3308 | // Make a secure request to `example.test`: we shouldn't record data. |
| 3309 | { |
| 3310 | TestDelegate d; |
| 3311 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3312 | https_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3313 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3314 | req->Start(); |
| 3315 | base::RunLoop().Run(); |
| 3316 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3317 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3318 | } |
| 3319 | |
| 3320 | // Make a non-secure same-site request. |
| 3321 | { |
| 3322 | TestDelegate d; |
| 3323 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3324 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3325 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3326 | req->set_site_for_cookies(http_server.GetURL(kHost, "/")); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3327 | req->set_initiator(url::Origin(http_server.GetURL(kHost, "/"))); |
| 3328 | req->Start(); |
| 3329 | base::RunLoop().Run(); |
| 3330 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3331 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3332 | } |
| 3333 | |
| 3334 | // Make a non-secure cross-site request. |
| 3335 | { |
| 3336 | TestDelegate d; |
| 3337 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3338 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3339 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3340 | req->set_site_for_cookies(http_server.GetURL(kCrossHost, "/")); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3341 | req->set_initiator(url::Origin(http_server.GetURL(kCrossHost, "/"))); |
| 3342 | req->Start(); |
| 3343 | base::RunLoop().Run(); |
| 3344 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 1); |
| 3345 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3346 | } |
| 3347 | } |
| 3348 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3349 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3350 | // rather than a spawned test server because the connection used to talk to |
| 3351 | // the test server is affected by entering suspend mode on Android. |
| 3352 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3353 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3354 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3355 | |
| 3356 | URLRequestFailedJob::AddUrlHandler(); |
| 3357 | |
| 3358 | TestDelegate d; |
| 3359 | // Request that just hangs. |
| 3360 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3361 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3362 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3363 | r->Start(); |
| 3364 | |
| 3365 | power_monitor_source->Suspend(); |
| 3366 | // Wait for the suspend notification to cause the request to fail. |
| 3367 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3368 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3369 | EXPECT_TRUE(d.request_failed()); |
| 3370 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3371 | |
| 3372 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3373 | |
| 3374 | // Shouldn't be needed, but just in case. |
| 3375 | power_monitor_source->Resume(); |
| 3376 | } |
| 3377 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3378 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3379 | // value for the |fixed_date| argument given to the constructor. |
| 3380 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3381 | public: |
| 3382 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3383 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3384 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3385 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3386 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3387 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3388 | URLRequest* request, |
| 3389 | const CompletionCallback& callback, |
| 3390 | const HttpResponseHeaders* original_response_headers, |
| 3391 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3392 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3393 | |
| 3394 | private: |
| 3395 | std::string fixed_date_; |
| 3396 | |
| 3397 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3398 | }; |
| 3399 | |
| 3400 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3401 | URLRequest* request, |
| 3402 | const CompletionCallback& callback, |
| 3403 | const HttpResponseHeaders* original_response_headers, |
| 3404 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3405 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3406 | HttpResponseHeaders* new_response_headers = |
| 3407 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3408 | |
| 3409 | new_response_headers->RemoveHeader("Date"); |
| 3410 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3411 | |
| 3412 | *override_response_headers = new_response_headers; |
| 3413 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3414 | callback, |
| 3415 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3416 | override_response_headers, |
| 3417 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | // Test that cookie expiration times are adjusted for server/client clock |
| 3421 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3422 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3423 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 3424 | LocalHttpTestServer test_server; |
| 3425 | ASSERT_TRUE(test_server.Start()); |
| 3426 | |
| 3427 | // Set up an expired cookie. |
| 3428 | { |
| 3429 | TestNetworkDelegate network_delegate; |
| 3430 | default_context_.set_network_delegate(&network_delegate); |
| 3431 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3432 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3433 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3434 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3435 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3436 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3437 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3438 | } |
| 3439 | // Verify that the cookie is not set. |
| 3440 | { |
| 3441 | TestNetworkDelegate network_delegate; |
| 3442 | default_context_.set_network_delegate(&network_delegate); |
| 3443 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3444 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3445 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3446 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3447 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3448 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3449 | |
| 3450 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3451 | } |
| 3452 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3453 | { |
| 3454 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3455 | default_context_.set_network_delegate(&network_delegate); |
| 3456 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3457 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3458 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3459 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3460 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3461 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3462 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3463 | } |
| 3464 | // Verify that the cookie is set. |
| 3465 | { |
| 3466 | TestNetworkDelegate network_delegate; |
| 3467 | default_context_.set_network_delegate(&network_delegate); |
| 3468 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3469 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3470 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3471 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3472 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3473 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3474 | |
| 3475 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3480 | // Check that it is impossible to change the referrer in the extra headers of |
| 3481 | // an URLRequest. |
| 3482 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 3483 | LocalHttpTestServer test_server; |
| 3484 | ASSERT_TRUE(test_server.Start()); |
| 3485 | |
| 3486 | // If extra headers contain referer and the request contains a referer, |
| 3487 | // only the latter shall be respected. |
| 3488 | { |
| 3489 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3490 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3491 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3492 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3493 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3494 | |
| 3495 | HttpRequestHeaders headers; |
| 3496 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3497 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3499 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3500 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3501 | |
| 3502 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3503 | } |
| 3504 | |
| 3505 | // If extra headers contain a referer but the request does not, no referer |
| 3506 | // shall be sent in the header. |
| 3507 | { |
| 3508 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3509 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3510 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3511 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3512 | |
| 3513 | HttpRequestHeaders headers; |
| 3514 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3515 | req->SetExtraRequestHeaders(headers); |
| 3516 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3517 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3518 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3519 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3520 | |
| 3521 | EXPECT_EQ("None", d.data_received()); |
| 3522 | } |
| 3523 | } |
| 3524 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3525 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3526 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3527 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3528 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3529 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3530 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3531 | // |request_method| is the method to use for the initial request. |
| 3532 | // |redirect_method| is the method that is expected to be used for the second |
| 3533 | // request, after redirection. |
| 3534 | // If |include_data| is true, data is uploaded with the request. The |
| 3535 | // response body is expected to match it exactly, if and only if |
| 3536 | // |request_method| == |redirect_method|. |
| 3537 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3538 | const std::string& request_method, |
| 3539 | const std::string& redirect_method, |
| 3540 | bool include_data) { |
| 3541 | static const char kData[] = "hello world"; |
| 3542 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3543 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3544 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3545 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3546 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3547 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3548 | HttpRequestHeaders headers; |
| 3549 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3550 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3551 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3552 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3553 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3554 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3555 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3556 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3557 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3558 | if (include_data) { |
| 3559 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3560 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3561 | HttpRequestHeaders::kContentLength)); |
| 3562 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3563 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3564 | EXPECT_EQ(kData, d.data_received()); |
| 3565 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3566 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3567 | HttpRequestHeaders::kContentLength)); |
| 3568 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3569 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3570 | EXPECT_NE(kData, d.data_received()); |
| 3571 | } |
| 3572 | } |
| 3573 | if (HasFailure()) |
| 3574 | LOG(WARNING) << "Request method was: " << request_method; |
| 3575 | } |
| 3576 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3577 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3578 | // |request_method| is the method to use for the initial request. |
| 3579 | // |redirect_method| is the method that is expected to be used for the second |
| 3580 | // request, after redirection. |
| 3581 | // |origin_value| is the expected value for the Origin header after |
| 3582 | // redirection. If empty, expects that there will be no Origin header. |
| 3583 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3584 | const std::string& request_method, |
| 3585 | const std::string& redirect_method, |
| 3586 | const std::string& origin_value) { |
| 3587 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3588 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3589 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3590 | req->set_method(request_method); |
| 3591 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3592 | redirect_url.GetOrigin().spec(), false); |
| 3593 | req->Start(); |
| 3594 | |
| 3595 | base::RunLoop().Run(); |
| 3596 | |
| 3597 | EXPECT_EQ(redirect_method, req->method()); |
| 3598 | // Note that there is no check for request success here because, for |
| 3599 | // purposes of testing, the request very well may fail. For example, if the |
| 3600 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3601 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3602 | // request would fail. However, that's fine, as long as the request headers |
| 3603 | // are in order and pass the checks below. |
| 3604 | if (origin_value.empty()) { |
| 3605 | EXPECT_FALSE( |
| 3606 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3607 | } else { |
| 3608 | std::string origin_header; |
| 3609 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3610 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 3611 | EXPECT_EQ(origin_value, origin_header); |
| 3612 | } |
| 3613 | } |
| 3614 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3615 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3616 | const int kMsgSize = 20000; // multiple of 10 |
| 3617 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3618 | char* uploadBytes = new char[kMsgSize+1]; |
| 3619 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3620 | char marker = 'a'; |
| 3621 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3622 | memcpy(ptr, "----------", 10); |
| 3623 | ptr += 10; |
| 3624 | if (idx % 100 == 0) { |
| 3625 | ptr--; |
| 3626 | *ptr++ = marker; |
| 3627 | if (++marker > 'z') |
| 3628 | marker = 'a'; |
| 3629 | } |
| 3630 | } |
| 3631 | uploadBytes[kMsgSize] = '\0'; |
| 3632 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3633 | for (int i = 0; i < kIterations; ++i) { |
| 3634 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3635 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3636 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 3637 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3638 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3639 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3640 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3641 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3642 | r->Start(); |
| 3643 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3644 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3645 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3646 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3647 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 3648 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3649 | |
| 3650 | EXPECT_FALSE(d.received_data_before_response()); |
| 3651 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3652 | } |
| 3653 | delete[] uploadBytes; |
| 3654 | } |
| 3655 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3656 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3657 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3658 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3659 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3660 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3661 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3662 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3663 | r->Start(); |
| 3664 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3665 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3666 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3667 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3668 | if (d.request_status() != OK) { |
| 3669 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 3670 | return false; |
| 3671 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3672 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3673 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3674 | } |
| 3675 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3676 | LocalHttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3677 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3678 | private: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3679 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3680 | }; |
| 3681 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3682 | namespace { |
| 3683 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3684 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3685 | const test_server::HttpRequest& request) { |
| 3686 | if (request.headers.find("Host") == request.headers.end() || |
| 3687 | request.headers.at("Host") != "www.redirect.com" || |
| 3688 | request.method != test_server::METHOD_CONNECT) { |
| 3689 | return nullptr; |
| 3690 | } |
| 3691 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3692 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3693 | new test_server::BasicHttpResponse); |
| 3694 | http_response->set_code(HTTP_FOUND); |
| 3695 | http_response->AddCustomHeader("Location", |
| 3696 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3697 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | } // namespace |
| 3701 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3702 | class TestSSLConfigService : public SSLConfigService { |
| 3703 | public: |
| 3704 | TestSSLConfigService(bool ev_enabled, |
| 3705 | bool online_rev_checking, |
| 3706 | bool rev_checking_required_local_anchors, |
| 3707 | bool token_binding_enabled) |
| 3708 | : ev_enabled_(ev_enabled), |
| 3709 | online_rev_checking_(online_rev_checking), |
| 3710 | rev_checking_required_local_anchors_( |
| 3711 | rev_checking_required_local_anchors), |
| 3712 | token_binding_enabled_(token_binding_enabled), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3713 | min_version_(kDefaultSSLVersionMin), |
| 3714 | max_version_(kDefaultSSLVersionMax) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3715 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3716 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3717 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3718 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3719 | // SSLConfigService: |
| 3720 | void GetSSLConfig(SSLConfig* config) override { |
| 3721 | *config = SSLConfig(); |
| 3722 | config->rev_checking_enabled = online_rev_checking_; |
| 3723 | config->verify_ev_cert = ev_enabled_; |
| 3724 | config->rev_checking_required_local_anchors = |
| 3725 | rev_checking_required_local_anchors_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3726 | config->version_min = min_version_; |
| 3727 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3728 | if (token_binding_enabled_) { |
| 3729 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3730 | } |
| 3731 | } |
| 3732 | |
| 3733 | protected: |
| 3734 | ~TestSSLConfigService() override {} |
| 3735 | |
| 3736 | private: |
| 3737 | const bool ev_enabled_; |
| 3738 | const bool online_rev_checking_; |
| 3739 | const bool rev_checking_required_local_anchors_; |
| 3740 | const bool token_binding_enabled_; |
| 3741 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3742 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3743 | }; |
| 3744 | |
| 3745 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3746 | #if !defined(OS_IOS) |
| 3747 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3748 | public: |
fdoray | 9f0b478 | 2017-02-10 12:55:47 | [diff] [blame] | 3749 | TokenBindingURLRequestTest() = default; |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3750 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3751 | void SetUp() override { |
| 3752 | default_context_.set_ssl_config_service( |
| 3753 | new TestSSLConfigService(false, false, false, true)); |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3754 | channel_id_service_.reset( |
| 3755 | new ChannelIDService(new DefaultChannelIDStore(NULL))); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3756 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3757 | URLRequestTestHTTP::SetUp(); |
| 3758 | } |
| 3759 | |
| 3760 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3761 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3762 | }; |
| 3763 | |
| 3764 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3765 | SpawnedTestServer::SSLOptions ssl_options; |
| 3766 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3767 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3768 | ssl_options, |
| 3769 | base::FilePath(kTestFilePath)); |
| 3770 | ASSERT_TRUE(https_test_server.Start()); |
| 3771 | |
| 3772 | TestDelegate d; |
| 3773 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3774 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3775 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d, |
| 3776 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3777 | r->Start(); |
| 3778 | EXPECT_TRUE(r->is_pending()); |
| 3779 | |
| 3780 | base::RunLoop().Run(); |
| 3781 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3782 | EXPECT_EQ(OK, d.request_status()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3783 | |
| 3784 | HttpRequestHeaders headers; |
| 3785 | std::string token_binding_header, token_binding_message; |
| 3786 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3787 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3788 | &token_binding_header)); |
| 3789 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3790 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3791 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3792 | std::vector<TokenBinding> token_bindings; |
| 3793 | ASSERT_TRUE( |
| 3794 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3795 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3796 | |
| 3797 | EXPECT_GT(d.bytes_received(), 0); |
| 3798 | std::string ekm = d.data_received(); |
| 3799 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3800 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3801 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3802 | token_bindings[0].signature, |
| 3803 | TokenBindingType::PROVIDED, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3804 | } |
| 3805 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3806 | |
| 3807 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3808 | SpawnedTestServer::SSLOptions ssl_options; |
| 3809 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3810 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3811 | ssl_options, |
| 3812 | base::FilePath(kTestFilePath)); |
| 3813 | ASSERT_TRUE(https_test_server.Start()); |
| 3814 | |
| 3815 | TestDelegate d; |
| 3816 | { |
| 3817 | GURL redirect_url = |
| 3818 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3819 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3820 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3821 | r->Start(); |
| 3822 | EXPECT_TRUE(r->is_pending()); |
| 3823 | |
| 3824 | base::RunLoop().Run(); |
| 3825 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3826 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3827 | |
| 3828 | HttpRequestHeaders headers; |
| 3829 | std::string token_binding_header, token_binding_message; |
| 3830 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3831 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3832 | &token_binding_header)); |
| 3833 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3834 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3835 | &token_binding_message)); |
| 3836 | std::vector<TokenBinding> token_bindings; |
| 3837 | ASSERT_TRUE( |
| 3838 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3839 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3840 | |
| 3841 | EXPECT_GT(d.bytes_received(), 0); |
| 3842 | std::string ekm = d.data_received(); |
| 3843 | |
| 3844 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3845 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3846 | token_bindings[0].signature, |
| 3847 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3848 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3849 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[1].ec_point, |
| 3850 | token_bindings[1].signature, |
| 3851 | TokenBindingType::REFERRED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3856 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3857 | #if !defined(OS_ANDROID) |
| 3858 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 3859 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, base::FilePath()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3860 | ASSERT_TRUE(http_server.Start()); |
| 3861 | SpawnedTestServer::SSLOptions ssl_options; |
| 3862 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3863 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3864 | ssl_options, |
| 3865 | base::FilePath(kTestFilePath)); |
| 3866 | ASSERT_TRUE(https_test_server.Start()); |
| 3867 | |
| 3868 | TestDelegate d; |
| 3869 | { |
| 3870 | GURL redirect_url = http_server.GetURL( |
| 3871 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3872 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3873 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3874 | r->Start(); |
| 3875 | EXPECT_TRUE(r->is_pending()); |
| 3876 | |
| 3877 | base::RunLoop().Run(); |
| 3878 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3879 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3880 | |
| 3881 | HttpRequestHeaders headers; |
| 3882 | std::string token_binding_header, token_binding_message; |
| 3883 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3884 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3885 | &token_binding_header)); |
| 3886 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3887 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3888 | &token_binding_message)); |
| 3889 | std::vector<TokenBinding> token_bindings; |
| 3890 | ASSERT_TRUE( |
| 3891 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3892 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3893 | |
| 3894 | EXPECT_GT(d.bytes_received(), 0); |
| 3895 | std::string ekm = d.data_received(); |
| 3896 | |
| 3897 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3898 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3899 | token_bindings[0].signature, |
| 3900 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 3905 | // 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] | 3906 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3907 | // the redirect. |
| 3908 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3909 | SpawnedTestServer::SSLOptions ssl_options; |
| 3910 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3911 | ssl_options, |
| 3912 | base::FilePath(kTestFilePath)); |
| 3913 | ASSERT_TRUE(https_test_server.Start()); |
| 3914 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3915 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3916 | ssl_options, |
| 3917 | base::FilePath(kTestFilePath)); |
| 3918 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3919 | |
| 3920 | TestDelegate d; |
| 3921 | { |
| 3922 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3923 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3924 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3925 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3926 | r->Start(); |
| 3927 | EXPECT_TRUE(r->is_pending()); |
| 3928 | |
| 3929 | base::RunLoop().Run(); |
| 3930 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3931 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3932 | |
| 3933 | HttpRequestHeaders headers; |
| 3934 | std::string token_binding_header, token_binding_message; |
| 3935 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3936 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3937 | &token_binding_header)); |
| 3938 | } |
| 3939 | } |
| 3940 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3941 | #endif // !defined(OS_IOS) |
| 3942 | |
| 3943 | // 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] | 3944 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3945 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3946 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3947 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3948 | http_test_server()->RegisterRequestHandler( |
| 3949 | base::Bind(&HandleRedirectConnect)); |
| 3950 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3951 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3952 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3953 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3954 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3955 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3956 | TestDelegate d; |
| 3957 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3958 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3959 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3960 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3961 | r->Start(); |
| 3962 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3963 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3964 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3965 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3966 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 3967 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3968 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3969 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3970 | // We should not have followed the redirect. |
| 3971 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3972 | } |
| 3973 | } |
| 3974 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3975 | // This is the same as the previous test, but checks that the network delegate |
| 3976 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3977 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3978 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3979 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3980 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3981 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3982 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3983 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3984 | TestDelegate d; |
| 3985 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3986 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3987 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3988 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3989 | r->Start(); |
| 3990 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3991 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3992 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3993 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3994 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 3995 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3996 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3997 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3998 | // We should not have followed the redirect. |
| 3999 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4000 | |
| 4001 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4002 | EXPECT_THAT(network_delegate.last_error(), |
| 4003 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4004 | } |
| 4005 | } |
| 4006 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4007 | // Tests that we can block and asynchronously return OK in various stages. |
| 4008 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4009 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4010 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4011 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4012 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4013 | }; |
| 4014 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 4015 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4016 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4017 | |
| 4018 | TestDelegate d; |
| 4019 | BlockingNetworkDelegate network_delegate( |
| 4020 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4021 | network_delegate.set_block_on( |
| 4022 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4023 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4024 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4025 | |
| 4026 | TestURLRequestContext context(true); |
| 4027 | context.set_network_delegate(&network_delegate); |
| 4028 | context.Init(); |
| 4029 | |
| 4030 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4031 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4032 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4033 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4034 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4035 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4036 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4037 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4038 | EXPECT_EQ(blocking_stages[i], |
| 4039 | network_delegate.stage_blocked_for_callback()); |
| 4040 | network_delegate.DoCallback(OK); |
| 4041 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4042 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4043 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4044 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4045 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4046 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4047 | } |
| 4048 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4049 | } |
| 4050 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4051 | // Tests that the network delegate can block and cancel a request. |
| 4052 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4053 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4054 | |
| 4055 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4056 | BlockingNetworkDelegate network_delegate( |
| 4057 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4058 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4059 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4060 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4061 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4062 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4063 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4064 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4065 | std::unique_ptr<URLRequest> r( |
| 4066 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4067 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4068 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4069 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4070 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4071 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4072 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4073 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4074 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4075 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4076 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4077 | } |
| 4078 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4079 | } |
| 4080 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4081 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4082 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4083 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4084 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4085 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4086 | BlockingNetworkDelegate::Stage stage, |
| 4087 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4088 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4089 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4090 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4091 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4092 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4093 | TestURLRequestContext context(true); |
| 4094 | context.set_network_delegate(&network_delegate); |
| 4095 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4096 | |
| 4097 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4098 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4099 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4100 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4101 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4102 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4103 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4104 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4105 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4106 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4107 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4108 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4109 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4110 | } else { |
| 4111 | NOTREACHED(); |
| 4112 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4113 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4114 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4115 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4116 | } |
| 4117 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4118 | } |
| 4119 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4120 | // The following 3 tests check that the network delegate can cancel a request |
| 4121 | // synchronously in various stages of the request. |
| 4122 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4123 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4124 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4125 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4126 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4130 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4131 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4132 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4133 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4137 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4138 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4139 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4140 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | // The following 3 tests check that the network delegate can cancel a request |
| 4144 | // asynchronously in various stages of the request. |
| 4145 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4146 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4147 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4148 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4149 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4153 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4154 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4155 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4156 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4160 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4161 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4162 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4163 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4164 | } |
| 4165 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4166 | // Tests that the network delegate can block and redirect a request to a new |
| 4167 | // URL. |
| 4168 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4169 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4170 | |
| 4171 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4172 | BlockingNetworkDelegate network_delegate( |
| 4173 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4174 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4175 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4176 | network_delegate.set_redirect_url(redirect_url); |
| 4177 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4178 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4179 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4180 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4181 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4182 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4183 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4184 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4185 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4186 | // Quit after hitting the redirect, so can check the headers. |
| 4187 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4188 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4189 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4190 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4191 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4192 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4193 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4194 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4195 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4196 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4197 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4198 | |
| 4199 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4200 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4201 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4202 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4203 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4204 | http_test_server()->host_port_pair()), |
| 4205 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4206 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4207 | // through an untunneled proxy. |
| 4208 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4209 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4210 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4211 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4212 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4213 | EXPECT_EQ(redirect_url, r->url()); |
| 4214 | EXPECT_EQ(original_url, r->original_url()); |
| 4215 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4216 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4217 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4218 | } |
| 4219 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4220 | } |
| 4221 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4222 | // Tests that the network delegate can block and redirect a request to a new |
| 4223 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4224 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4225 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4226 | |
| 4227 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4228 | BlockingNetworkDelegate network_delegate( |
| 4229 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4230 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4231 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4232 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4233 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4234 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4235 | |
| 4236 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4237 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4238 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4239 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4240 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4241 | // Quit after hitting the redirect, so can check the headers. |
| 4242 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4243 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4244 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4245 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4246 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4247 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4248 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4249 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4250 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4251 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4252 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4253 | |
| 4254 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4255 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4256 | base::RunLoop().Run(); |
| 4257 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4258 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4259 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4260 | http_test_server()->host_port_pair()), |
| 4261 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4262 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4263 | // through an untunneled proxy. |
| 4264 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4265 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4266 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4267 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4268 | EXPECT_EQ(redirect_url, r->url()); |
| 4269 | EXPECT_EQ(original_url, r->original_url()); |
| 4270 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4271 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4272 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4273 | } |
| 4274 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4275 | } |
| 4276 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4277 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4278 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4279 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4280 | |
| 4281 | const char kData[] = "hello world"; |
| 4282 | |
| 4283 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4284 | BlockingNetworkDelegate network_delegate( |
| 4285 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4286 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4287 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4288 | network_delegate.set_redirect_url(redirect_url); |
| 4289 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4290 | TestURLRequestContext context(true); |
| 4291 | context.set_network_delegate(&network_delegate); |
| 4292 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4293 | |
| 4294 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4295 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4296 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4297 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4298 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4299 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4300 | HttpRequestHeaders headers; |
| 4301 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 4302 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4303 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4304 | |
| 4305 | // Quit after hitting the redirect, so can check the headers. |
| 4306 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4307 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4308 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4309 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4310 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4311 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4312 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4313 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4314 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4315 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4316 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4317 | |
| 4318 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4319 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4320 | base::RunLoop().Run(); |
| 4321 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4322 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4323 | EXPECT_EQ(redirect_url, r->url()); |
| 4324 | EXPECT_EQ(original_url, r->original_url()); |
| 4325 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4326 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4327 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4328 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4329 | EXPECT_EQ(kData, d.data_received()); |
| 4330 | } |
| 4331 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4332 | } |
| 4333 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4334 | // Tests that the network delegate can block and redirect a request to a new |
| 4335 | // URL during OnHeadersReceived. |
| 4336 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4337 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4338 | |
| 4339 | TestDelegate d; |
| 4340 | BlockingNetworkDelegate network_delegate( |
| 4341 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4342 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4343 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4344 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4345 | |
| 4346 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4347 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4348 | |
| 4349 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4350 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4351 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4352 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4353 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4354 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4355 | base::RunLoop().Run(); |
| 4356 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4357 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4358 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4359 | http_test_server()->host_port_pair()), |
| 4360 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4361 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4362 | // through an untunneled proxy. |
| 4363 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4364 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4365 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4366 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4367 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4368 | EXPECT_EQ(redirect_url, r->url()); |
| 4369 | EXPECT_EQ(original_url, r->original_url()); |
| 4370 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4371 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4372 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4373 | } |
| 4374 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4375 | } |
| 4376 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4377 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4378 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4379 | // handle the challenge, and is passing the buck along to the |
| 4380 | // URLRequest::Delegate. |
| 4381 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4382 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4383 | |
| 4384 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4385 | BlockingNetworkDelegate network_delegate( |
| 4386 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4387 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4388 | TestURLRequestContext context(true); |
| 4389 | context.set_network_delegate(&network_delegate); |
| 4390 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4391 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4392 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4393 | |
| 4394 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4395 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4396 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4397 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4398 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4399 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4400 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4401 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4402 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4403 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4404 | EXPECT_TRUE(d.auth_required_called()); |
| 4405 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4406 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4407 | } |
| 4408 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4409 | } |
| 4410 | |
| 4411 | TEST_F(URLRequestTestHTTP, |
| 4412 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4413 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4414 | |
| 4415 | TestDelegate d; |
| 4416 | BlockingNetworkDelegate network_delegate( |
| 4417 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4418 | |
| 4419 | TestURLRequestContext context(true); |
| 4420 | context.set_network_delegate(&network_delegate); |
| 4421 | context.Init(); |
| 4422 | |
| 4423 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4424 | |
| 4425 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4426 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4427 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4428 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4429 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4430 | |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4431 | base::RunLoop().Run(); |
| 4432 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4433 | { |
| 4434 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4435 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4436 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4437 | } |
| 4438 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4439 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4440 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4441 | EXPECT_TRUE(d.auth_required_called()); |
| 4442 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4443 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4444 | } |
| 4445 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4446 | } |
| 4447 | |
| 4448 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4449 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4450 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4451 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4452 | |
| 4453 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4454 | BlockingNetworkDelegate network_delegate( |
| 4455 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4456 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4457 | network_delegate.set_auth_retval( |
| 4458 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4459 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4460 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4461 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4462 | TestURLRequestContext context(true); |
| 4463 | context.set_network_delegate(&network_delegate); |
| 4464 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4465 | |
| 4466 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4467 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4468 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4469 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4470 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4471 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4472 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4473 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4474 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4475 | EXPECT_FALSE(d.auth_required_called()); |
| 4476 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4477 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4478 | } |
| 4479 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4480 | } |
| 4481 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4482 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4483 | // headers (for the first or second request) when called at the proper times. |
| 4484 | TEST_F(URLRequestTestHTTP, |
| 4485 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4486 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4487 | |
| 4488 | TestDelegate d; |
| 4489 | BlockingNetworkDelegate network_delegate( |
| 4490 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4491 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4492 | network_delegate.set_auth_retval( |
| 4493 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4494 | |
| 4495 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4496 | |
| 4497 | TestURLRequestContext context(true); |
| 4498 | context.set_network_delegate(&network_delegate); |
| 4499 | context.Init(); |
| 4500 | |
| 4501 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4502 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4503 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4504 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4505 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4506 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4507 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4508 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4509 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4510 | EXPECT_FALSE(d.auth_required_called()); |
| 4511 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4512 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4513 | |
| 4514 | { |
| 4515 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4516 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4517 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4518 | } |
| 4519 | } |
| 4520 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4521 | } |
| 4522 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4523 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4524 | // by cancelling authentication. |
| 4525 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4526 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4527 | |
| 4528 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4529 | BlockingNetworkDelegate network_delegate( |
| 4530 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4531 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4532 | network_delegate.set_auth_retval( |
| 4533 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4534 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4535 | TestURLRequestContext context(true); |
| 4536 | context.set_network_delegate(&network_delegate); |
| 4537 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4538 | |
| 4539 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4540 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4541 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4542 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4543 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4544 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4545 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4546 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4547 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4548 | EXPECT_FALSE(d.auth_required_called()); |
| 4549 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4550 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4551 | } |
| 4552 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4553 | } |
| 4554 | |
| 4555 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4556 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4557 | // to handle the challenge, and is passing the buck along to the |
| 4558 | // URLRequest::Delegate. |
| 4559 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4560 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4561 | |
| 4562 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4563 | BlockingNetworkDelegate network_delegate( |
| 4564 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4565 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4566 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4567 | TestURLRequestContext context(true); |
| 4568 | context.set_network_delegate(&network_delegate); |
| 4569 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4570 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4571 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4572 | |
| 4573 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4574 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4575 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4576 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4577 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4578 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4579 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4580 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4581 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4582 | EXPECT_TRUE(d.auth_required_called()); |
| 4583 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4584 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4585 | } |
| 4586 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4587 | } |
| 4588 | |
| 4589 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4590 | // by setting credentials. |
| 4591 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4592 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4593 | |
| 4594 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4595 | BlockingNetworkDelegate network_delegate( |
| 4596 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4597 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4598 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4599 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4600 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4601 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4602 | network_delegate.set_auth_credentials(auth_credentials); |
| 4603 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4604 | TestURLRequestContext context(true); |
| 4605 | context.set_network_delegate(&network_delegate); |
| 4606 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4607 | |
| 4608 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4609 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4610 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4611 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4612 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4613 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4614 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4615 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4616 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4617 | EXPECT_FALSE(d.auth_required_called()); |
| 4618 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4619 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4620 | } |
| 4621 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4622 | } |
| 4623 | |
| 4624 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4625 | // by cancelling authentication. |
| 4626 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4627 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4628 | |
| 4629 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4630 | BlockingNetworkDelegate network_delegate( |
| 4631 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4632 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4633 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4634 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4635 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4636 | TestURLRequestContext context(true); |
| 4637 | context.set_network_delegate(&network_delegate); |
| 4638 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4639 | |
| 4640 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4641 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4642 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4643 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4644 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4645 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4646 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4647 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4648 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4649 | EXPECT_FALSE(d.auth_required_called()); |
| 4650 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4651 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4652 | } |
| 4653 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4654 | } |
| 4655 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4656 | // Tests that we can handle when a network request was canceled while we were |
| 4657 | // waiting for the network delegate. |
| 4658 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4659 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4660 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4661 | |
| 4662 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4663 | BlockingNetworkDelegate network_delegate( |
| 4664 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4665 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4666 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4667 | TestURLRequestContext context(true); |
| 4668 | context.set_network_delegate(&network_delegate); |
| 4669 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4670 | |
| 4671 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4672 | std::unique_ptr<URLRequest> r( |
| 4673 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4674 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4675 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4676 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4677 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4678 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4679 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4680 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4681 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4682 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4683 | // Ensure that network delegate is notified. |
| 4684 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4685 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4686 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4687 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4688 | } |
| 4689 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4690 | } |
| 4691 | |
| 4692 | // Tests that we can handle when a network request was canceled while we were |
| 4693 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4694 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4695 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4696 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4697 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4698 | |
| 4699 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4700 | BlockingNetworkDelegate network_delegate( |
| 4701 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4702 | network_delegate.set_block_on( |
| 4703 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4704 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4705 | TestURLRequestContext context(true); |
| 4706 | context.set_network_delegate(&network_delegate); |
| 4707 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4708 | |
| 4709 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4710 | std::unique_ptr<URLRequest> r( |
| 4711 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4712 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4713 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4714 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4715 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4716 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4717 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4718 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4719 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4720 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4721 | // Ensure that network delegate is notified. |
| 4722 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4723 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4724 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4725 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4726 | } |
| 4727 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4728 | } |
| 4729 | |
| 4730 | // Tests that we can handle when a network request was canceled while we were |
| 4731 | // waiting for the network delegate. |
| 4732 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4733 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4734 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4735 | |
| 4736 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4737 | BlockingNetworkDelegate network_delegate( |
| 4738 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4739 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4740 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4741 | TestURLRequestContext context(true); |
| 4742 | context.set_network_delegate(&network_delegate); |
| 4743 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4744 | |
| 4745 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4746 | std::unique_ptr<URLRequest> r( |
| 4747 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4748 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4749 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4750 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4751 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4752 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4753 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4754 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4755 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4756 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4757 | // Ensure that network delegate is notified. |
| 4758 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4759 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4760 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4761 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4762 | } |
| 4763 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4764 | } |
| 4765 | |
| 4766 | // Tests that we can handle when a network request was canceled while we were |
| 4767 | // waiting for the network delegate. |
| 4768 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4769 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4770 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4771 | |
| 4772 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4773 | BlockingNetworkDelegate network_delegate( |
| 4774 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4775 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4776 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4777 | TestURLRequestContext context(true); |
| 4778 | context.set_network_delegate(&network_delegate); |
| 4779 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4780 | |
| 4781 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4782 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4783 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 4784 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4785 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4786 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4787 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4788 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4789 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4790 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4791 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4792 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4793 | // Ensure that network delegate is notified. |
| 4794 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4795 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4796 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4797 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4798 | } |
| 4799 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4800 | } |
| 4801 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4802 | namespace { |
| 4803 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4804 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4805 | const test_server::HttpRequest& request) { |
| 4806 | if (request.headers.find("Host") == request.headers.end() || |
| 4807 | request.headers.at("Host") != "www.server-auth.com" || |
| 4808 | request.method != test_server::METHOD_CONNECT) { |
| 4809 | return nullptr; |
| 4810 | } |
| 4811 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4812 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4813 | new test_server::BasicHttpResponse); |
| 4814 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4815 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4816 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4817 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | } // namespace |
| 4821 | |
| 4822 | // 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] | 4823 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4824 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4825 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4826 | http_test_server()->RegisterRequestHandler( |
| 4827 | base::Bind(&HandleServerAuthConnect)); |
| 4828 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4829 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4830 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4831 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4832 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4833 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4834 | TestDelegate d; |
| 4835 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4836 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4837 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 4838 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4839 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4840 | r->Start(); |
| 4841 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4842 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4843 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4844 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4845 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4846 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4847 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4851 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4852 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4853 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4854 | TestDelegate d; |
| 4855 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4856 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4857 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4858 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4859 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4860 | r->Start(); |
| 4861 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4862 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4863 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4864 | |
| 4865 | EXPECT_EQ(1, d.response_started_count()); |
| 4866 | EXPECT_FALSE(d.received_data_before_response()); |
| 4867 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4868 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4869 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4870 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4871 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4872 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4873 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4874 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4875 | } |
| 4876 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4877 | // This test has the server send a large number of cookies to the client. |
| 4878 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4879 | // search is used to estimate that maximum number of cookies that are accepted |
| 4880 | // by the browser. Beyond the maximum number, the request will fail with |
| 4881 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4882 | #if defined(OS_WIN) |
| 4883 | // http://crbug.com/177916 |
| 4884 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4885 | #else |
| 4886 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4887 | #endif // defined(OS_WIN) |
| 4888 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4889 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4890 | |
| 4891 | int lower_bound = 0; |
| 4892 | int upper_bound = 1; |
| 4893 | |
| 4894 | // Double the number of cookies until the response header limits are |
| 4895 | // exceeded. |
| 4896 | while (DoManyCookiesRequest(upper_bound)) { |
| 4897 | lower_bound = upper_bound; |
| 4898 | upper_bound *= 2; |
| 4899 | ASSERT_LT(upper_bound, 1000000); |
| 4900 | } |
| 4901 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4902 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4903 | if (tolerance < 2) |
| 4904 | tolerance = 2; |
| 4905 | |
| 4906 | // Perform a binary search to find the highest possible number of cookies, |
| 4907 | // within the desired tolerance. |
| 4908 | while (upper_bound - lower_bound >= tolerance) { |
| 4909 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4910 | |
| 4911 | if (DoManyCookiesRequest(num_cookies)) |
| 4912 | lower_bound = num_cookies; |
| 4913 | else |
| 4914 | upper_bound = num_cookies; |
| 4915 | } |
| 4916 | // Success: the test did not crash. |
| 4917 | } |
| 4918 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4919 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4920 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4921 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4922 | TestDelegate d; |
| 4923 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4924 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4925 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4926 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4927 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4928 | r->Start(); |
| 4929 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4930 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4931 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4932 | |
| 4933 | EXPECT_EQ(1, d.response_started_count()); |
| 4934 | EXPECT_FALSE(d.received_data_before_response()); |
| 4935 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4936 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4937 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4938 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4939 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4940 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4941 | } |
| 4942 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4943 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4944 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4945 | |
| 4946 | TestDelegate d; |
| 4947 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4948 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4949 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4950 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4951 | |
| 4952 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4953 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4954 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4955 | r->Start(); |
| 4956 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4957 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4958 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4959 | |
| 4960 | EXPECT_EQ(1, d.response_started_count()); |
| 4961 | EXPECT_FALSE(d.received_data_before_response()); |
| 4962 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4963 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4964 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4965 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4966 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4967 | |
| 4968 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4969 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4970 | } |
| 4971 | } |
| 4972 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4973 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4974 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4975 | |
| 4976 | TestDelegate d; |
| 4977 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4978 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4979 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4980 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4981 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4982 | r->Start(); |
| 4983 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4984 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4985 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4986 | |
| 4987 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4988 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4989 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4990 | |
| 4991 | EXPECT_EQ(1, d.response_started_count()); |
| 4992 | EXPECT_FALSE(d.received_data_before_response()); |
| 4993 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4994 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4995 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4996 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4997 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4998 | } |
| 4999 | } |
| 5000 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5001 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5002 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5003 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5004 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5005 | base::FilePath(kTestFilePath)); |
| 5006 | |
| 5007 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5008 | |
| 5009 | // Parameter that specifies the Content-Length field in the response: |
| 5010 | // C - Compressed length. |
| 5011 | // U - Uncompressed length. |
| 5012 | // L - Large length (larger than both C & U). |
| 5013 | // M - Medium length (between C & U). |
| 5014 | // S - Small length (smaller than both C & U). |
| 5015 | const char test_parameters[] = "CULMS"; |
| 5016 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 5017 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5018 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5019 | // S has too little data, but we seem to accept it. |
| 5020 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5021 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5022 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5023 | base::FilePath file_path; |
| 5024 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 5025 | file_path = file_path.Append(kTestFilePath); |
| 5026 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5027 | std::string expected_content; |
| 5028 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5029 | |
| 5030 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5031 | TestDelegate d; |
| 5032 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5033 | std::string test_file = base::StringPrintf( |
| 5034 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5035 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5036 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5037 | TestURLRequestContext context(true); |
| 5038 | context.set_network_delegate(&network_delegate); |
| 5039 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5040 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5041 | std::unique_ptr<URLRequest> r( |
| 5042 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5043 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5044 | r->Start(); |
| 5045 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5046 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5047 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5048 | |
| 5049 | EXPECT_EQ(1, d.response_started_count()); |
| 5050 | EXPECT_FALSE(d.received_data_before_response()); |
| 5051 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5052 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5053 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5054 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5055 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5056 | if (test_parameters[i] == 'S') { |
| 5057 | // When content length is smaller than both compressed length and |
| 5058 | // uncompressed length, HttpStreamParser might not read the full |
| 5059 | // response body. |
| 5060 | continue; |
| 5061 | } |
| 5062 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5063 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5064 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5065 | << " Parameter = \"" << test_file << "\""; |
| 5066 | } |
| 5067 | } |
| 5068 | } |
| 5069 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5070 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5071 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5072 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5073 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5074 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5075 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5076 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5077 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5078 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5079 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5080 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5081 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5082 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5083 | |
| 5084 | EXPECT_EQ(1, d.response_started_count()); |
| 5085 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5086 | EXPECT_EQ(destination_url, req->url()); |
| 5087 | EXPECT_EQ(original_url, req->original_url()); |
| 5088 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5089 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5090 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5091 | |
| 5092 | LoadTimingInfo load_timing_info_before_redirect; |
| 5093 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5094 | &load_timing_info_before_redirect)); |
| 5095 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5096 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5097 | |
| 5098 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5099 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5100 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5101 | |
| 5102 | // Check that a new socket was used on redirect, since the server does not |
| 5103 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5104 | // before the ones recorded for the second request. |
| 5105 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5106 | load_timing_info.socket_log_id); |
| 5107 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5108 | load_timing_info.connect_timing.connect_start); |
| 5109 | } |
| 5110 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5111 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5112 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5113 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5114 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5115 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5116 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5117 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5118 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5119 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5120 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5121 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5122 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5123 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5124 | |
| 5125 | EXPECT_EQ(1, d.response_started_count()); |
| 5126 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5127 | EXPECT_EQ(destination_url, req->url()); |
| 5128 | EXPECT_EQ(original_url, req->original_url()); |
| 5129 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5130 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5131 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5132 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5133 | } |
| 5134 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5135 | // First and second pieces of information logged by delegates to URLRequests. |
| 5136 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5137 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5138 | |
| 5139 | // Logs delegate information to a URLRequest. The first string is logged |
| 5140 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5141 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5142 | // another asynchronous call is used to clear the delegate information |
| 5143 | // before calling a callback. The object then deletes itself. |
| 5144 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5145 | public: |
| 5146 | typedef base::Callback<void()> Callback; |
| 5147 | |
| 5148 | // Each time delegate information is added to the URLRequest, the resulting |
| 5149 | // load state is checked. The expected load state after each request is |
| 5150 | // passed in as an argument. |
| 5151 | static void Run(URLRequest* url_request, |
| 5152 | LoadState expected_first_load_state, |
| 5153 | LoadState expected_second_load_state, |
| 5154 | LoadState expected_third_load_state, |
| 5155 | const Callback& callback) { |
| 5156 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 5157 | url_request, |
| 5158 | expected_first_load_state, |
| 5159 | expected_second_load_state, |
| 5160 | expected_third_load_state, |
| 5161 | callback); |
| 5162 | logger->Start(); |
| 5163 | } |
| 5164 | |
| 5165 | // Checks that the log entries, starting with log_position, contain the |
| 5166 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5167 | // recorded. Returns the index of entry after the expected number of |
| 5168 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5169 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5170 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5171 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5172 | if (log_position + 3 >= entries.size()) { |
| 5173 | ADD_FAILURE() << "Not enough log entries"; |
| 5174 | return entries.size(); |
| 5175 | } |
| 5176 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5177 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5178 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5179 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5180 | &delegate_info)); |
| 5181 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5182 | |
| 5183 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5184 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5185 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5186 | |
| 5187 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5188 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5189 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5190 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5191 | &delegate_info)); |
| 5192 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5193 | |
| 5194 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5195 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5196 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5197 | |
| 5198 | return log_position + 1; |
| 5199 | } |
| 5200 | |
| 5201 | private: |
| 5202 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5203 | |
| 5204 | AsyncDelegateLogger(URLRequest* url_request, |
| 5205 | LoadState expected_first_load_state, |
| 5206 | LoadState expected_second_load_state, |
| 5207 | LoadState expected_third_load_state, |
| 5208 | const Callback& callback) |
| 5209 | : url_request_(url_request), |
| 5210 | expected_first_load_state_(expected_first_load_state), |
| 5211 | expected_second_load_state_(expected_second_load_state), |
| 5212 | expected_third_load_state_(expected_third_load_state), |
| 5213 | callback_(callback) { |
| 5214 | } |
| 5215 | |
| 5216 | ~AsyncDelegateLogger() {} |
| 5217 | |
| 5218 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5219 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5220 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5221 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5222 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5223 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5224 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5228 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5229 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5230 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5231 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5232 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5233 | } else { |
| 5234 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5235 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5236 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5237 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5241 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5242 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5243 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5244 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5245 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5246 | callback_.Run(); |
| 5247 | } |
| 5248 | |
| 5249 | URLRequest* url_request_; |
| 5250 | const int expected_first_load_state_; |
| 5251 | const int expected_second_load_state_; |
| 5252 | const int expected_third_load_state_; |
| 5253 | const Callback callback_; |
| 5254 | |
| 5255 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5256 | }; |
| 5257 | |
| 5258 | // NetworkDelegate that logs delegate information before a request is started, |
| 5259 | // before headers are sent, when headers are read, and when auth information |
| 5260 | // is requested. Uses AsyncDelegateLogger. |
| 5261 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5262 | public: |
| 5263 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5264 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5265 | |
| 5266 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5267 | int OnBeforeURLRequest(URLRequest* request, |
| 5268 | const CompletionCallback& callback, |
| 5269 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5270 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 5271 | return RunCallbackAsynchronously(request, callback); |
| 5272 | } |
| 5273 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5274 | int OnBeforeStartTransaction(URLRequest* request, |
| 5275 | const CompletionCallback& callback, |
| 5276 | HttpRequestHeaders* headers) override { |
| 5277 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5278 | return RunCallbackAsynchronously(request, callback); |
| 5279 | } |
| 5280 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5281 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5282 | URLRequest* request, |
| 5283 | const CompletionCallback& callback, |
| 5284 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5285 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5286 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5287 | TestNetworkDelegate::OnHeadersReceived(request, |
| 5288 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5289 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5290 | override_response_headers, |
| 5291 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5292 | return RunCallbackAsynchronously(request, callback); |
| 5293 | } |
| 5294 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5295 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5296 | URLRequest* request, |
| 5297 | const AuthChallengeInfo& auth_info, |
| 5298 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5299 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5300 | AsyncDelegateLogger::Run( |
| 5301 | request, |
| 5302 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5303 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5304 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5305 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5306 | callback, credentials)); |
| 5307 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5308 | } |
| 5309 | |
| 5310 | private: |
| 5311 | static int RunCallbackAsynchronously( |
| 5312 | URLRequest* request, |
| 5313 | const CompletionCallback& callback) { |
| 5314 | AsyncDelegateLogger::Run( |
| 5315 | request, |
| 5316 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5317 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5318 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5319 | base::Bind(callback, OK)); |
| 5320 | return ERR_IO_PENDING; |
| 5321 | } |
| 5322 | |
| 5323 | static void SetAuthAndResume(const AuthCallback& callback, |
| 5324 | AuthCredentials* credentials) { |
| 5325 | *credentials = AuthCredentials(kUser, kSecret); |
| 5326 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5327 | } |
| 5328 | |
| 5329 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5330 | }; |
| 5331 | |
| 5332 | // URLRequest::Delegate that logs delegate information when the headers |
| 5333 | // are received, when each read completes, and during redirects. Uses |
| 5334 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5335 | // |
| 5336 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5337 | // advancing to the next step in most cases, as well as cancellation. |
| 5338 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5339 | public: |
| 5340 | enum CancelStage { |
| 5341 | NO_CANCEL = 0, |
| 5342 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5343 | CANCEL_ON_RESPONSE_STARTED, |
| 5344 | CANCEL_ON_READ_COMPLETED |
| 5345 | }; |
| 5346 | |
| 5347 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5348 | : cancel_stage_(cancel_stage) { |
| 5349 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5350 | set_cancel_in_received_redirect(true); |
| 5351 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5352 | set_cancel_in_response_started(true); |
| 5353 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5354 | set_cancel_in_received_data(true); |
| 5355 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5356 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5357 | |
| 5358 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5359 | void OnReceivedRedirect(URLRequest* request, |
| 5360 | const RedirectInfo& redirect_info, |
| 5361 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5362 | *defer_redirect = true; |
| 5363 | AsyncDelegateLogger::Run( |
| 5364 | request, |
| 5365 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5366 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5367 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5368 | base::Bind( |
| 5369 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5370 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5371 | } |
| 5372 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5373 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5374 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5375 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5376 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5377 | base::Bind( |
| 5378 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5379 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5380 | } |
| 5381 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5382 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5383 | AsyncDelegateLogger::Run( |
| 5384 | request, |
| 5385 | LOAD_STATE_IDLE, |
| 5386 | LOAD_STATE_IDLE, |
| 5387 | LOAD_STATE_IDLE, |
| 5388 | base::Bind( |
| 5389 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5390 | base::Unretained(this), request, bytes_read)); |
| 5391 | } |
| 5392 | |
| 5393 | private: |
| 5394 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5395 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5396 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5397 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5398 | // FollowDeferredRedirect should not be called after cancellation. |
| 5399 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5400 | return; |
| 5401 | if (!defer_redirect) |
| 5402 | request->FollowDeferredRedirect(); |
| 5403 | } |
| 5404 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5405 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5406 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5407 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5411 | // The parent class continues the request. |
| 5412 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5413 | } |
| 5414 | |
| 5415 | const CancelStage cancel_stage_; |
| 5416 | |
| 5417 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5418 | }; |
| 5419 | |
| 5420 | // Tests handling of delegate info before a request starts. |
| 5421 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5422 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5423 | |
| 5424 | TestDelegate request_delegate; |
| 5425 | TestURLRequestContext context(true); |
| 5426 | context.set_network_delegate(NULL); |
| 5427 | context.set_net_log(&net_log_); |
| 5428 | context.Init(); |
| 5429 | |
| 5430 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5431 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5432 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5433 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5434 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5435 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5436 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5437 | |
| 5438 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5439 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5440 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5441 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5442 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5443 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5444 | |
| 5445 | base::RunLoop().Run(); |
| 5446 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5447 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5448 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5449 | } |
| 5450 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5451 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5452 | net_log_.GetEntries(&entries); |
| 5453 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5454 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5455 | |
| 5456 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5457 | |
| 5458 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5459 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5460 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5461 | } |
| 5462 | |
| 5463 | // Tests handling of delegate info from a network delegate. |
| 5464 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5465 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5466 | |
| 5467 | TestDelegate request_delegate; |
| 5468 | AsyncLoggingNetworkDelegate network_delegate; |
| 5469 | TestURLRequestContext context(true); |
| 5470 | context.set_network_delegate(&network_delegate); |
| 5471 | context.set_net_log(&net_log_); |
| 5472 | context.Init(); |
| 5473 | |
| 5474 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5475 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5476 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5477 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5478 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5479 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5480 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5482 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5483 | base::RunLoop().Run(); |
| 5484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5485 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5486 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5487 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5488 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5489 | } |
| 5490 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5491 | |
| 5492 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5493 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5494 | net_log_.GetEntries(&entries); |
| 5495 | for (size_t i = 0; i < 3; ++i) { |
| 5496 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5497 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5498 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5499 | |
| 5500 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5501 | log_position + 1); |
| 5502 | |
| 5503 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5504 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5505 | entries[log_position].type); |
| 5506 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5507 | } |
| 5508 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5509 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5510 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | // Tests handling of delegate info from a network delegate in the case of an |
| 5514 | // HTTP redirect. |
| 5515 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5516 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5517 | |
| 5518 | TestDelegate request_delegate; |
| 5519 | AsyncLoggingNetworkDelegate network_delegate; |
| 5520 | TestURLRequestContext context(true); |
| 5521 | context.set_network_delegate(&network_delegate); |
| 5522 | context.set_net_log(&net_log_); |
| 5523 | context.Init(); |
| 5524 | |
| 5525 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5526 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5527 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5528 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5529 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5530 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5531 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5532 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5533 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5534 | base::RunLoop().Run(); |
| 5535 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5536 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5537 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5538 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5539 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5540 | } |
| 5541 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5542 | |
| 5543 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5544 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5545 | net_log_.GetEntries(&entries); |
| 5546 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5547 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5548 | for (size_t i = 0; i < 3; ++i) { |
| 5549 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5550 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5551 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5552 | |
| 5553 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5554 | log_position + 1); |
| 5555 | |
| 5556 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5557 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5558 | entries[log_position].type); |
| 5559 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5563 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5564 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5565 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5566 | |
| 5567 | // The NetworkDelegate logged information in the same three events as before. |
| 5568 | for (size_t i = 0; i < 3; ++i) { |
| 5569 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5570 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5571 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5572 | |
| 5573 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5574 | log_position + 1); |
| 5575 | |
| 5576 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5577 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5578 | entries[log_position].type); |
| 5579 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5580 | } |
| 5581 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5582 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5583 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5587 | // AUTH. |
| 5588 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5589 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5590 | |
| 5591 | TestDelegate request_delegate; |
| 5592 | AsyncLoggingNetworkDelegate network_delegate; |
| 5593 | TestURLRequestContext context(true); |
| 5594 | context.set_network_delegate(&network_delegate); |
| 5595 | context.set_net_log(&net_log_); |
| 5596 | context.Init(); |
| 5597 | |
| 5598 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5599 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5600 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5601 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5602 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5603 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5604 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5605 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5606 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5607 | base::RunLoop().Run(); |
| 5608 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5609 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5610 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5611 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5612 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5613 | } |
| 5614 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5615 | |
| 5616 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5617 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5618 | net_log_.GetEntries(&entries); |
| 5619 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5620 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5621 | // in |
| 5622 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5623 | for (size_t i = 0; i < 6; ++i) { |
| 5624 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5625 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5626 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5627 | |
| 5628 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5629 | log_position + 1); |
| 5630 | |
| 5631 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5632 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5633 | entries[log_position].type); |
| 5634 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5635 | } |
| 5636 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5637 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5638 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5639 | } |
| 5640 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5641 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5642 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5643 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5644 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5645 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5646 | base::FilePath(kTestFilePath)); |
| 5647 | |
| 5648 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5649 | |
| 5650 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5651 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5652 | TestURLRequestContext context(true); |
| 5653 | context.set_network_delegate(NULL); |
| 5654 | context.set_net_log(&net_log_); |
| 5655 | context.Init(); |
| 5656 | |
| 5657 | { |
| 5658 | // A chunked response with delays between chunks is used to make sure that |
| 5659 | // attempts by the URLRequest delegate to log information while reading the |
| 5660 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5661 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5662 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5663 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5664 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5665 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5666 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5667 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5668 | base::RunLoop().Run(); |
| 5669 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5670 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5671 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | } |
| 5673 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5674 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5675 | net_log_.GetEntries(&entries); |
| 5676 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5677 | size_t log_position = 0; |
| 5678 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5679 | // The delegate info should only have been logged on header complete. Other |
| 5680 | // times it should silently be ignored. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5681 | log_position = ExpectLogContainsSomewhereAfter( |
| 5682 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5683 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5684 | |
| 5685 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5686 | log_position + 1); |
| 5687 | |
| 5688 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5689 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5690 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5691 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5692 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5693 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5694 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5695 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5696 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5697 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5698 | |
| 5699 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5700 | // an HTTP redirect. |
| 5701 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5702 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5703 | |
| 5704 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5705 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5706 | TestURLRequestContext context(true); |
| 5707 | context.set_network_delegate(NULL); |
| 5708 | context.set_net_log(&net_log_); |
| 5709 | context.Init(); |
| 5710 | |
| 5711 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5712 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5713 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5714 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5715 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5716 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5717 | base::RunLoop().Run(); |
| 5718 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5719 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5720 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5721 | } |
| 5722 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5723 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5724 | net_log_.GetEntries(&entries); |
| 5725 | |
| 5726 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5727 | // OnResponseStarted. |
| 5728 | size_t log_position = 0; |
| 5729 | for (int i = 0; i < 2; ++i) { |
| 5730 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5731 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5732 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5733 | |
| 5734 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5735 | log_position + 1); |
| 5736 | |
| 5737 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5738 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5739 | entries[log_position].type); |
| 5740 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5741 | } |
| 5742 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5743 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5744 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5745 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5746 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5750 | // an HTTP redirect, with cancellation at various points. |
| 5751 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5752 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5753 | |
| 5754 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5755 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5756 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5757 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5758 | }; |
| 5759 | |
| 5760 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5761 | ++test_case) { |
| 5762 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5763 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 5764 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5765 | context.set_network_delegate(NULL); |
| 5766 | context.set_net_log(&net_log); |
| 5767 | context.Init(); |
| 5768 | |
| 5769 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5770 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5771 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5772 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5773 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5774 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5775 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5776 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5777 | } |
| 5778 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5779 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5780 | net_log.GetEntries(&entries); |
| 5781 | |
| 5782 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5783 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5784 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5785 | // still currently supported in that call. |
| 5786 | size_t log_position = 0; |
| 5787 | for (int i = 0; i < 2; ++i) { |
| 5788 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5789 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5790 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5791 | |
| 5792 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5793 | log_position + 1); |
| 5794 | |
| 5795 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5796 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5797 | entries[log_position].type); |
| 5798 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5799 | } |
| 5800 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5801 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5802 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5803 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5804 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5805 | } |
| 5806 | } |
| 5807 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5808 | namespace { |
| 5809 | |
| 5810 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5811 | const char kExtraValue[] = "fubar"; |
| 5812 | |
| 5813 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5814 | void OnReceivedRedirect(URLRequest* request, |
| 5815 | const RedirectInfo& redirect_info, |
| 5816 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5817 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5818 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5819 | } |
| 5820 | }; |
| 5821 | |
| 5822 | } // namespace |
| 5823 | |
| 5824 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5825 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5826 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5827 | GURL destination_url = |
| 5828 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5829 | GURL original_url = |
| 5830 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5831 | RedirectWithAdditionalHeadersDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5832 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5833 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5834 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5835 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5836 | |
| 5837 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5838 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5839 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5840 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5841 | EXPECT_FALSE(req->is_pending()); |
| 5842 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5843 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5844 | } |
| 5845 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5846 | namespace { |
| 5847 | |
| 5848 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5849 | |
| 5850 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5851 | void OnReceivedRedirect(URLRequest* request, |
| 5852 | const RedirectInfo& redirect_info, |
| 5853 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5854 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5855 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5856 | } |
| 5857 | }; |
| 5858 | |
| 5859 | } // namespace |
| 5860 | |
| 5861 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5862 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5863 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5864 | GURL destination_url = http_test_server()->GetURL( |
| 5865 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5866 | GURL original_url = |
| 5867 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5868 | RedirectWithHeaderRemovalDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5869 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5870 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5871 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5872 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5873 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5874 | |
| 5875 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5876 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5877 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5878 | EXPECT_FALSE(req->is_pending()); |
| 5879 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5880 | EXPECT_EQ("None", d.data_received()); |
| 5881 | } |
| 5882 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5883 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5884 | TestDelegate d; |
| 5885 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5886 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5887 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 5888 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5889 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5890 | r->Start(); |
| 5891 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5892 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5893 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5894 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5895 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5896 | |
| 5897 | // We expect to receive OnResponseStarted even though the request has been |
| 5898 | // cancelled. |
| 5899 | EXPECT_EQ(1, d.response_started_count()); |
| 5900 | EXPECT_EQ(0, d.bytes_received()); |
| 5901 | EXPECT_FALSE(d.received_data_before_response()); |
| 5902 | } |
| 5903 | } |
| 5904 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5905 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5906 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5907 | |
| 5908 | TestDelegate d; |
| 5909 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5910 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5911 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5912 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5913 | |
| 5914 | d.set_cancel_in_response_started(true); |
| 5915 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5916 | r->Start(); |
| 5917 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5918 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5919 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5920 | |
| 5921 | EXPECT_EQ(1, d.response_started_count()); |
| 5922 | EXPECT_EQ(0, d.bytes_received()); |
| 5923 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5924 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5925 | } |
| 5926 | } |
| 5927 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5928 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5929 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5930 | |
| 5931 | TestDelegate d; |
| 5932 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5933 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5934 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5935 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5936 | |
| 5937 | d.set_cancel_in_received_data(true); |
| 5938 | |
| 5939 | r->Start(); |
| 5940 | EXPECT_TRUE(r->is_pending()); |
| 5941 | |
| 5942 | base::RunLoop().Run(); |
| 5943 | |
| 5944 | EXPECT_EQ(1, d.response_started_count()); |
| 5945 | EXPECT_NE(0, d.received_bytes_count()); |
| 5946 | EXPECT_FALSE(d.received_data_before_response()); |
| 5947 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 5952 | ASSERT_TRUE(http_test_server()->Start()); |
| 5953 | |
| 5954 | TestDelegate d; |
| 5955 | { |
| 5956 | // This returns an empty response (With headers). |
| 5957 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5958 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5959 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5960 | |
| 5961 | d.set_cancel_in_received_data(true); |
| 5962 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5963 | r->Start(); |
| 5964 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5965 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5966 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5967 | |
| 5968 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5969 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5970 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5971 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5972 | } |
| 5973 | } |
| 5974 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5975 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5976 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5977 | |
| 5978 | TestDelegate d; |
| 5979 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5980 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5981 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5982 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5983 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5984 | r->Start(); |
| 5985 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5986 | |
| 5987 | // The request will be implicitly canceled when it is destroyed. The |
| 5988 | // test delegate must not post a quit message when this happens because |
| 5989 | // this test doesn't actually have a message loop. The quit message would |
| 5990 | // get put on this thread's message queue and the next test would exit |
| 5991 | // early, causing problems. |
| 5992 | d.set_quit_on_complete(false); |
| 5993 | } |
| 5994 | // expect things to just cleanup properly. |
| 5995 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 5996 | // 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] | 5997 | // message loop |
| 5998 | EXPECT_FALSE(d.received_data_before_response()); |
| 5999 | EXPECT_EQ(0, d.bytes_received()); |
| 6000 | } |
| 6001 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6002 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6003 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6004 | |
| 6005 | // populate cache |
| 6006 | { |
| 6007 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6008 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6009 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6010 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6011 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6012 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6013 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6014 | } |
| 6015 | |
| 6016 | // cancel read from cache (see bug 990242) |
| 6017 | { |
| 6018 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6019 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6020 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6021 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6022 | r->Start(); |
| 6023 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6024 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6025 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6026 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6027 | EXPECT_EQ(1, d.response_started_count()); |
| 6028 | EXPECT_EQ(0, d.bytes_received()); |
| 6029 | EXPECT_FALSE(d.received_data_before_response()); |
| 6030 | } |
| 6031 | } |
| 6032 | |
| 6033 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6034 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6035 | HTTPUploadDataOperationTest("POST"); |
| 6036 | } |
| 6037 | |
| 6038 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6039 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6040 | HTTPUploadDataOperationTest("PUT"); |
| 6041 | } |
| 6042 | |
| 6043 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6044 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6045 | |
| 6046 | TestDelegate d; |
| 6047 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6048 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6049 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6050 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6051 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6052 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6053 | r->Start(); |
| 6054 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6055 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6056 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6057 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6058 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6059 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6060 | |
| 6061 | EXPECT_FALSE(d.received_data_before_response()); |
| 6062 | EXPECT_TRUE(d.data_received().empty()); |
| 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6067 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6068 | |
| 6069 | TestDelegate d; |
| 6070 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6071 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6072 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6073 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6074 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6075 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6076 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6077 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6078 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6079 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6080 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6081 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6082 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6083 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6084 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6085 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6086 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6087 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6088 | std::numeric_limits<uint64_t>::max(), base::Time())); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6089 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 6090 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6091 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6092 | r->Start(); |
| 6093 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6094 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6095 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6096 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6097 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6098 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6099 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6100 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6101 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6102 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6103 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6104 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6105 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6106 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6107 | |
| 6108 | EXPECT_FALSE(d.received_data_before_response()); |
| 6109 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6110 | EXPECT_EQ(size, d.bytes_received()); |
| 6111 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6112 | } |
| 6113 | } |
| 6114 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6115 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6116 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6117 | |
| 6118 | TestDelegate d; |
| 6119 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6120 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6121 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6122 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6123 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6124 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6125 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6126 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6127 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6128 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6129 | base::FilePath(FILE_PATH_LITERAL( |
| 6130 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6131 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6132 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 6133 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6134 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6135 | r->Start(); |
| 6136 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6137 | |
| 6138 | base::RunLoop().Run(); |
| 6139 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6140 | EXPECT_TRUE(d.request_failed()); |
| 6141 | EXPECT_FALSE(d.received_data_before_response()); |
| 6142 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6143 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6144 | } |
| 6145 | } |
| 6146 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6147 | namespace { |
| 6148 | |
| 6149 | // Adds a standard set of data to an upload for chunked upload integration |
| 6150 | // tests. |
| 6151 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6152 | writer->AppendData("a", 1, false); |
| 6153 | writer->AppendData("bcd", 3, false); |
| 6154 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6155 | writer->AppendData("\r\n\r\n", 4, false); |
| 6156 | writer->AppendData("0", 1, false); |
| 6157 | writer->AppendData("2323", 4, true); |
| 6158 | } |
| 6159 | |
| 6160 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6161 | // the server. |
| 6162 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6163 | // This should match the chunks sent by AddChunksToUpload(). |
| 6164 | const std::string expected_data = |
| 6165 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6166 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6167 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6168 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6169 | |
| 6170 | EXPECT_FALSE(d->received_data_before_response()); |
| 6171 | |
| 6172 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6173 | EXPECT_EQ(expected_data, d->data_received()); |
| 6174 | } |
| 6175 | |
| 6176 | } // namespace |
| 6177 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6178 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6179 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6180 | |
| 6181 | TestDelegate d; |
| 6182 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6183 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6184 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6185 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6186 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6187 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6188 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6189 | upload_data_stream->CreateWriter(); |
| 6190 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6191 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6192 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6193 | r->Start(); |
| 6194 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6195 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6196 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6197 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6198 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6202 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6203 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6204 | |
| 6205 | TestDelegate d; |
| 6206 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6207 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6208 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6209 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6210 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6211 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6212 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6213 | upload_data_stream->CreateWriter(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6214 | r->set_upload(base::WrapUnique(upload_data_stream.release())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6215 | r->set_method("POST"); |
| 6216 | r->Start(); |
| 6217 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6218 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6219 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6220 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6221 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6222 | } |
| 6223 | } |
| 6224 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6225 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6226 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6227 | |
| 6228 | TestDelegate d; |
| 6229 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6230 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6231 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6232 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6233 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6234 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6235 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6236 | upload_data_stream->CreateWriter(); |
| 6237 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6238 | r->set_method("POST"); |
| 6239 | r->Start(); |
| 6240 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6241 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6242 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6243 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6244 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6245 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6246 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6247 | } |
| 6248 | } |
| 6249 | |
| 6250 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6251 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6252 | |
| 6253 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6254 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6255 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6256 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6257 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6258 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6259 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6260 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6261 | |
| 6262 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6263 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6264 | |
| 6265 | std::string header; |
| 6266 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6267 | EXPECT_EQ("private", header); |
| 6268 | |
| 6269 | header.clear(); |
| 6270 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6271 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6272 | |
| 6273 | // The response has two "X-Multiple-Entries" headers. |
| 6274 | // This verfies our output has them concatenated together. |
| 6275 | header.clear(); |
| 6276 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6277 | EXPECT_EQ("a, b", header); |
| 6278 | } |
| 6279 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6280 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6281 | // security state. (see http://crbug.com/550977). |
| 6282 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6283 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6284 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6285 | https_test_server.SetSSLConfig( |
| 6286 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6287 | https_test_server.ServeFilesFromSourceDirectory( |
| 6288 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6289 | ASSERT_TRUE(https_test_server.Start()); |
| 6290 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6291 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6292 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6293 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6294 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6295 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6296 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6297 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6298 | |
| 6299 | TransportSecurityState* security_state = |
| 6300 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6301 | TransportSecurityState::STSState sts_state; |
| 6302 | TransportSecurityState::PKPState pkp_state; |
| 6303 | EXPECT_TRUE( |
| 6304 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6305 | EXPECT_FALSE( |
| 6306 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6307 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6308 | sts_state.upgrade_mode); |
| 6309 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6310 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6311 | #if defined(OS_ANDROID) |
| 6312 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6313 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6314 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6315 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6316 | } |
| 6317 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6318 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6319 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6320 | https_test_server.ServeFilesFromSourceDirectory( |
| 6321 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6322 | ASSERT_TRUE(https_test_server.Start()); |
| 6323 | // Make sure this test fails if the test server is changed to not |
| 6324 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6325 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6326 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6327 | |
| 6328 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6329 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6330 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6331 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6332 | request->Start(); |
| 6333 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6334 | TransportSecurityState* security_state = |
| 6335 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6336 | TransportSecurityState::STSState sts_state; |
| 6337 | EXPECT_FALSE( |
| 6338 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6339 | } |
| 6340 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6341 | namespace { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6342 | const char kExpectCTStaticHostname[] = "preloaded-expect-ct.badssl.com"; |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 6343 | const char kExpectStapleStaticHostname[] = "preloaded-expect-staple.badssl.com"; |
| 6344 | const char kExpectStapleReportURI[] = "https://report.badssl.com/expect-staple"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6345 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 6346 | } // namespace |
| 6347 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6348 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 6349 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6350 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6351 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6352 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6353 | https_test_server.SetSSLConfig( |
| 6354 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6355 | https_test_server.ServeFilesFromSourceDirectory( |
| 6356 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6357 | ASSERT_TRUE(https_test_server.Start()); |
| 6358 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6359 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6360 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6361 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6362 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6363 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6364 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6365 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6366 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6367 | TransportSecurityState* security_state = |
| 6368 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6369 | TransportSecurityState::STSState sts_state; |
| 6370 | TransportSecurityState::PKPState pkp_state; |
| 6371 | EXPECT_FALSE( |
| 6372 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6373 | EXPECT_TRUE( |
| 6374 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6375 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6376 | sts_state.upgrade_mode); |
| 6377 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6378 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6379 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6380 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6381 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6382 | } |
| 6383 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6384 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6385 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6386 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6387 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6388 | https_test_server.SetSSLConfig( |
| 6389 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6390 | https_test_server.ServeFilesFromSourceDirectory( |
| 6391 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6392 | ASSERT_TRUE(https_test_server.Start()); |
| 6393 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6394 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6395 | |
| 6396 | // Set up a pin for |test_server_hostname|. |
| 6397 | TransportSecurityState security_state; |
| 6398 | const base::Time current_time(base::Time::Now()); |
| 6399 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6400 | HashValueVector hashes; |
| 6401 | HashValue hash1; |
| 6402 | HashValue hash2; |
| 6403 | // The values here don't matter, as long as they are different from |
| 6404 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6405 | ASSERT_TRUE( |
| 6406 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6407 | ASSERT_TRUE( |
| 6408 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6409 | hashes.push_back(hash1); |
| 6410 | hashes.push_back(hash2); |
| 6411 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6412 | false, /* include subdomains */ |
| 6413 | hashes, report_uri); |
| 6414 | |
| 6415 | MockCertificateReportSender mock_report_sender; |
| 6416 | security_state.SetReportSender(&mock_report_sender); |
| 6417 | |
| 6418 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6419 | // set pin. |
| 6420 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6421 | ASSERT_TRUE(cert); |
| 6422 | |
| 6423 | MockCertVerifier cert_verifier; |
| 6424 | CertVerifyResult verify_result; |
| 6425 | verify_result.verified_cert = cert; |
| 6426 | verify_result.is_issued_by_known_root = true; |
| 6427 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6428 | ASSERT_TRUE( |
| 6429 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6430 | verify_result.public_key_hashes.push_back(hash3); |
| 6431 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6432 | |
| 6433 | TestNetworkDelegate network_delegate; |
| 6434 | TestURLRequestContext context(true); |
| 6435 | context.set_transport_security_state(&security_state); |
| 6436 | context.set_network_delegate(&network_delegate); |
| 6437 | context.set_cert_verifier(&cert_verifier); |
| 6438 | context.Init(); |
| 6439 | |
| 6440 | // Now send a request to trigger the violation. |
| 6441 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6442 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6443 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 6444 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6445 | violating_request->Start(); |
| 6446 | base::RunLoop().Run(); |
| 6447 | |
| 6448 | // Check that a report was sent. |
| 6449 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6450 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6451 | EXPECT_EQ("application/json; charset=utf-8", |
| 6452 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6453 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6454 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6455 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6456 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6457 | base::DictionaryValue* report_dict; |
| 6458 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6459 | std::string report_hostname; |
| 6460 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6461 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6462 | } |
| 6463 | |
| 6464 | // Tests that reports get sent on requests with |
| 6465 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6466 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6467 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6468 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6469 | https_test_server.SetSSLConfig( |
| 6470 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6471 | https_test_server.ServeFilesFromSourceDirectory( |
| 6472 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6473 | ASSERT_TRUE(https_test_server.Start()); |
| 6474 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6475 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6476 | |
| 6477 | TransportSecurityState security_state; |
| 6478 | MockCertificateReportSender mock_report_sender; |
| 6479 | security_state.SetReportSender(&mock_report_sender); |
| 6480 | |
| 6481 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6482 | // header. |
| 6483 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6484 | ASSERT_TRUE(cert); |
| 6485 | |
| 6486 | MockCertVerifier cert_verifier; |
| 6487 | CertVerifyResult verify_result; |
| 6488 | verify_result.verified_cert = cert; |
| 6489 | verify_result.is_issued_by_known_root = true; |
| 6490 | HashValue hash; |
| 6491 | // This value doesn't matter, as long as it is different from the pins |
| 6492 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6493 | ASSERT_TRUE( |
| 6494 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6495 | verify_result.public_key_hashes.push_back(hash); |
| 6496 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6497 | |
| 6498 | TestNetworkDelegate network_delegate; |
| 6499 | TestURLRequestContext context(true); |
| 6500 | context.set_transport_security_state(&security_state); |
| 6501 | context.set_network_delegate(&network_delegate); |
| 6502 | context.set_cert_verifier(&cert_verifier); |
| 6503 | context.Init(); |
| 6504 | |
| 6505 | // Now send a request to trigger the violation. |
| 6506 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6507 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6508 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6509 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6510 | violating_request->Start(); |
| 6511 | base::RunLoop().Run(); |
| 6512 | |
| 6513 | // Check that a report was sent. |
| 6514 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6515 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6516 | EXPECT_EQ("application/json; charset=utf-8", |
| 6517 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6518 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6519 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6520 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6521 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6522 | base::DictionaryValue* report_dict; |
| 6523 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6524 | std::string report_hostname; |
| 6525 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6526 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6527 | } |
| 6528 | |
| 6529 | // Tests that reports do not get sent on requests with |
| 6530 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6531 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6532 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6533 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6534 | https_test_server.SetSSLConfig( |
| 6535 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6536 | https_test_server.ServeFilesFromSourceDirectory( |
| 6537 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6538 | ASSERT_TRUE(https_test_server.Start()); |
| 6539 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6540 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6541 | |
| 6542 | TransportSecurityState security_state; |
| 6543 | MockCertificateReportSender mock_report_sender; |
| 6544 | security_state.SetReportSender(&mock_report_sender); |
| 6545 | |
| 6546 | TestNetworkDelegate network_delegate; |
| 6547 | MockCertVerifier mock_cert_verifier; |
| 6548 | TestURLRequestContext context(true); |
| 6549 | context.set_transport_security_state(&security_state); |
| 6550 | context.set_network_delegate(&network_delegate); |
| 6551 | context.set_cert_verifier(&mock_cert_verifier); |
| 6552 | mock_cert_verifier.set_default_result(OK); |
| 6553 | context.Init(); |
| 6554 | |
| 6555 | // Now send a request that does not trigger the violation. |
| 6556 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6557 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6558 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6559 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6560 | request->Start(); |
| 6561 | base::RunLoop().Run(); |
| 6562 | |
| 6563 | // Check that a report was not sent. |
| 6564 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6565 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6566 | } |
| 6567 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6568 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6569 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6570 | https_test_server.ServeFilesFromSourceDirectory( |
| 6571 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6572 | ASSERT_TRUE(https_test_server.Start()); |
| 6573 | // Make sure this test fails if the test server is changed to not |
| 6574 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6575 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6576 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6577 | |
| 6578 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6579 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6580 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6581 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6582 | request->Start(); |
| 6583 | base::RunLoop().Run(); |
| 6584 | |
| 6585 | TransportSecurityState* security_state = |
| 6586 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6587 | TransportSecurityState::PKPState pkp_state; |
| 6588 | EXPECT_FALSE( |
| 6589 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6590 | } |
| 6591 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6592 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6593 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6594 | https_test_server.SetSSLConfig( |
| 6595 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6596 | https_test_server.ServeFilesFromSourceDirectory( |
| 6597 | base::FilePath(kTestFilePath)); |
| 6598 | ASSERT_TRUE(https_test_server.Start()); |
| 6599 | |
| 6600 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6601 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6602 | ASSERT_TRUE(cert); |
| 6603 | |
| 6604 | MockCertVerifier cert_verifier; |
| 6605 | CertVerifyResult verify_result; |
| 6606 | verify_result.verified_cert = cert; |
| 6607 | verify_result.is_issued_by_known_root = false; |
| 6608 | HashValue hash; |
| 6609 | ASSERT_TRUE( |
| 6610 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6611 | verify_result.public_key_hashes.push_back(hash); |
| 6612 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6613 | cert_verifier.set_default_result(OK); |
| 6614 | |
| 6615 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6616 | |
| 6617 | // Set up HPKP |
| 6618 | base::Time current_time = base::Time::Now(); |
| 6619 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6620 | HashValue pin; |
| 6621 | ASSERT_TRUE( |
| 6622 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6623 | HashValueVector hashes; |
| 6624 | hashes.push_back(pin); |
| 6625 | GURL report_uri(kHPKPReportUri); |
| 6626 | TransportSecurityState security_state; |
| 6627 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6628 | false, /* include subdomains */ |
| 6629 | hashes, report_uri); |
| 6630 | |
| 6631 | TestNetworkDelegate network_delegate; |
| 6632 | TestURLRequestContext context(true); |
| 6633 | context.set_transport_security_state(&security_state); |
| 6634 | context.set_network_delegate(&network_delegate); |
| 6635 | context.set_cert_verifier(&cert_verifier); |
| 6636 | context.Init(); |
| 6637 | |
| 6638 | TestDelegate d; |
| 6639 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6640 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6641 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6642 | request->Start(); |
| 6643 | base::RunLoop().Run(); |
| 6644 | |
| 6645 | TransportSecurityState::PKPState pkp_state; |
| 6646 | EXPECT_TRUE( |
| 6647 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6648 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6649 | } |
| 6650 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6651 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6652 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6653 | https_test_server.SetSSLConfig( |
| 6654 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6655 | https_test_server.ServeFilesFromSourceDirectory( |
| 6656 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6657 | ASSERT_TRUE(https_test_server.Start()); |
| 6658 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6659 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6660 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6661 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6662 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6663 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6664 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6665 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6666 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6667 | |
| 6668 | // We should have set parameters from the first header, not the second. |
| 6669 | TransportSecurityState* security_state = |
| 6670 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6671 | TransportSecurityState::STSState sts_state; |
| 6672 | EXPECT_TRUE( |
| 6673 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6674 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6675 | sts_state.upgrade_mode); |
| 6676 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6677 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6678 | } |
| 6679 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6680 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6681 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6682 | https_test_server.SetSSLConfig( |
| 6683 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6684 | https_test_server.ServeFilesFromSourceDirectory( |
| 6685 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6686 | ASSERT_TRUE(https_test_server.Start()); |
| 6687 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6688 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6689 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6690 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6691 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6692 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6693 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6694 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6695 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6696 | |
| 6697 | // We should have set parameters from the first header, not the second. |
| 6698 | TransportSecurityState* security_state = |
| 6699 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6700 | TransportSecurityState::STSState sts_state; |
| 6701 | TransportSecurityState::PKPState pkp_state; |
| 6702 | EXPECT_TRUE( |
| 6703 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6704 | EXPECT_TRUE( |
| 6705 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6706 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6707 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6708 | #if defined(OS_ANDROID) |
| 6709 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6710 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6711 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6712 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6713 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6714 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6715 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6716 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6717 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6718 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6719 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6720 | } |
| 6721 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6722 | // Tests that when multiple HPKP headers are present, asserting different |
| 6723 | // policies, that only the first such policy is processed. |
| 6724 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6725 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6726 | https_test_server.SetSSLConfig( |
| 6727 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6728 | https_test_server.ServeFilesFromSourceDirectory( |
| 6729 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6730 | ASSERT_TRUE(https_test_server.Start()); |
| 6731 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6732 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6733 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6734 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6735 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6736 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6737 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6738 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6739 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6740 | |
| 6741 | TransportSecurityState* security_state = |
| 6742 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6743 | TransportSecurityState::STSState sts_state; |
| 6744 | TransportSecurityState::PKPState pkp_state; |
| 6745 | EXPECT_TRUE( |
| 6746 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6747 | EXPECT_TRUE( |
| 6748 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6749 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6750 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6751 | #if defined(OS_ANDROID) |
| 6752 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6753 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6754 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6755 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6756 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6757 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6758 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6759 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6760 | } |
| 6761 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6762 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6763 | // called. |
| 6764 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6765 | public: |
| 6766 | MockExpectCTReporter() : num_failures_(0) {} |
| 6767 | ~MockExpectCTReporter() override {} |
| 6768 | |
| 6769 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6770 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6771 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6772 | const X509Certificate* validated_certificate_chain, |
| 6773 | const X509Certificate* served_certificate_chain, |
| 6774 | const SignedCertificateTimestampAndStatusList& |
| 6775 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6776 | num_failures_++; |
| 6777 | } |
| 6778 | |
| 6779 | uint32_t num_failures() { return num_failures_; } |
| 6780 | |
| 6781 | private: |
| 6782 | uint32_t num_failures_; |
| 6783 | }; |
| 6784 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6785 | // A CTPolicyEnforcer that returns a default CertPolicyCompliance value |
| 6786 | // for every certificate. |
| 6787 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6788 | public: |
| 6789 | MockCTPolicyEnforcer() |
| 6790 | : default_result_( |
| 6791 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS) {} |
| 6792 | ~MockCTPolicyEnforcer() override {} |
| 6793 | |
| 6794 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 6795 | X509Certificate* cert, |
| 6796 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6797 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6798 | return default_result_; |
| 6799 | } |
| 6800 | |
| 6801 | void set_default_result(ct::CertPolicyCompliance default_result) { |
| 6802 | default_result_ = default_result; |
| 6803 | } |
| 6804 | |
| 6805 | private: |
| 6806 | ct::CertPolicyCompliance default_result_; |
| 6807 | }; |
| 6808 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6809 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 6810 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 6811 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 6812 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 6813 | #endif |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6814 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6815 | https_test_server.SetSSLConfig( |
| 6816 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6817 | https_test_server.ServeFilesFromSourceDirectory( |
| 6818 | base::FilePath(kTestFilePath)); |
| 6819 | ASSERT_TRUE(https_test_server.Start()); |
| 6820 | |
| 6821 | MockExpectCTReporter reporter; |
| 6822 | TransportSecurityState transport_security_state; |
| 6823 | transport_security_state.enable_static_expect_ct_ = true; |
| 6824 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6825 | |
| 6826 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6827 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6828 | ASSERT_TRUE(cert); |
| 6829 | MockCertVerifier cert_verifier; |
| 6830 | CertVerifyResult verify_result; |
| 6831 | verify_result.verified_cert = cert; |
| 6832 | verify_result.is_issued_by_known_root = true; |
| 6833 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6834 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 6835 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 6836 | // CT violation. |
| 6837 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6838 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6839 | ct_policy_enforcer.set_default_result( |
| 6840 | ct::CertPolicyCompliance::CERT_POLICY_NOT_ENOUGH_SCTS); |
| 6841 | |
| 6842 | TestNetworkDelegate network_delegate; |
| 6843 | // Use a MockHostResolver (which by default maps all hosts to |
| 6844 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6845 | // CT preload list. |
| 6846 | MockHostResolver host_resolver; |
| 6847 | TestURLRequestContext context(true); |
| 6848 | context.set_host_resolver(&host_resolver); |
| 6849 | context.set_transport_security_state(&transport_security_state); |
| 6850 | context.set_network_delegate(&network_delegate); |
| 6851 | context.set_cert_verifier(&cert_verifier); |
| 6852 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 6853 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6854 | context.Init(); |
| 6855 | |
| 6856 | // Now send a request to trigger the violation. |
| 6857 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6858 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6859 | GURL::Replacements replace_host; |
| 6860 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6861 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6862 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 6863 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6864 | violating_request->Start(); |
| 6865 | base::RunLoop().Run(); |
| 6866 | |
| 6867 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6868 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6869 | |
| 6870 | // Tests that Expect CT HTTP headers are processed correctly. |
| 6871 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6872 | base::test::ScopedFeatureList feature_list; |
| 6873 | feature_list.InitAndEnableFeature( |
| 6874 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6875 | |
| 6876 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6877 | https_test_server.SetSSLConfig( |
| 6878 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6879 | https_test_server.ServeFilesFromSourceDirectory( |
| 6880 | base::FilePath(kTestFilePath)); |
| 6881 | ASSERT_TRUE(https_test_server.Start()); |
| 6882 | |
| 6883 | MockExpectCTReporter reporter; |
| 6884 | TransportSecurityState transport_security_state; |
| 6885 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6886 | |
| 6887 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6888 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6889 | ASSERT_TRUE(cert); |
| 6890 | MockCertVerifier cert_verifier; |
| 6891 | CertVerifyResult verify_result; |
| 6892 | verify_result.verified_cert = cert; |
| 6893 | verify_result.is_issued_by_known_root = true; |
| 6894 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6895 | |
| 6896 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6897 | // compliance. |
| 6898 | DoNothingCTVerifier ct_verifier; |
| 6899 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6900 | ct_policy_enforcer.set_default_result( |
| 6901 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6902 | |
| 6903 | TestNetworkDelegate network_delegate; |
| 6904 | // Use a MockHostResolver (which by default maps all hosts to |
| 6905 | // 127.0.0.1). |
| 6906 | MockHostResolver host_resolver; |
| 6907 | TestURLRequestContext context(true); |
| 6908 | context.set_host_resolver(&host_resolver); |
| 6909 | context.set_transport_security_state(&transport_security_state); |
| 6910 | context.set_network_delegate(&network_delegate); |
| 6911 | context.set_cert_verifier(&cert_verifier); |
| 6912 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6913 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6914 | context.Init(); |
| 6915 | |
| 6916 | // Now send a request to trigger the header processing. |
| 6917 | TestDelegate d; |
| 6918 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6919 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6920 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6921 | request->Start(); |
| 6922 | base::RunLoop().Run(); |
| 6923 | |
| 6924 | TransportSecurityState::ExpectCTState state; |
| 6925 | ASSERT_TRUE( |
| 6926 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6927 | EXPECT_TRUE(state.enforce); |
| 6928 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6929 | } |
| 6930 | |
| 6931 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 6932 | // processed. |
| 6933 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 6934 | base::test::ScopedFeatureList feature_list; |
| 6935 | feature_list.InitAndEnableFeature( |
| 6936 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6937 | |
| 6938 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6939 | https_test_server.SetSSLConfig( |
| 6940 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6941 | https_test_server.ServeFilesFromSourceDirectory( |
| 6942 | base::FilePath(kTestFilePath)); |
| 6943 | ASSERT_TRUE(https_test_server.Start()); |
| 6944 | |
| 6945 | MockExpectCTReporter reporter; |
| 6946 | TransportSecurityState transport_security_state; |
| 6947 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6948 | |
| 6949 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6950 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6951 | ASSERT_TRUE(cert); |
| 6952 | MockCertVerifier cert_verifier; |
| 6953 | CertVerifyResult verify_result; |
| 6954 | verify_result.verified_cert = cert; |
| 6955 | verify_result.is_issued_by_known_root = true; |
| 6956 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6957 | |
| 6958 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6959 | // compliance. |
| 6960 | DoNothingCTVerifier ct_verifier; |
| 6961 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6962 | ct_policy_enforcer.set_default_result( |
| 6963 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6964 | |
| 6965 | TestNetworkDelegate network_delegate; |
| 6966 | // Use a MockHostResolver (which by default maps all hosts to |
| 6967 | // 127.0.0.1). |
| 6968 | MockHostResolver host_resolver; |
| 6969 | TestURLRequestContext context(true); |
| 6970 | context.set_host_resolver(&host_resolver); |
| 6971 | context.set_transport_security_state(&transport_security_state); |
| 6972 | context.set_network_delegate(&network_delegate); |
| 6973 | context.set_cert_verifier(&cert_verifier); |
| 6974 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6975 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6976 | context.Init(); |
| 6977 | |
| 6978 | // Now send a request to trigger the header processing. |
| 6979 | TestDelegate d; |
| 6980 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 6981 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6982 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6983 | request->Start(); |
| 6984 | base::RunLoop().Run(); |
| 6985 | |
| 6986 | TransportSecurityState::ExpectCTState state; |
| 6987 | ASSERT_TRUE( |
| 6988 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6989 | EXPECT_TRUE(state.enforce); |
| 6990 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6991 | } |
| 6992 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 6993 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6994 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 6995 | #if BUILDFLAG(ENABLE_REPORTING) |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 6996 | namespace { |
| 6997 | |
| 6998 | class TestReportingService : public ReportingService { |
| 6999 | public: |
| 7000 | struct Header { |
| 7001 | GURL url; |
| 7002 | std::string header_value; |
| 7003 | }; |
| 7004 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7005 | const std::vector<Header>& headers() { return headers_; } |
| 7006 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7007 | // ReportingService implementation: |
| 7008 | |
| 7009 | ~TestReportingService() override {} |
| 7010 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7011 | void QueueReport(const GURL& url, |
| 7012 | const std::string& group, |
| 7013 | const std::string& type, |
| 7014 | std::unique_ptr<const base::Value> body) override { |
| 7015 | NOTIMPLEMENTED(); |
| 7016 | } |
| 7017 | |
| 7018 | void ProcessHeader(const GURL& url, |
| 7019 | const std::string& header_value) override { |
| 7020 | headers_.push_back({url, header_value}); |
| 7021 | } |
| 7022 | |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 7023 | void RemoveBrowsingData( |
| 7024 | int data_type_mask, |
| 7025 | base::Callback<bool(const GURL&)> origin_filter) override { |
| 7026 | NOTIMPLEMENTED(); |
| 7027 | } |
| 7028 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7029 | private: |
| 7030 | std::vector<Header> headers_; |
| 7031 | }; |
| 7032 | |
| 7033 | std::unique_ptr<test_server::HttpResponse> SendReportToHeader( |
| 7034 | const test_server::HttpRequest& request) { |
| 7035 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7036 | new test_server::BasicHttpResponse); |
| 7037 | http_response->set_code(HTTP_OK); |
| 7038 | http_response->AddCustomHeader("Report-To", "foo"); |
| 7039 | http_response->AddCustomHeader("Report-To", "bar"); |
| 7040 | return std::move(http_response); |
| 7041 | } |
| 7042 | |
| 7043 | } // namespace |
| 7044 | |
| 7045 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderNoService) { |
| 7046 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7047 | ASSERT_TRUE(http_test_server()->Start()); |
| 7048 | GURL request_url = http_test_server()->GetURL("/"); |
| 7049 | |
| 7050 | TestNetworkDelegate network_delegate; |
| 7051 | TestURLRequestContext context(true); |
| 7052 | context.set_network_delegate(&network_delegate); |
| 7053 | context.Init(); |
| 7054 | |
| 7055 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7056 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7057 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7058 | request->Start(); |
| 7059 | base::RunLoop().Run(); |
| 7060 | } |
| 7061 | |
| 7062 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderHTTP) { |
| 7063 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7064 | ASSERT_TRUE(http_test_server()->Start()); |
| 7065 | GURL request_url = http_test_server()->GetURL("/"); |
| 7066 | |
| 7067 | TestNetworkDelegate network_delegate; |
| 7068 | TestReportingService reporting_service; |
| 7069 | TestURLRequestContext context(true); |
| 7070 | context.set_network_delegate(&network_delegate); |
| 7071 | context.set_reporting_service(&reporting_service); |
| 7072 | context.Init(); |
| 7073 | |
| 7074 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7075 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7076 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7077 | request->Start(); |
| 7078 | base::RunLoop().Run(); |
| 7079 | |
| 7080 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7081 | } |
| 7082 | |
| 7083 | TEST_F(URLRequestTestHTTP, ProcessReportToHeaderHTTPS) { |
| 7084 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7085 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7086 | ASSERT_TRUE(https_test_server.Start()); |
| 7087 | GURL request_url = https_test_server.GetURL("/"); |
| 7088 | |
| 7089 | TestNetworkDelegate network_delegate; |
| 7090 | TestReportingService reporting_service; |
| 7091 | TestURLRequestContext context(true); |
| 7092 | context.set_network_delegate(&network_delegate); |
| 7093 | context.set_reporting_service(&reporting_service); |
| 7094 | context.Init(); |
| 7095 | |
| 7096 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7097 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7098 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7099 | request->Start(); |
| 7100 | base::RunLoop().Run(); |
| 7101 | |
| 7102 | ASSERT_EQ(1u, reporting_service.headers().size()); |
| 7103 | EXPECT_EQ(request_url, reporting_service.headers()[0].url); |
| 7104 | EXPECT_EQ("foo, bar", reporting_service.headers()[0].header_value); |
| 7105 | } |
| 7106 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7107 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderInvalidHttps) { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7108 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7109 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7110 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7111 | ASSERT_TRUE(https_test_server.Start()); |
| 7112 | GURL request_url = https_test_server.GetURL("/"); |
| 7113 | |
| 7114 | TestNetworkDelegate network_delegate; |
| 7115 | TestReportingService reporting_service; |
| 7116 | TestURLRequestContext context(true); |
| 7117 | context.set_network_delegate(&network_delegate); |
| 7118 | context.set_reporting_service(&reporting_service); |
| 7119 | context.Init(); |
| 7120 | |
| 7121 | TestDelegate d; |
| 7122 | d.set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7123 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7124 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7125 | request->Start(); |
| 7126 | base::RunLoop().Run(); |
| 7127 | |
| 7128 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7129 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7130 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7131 | } |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7132 | #endif // BUILDFLAG(ENABLE_REPORTING) |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7133 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7134 | namespace { |
| 7135 | |
| 7136 | class TestNetworkErrorLoggingDelegate : public NetworkErrorLoggingDelegate { |
| 7137 | public: |
| 7138 | struct Header { |
| 7139 | Header() {} |
| 7140 | ~Header() {} |
| 7141 | |
| 7142 | url::Origin origin; |
| 7143 | std::string value; |
| 7144 | }; |
| 7145 | |
| 7146 | struct NetworkError { |
| 7147 | NetworkError() {} |
| 7148 | ~NetworkError() {} |
| 7149 | |
| 7150 | url::Origin origin; |
| 7151 | Error error; |
| 7152 | NetworkErrorLoggingDelegate::ErrorDetails details; |
| 7153 | }; |
| 7154 | |
| 7155 | const std::vector<Header>& headers() { return headers_; } |
| 7156 | const std::vector<NetworkError>& errors() { return errors_; } |
| 7157 | |
| 7158 | // NetworkErrorLoggingDelegate implementation: |
| 7159 | |
| 7160 | ~TestNetworkErrorLoggingDelegate() override {} |
| 7161 | |
| 7162 | void SetReportingService(ReportingService* reporting_service) override { |
| 7163 | NOTREACHED(); |
| 7164 | } |
| 7165 | |
| 7166 | void OnHeader(const url::Origin& origin, const std::string& value) override { |
| 7167 | Header header; |
| 7168 | header.origin = origin; |
| 7169 | header.value = value; |
| 7170 | headers_.push_back(header); |
| 7171 | } |
| 7172 | |
| 7173 | void OnNetworkError(const url::Origin& origin, |
| 7174 | Error error, |
| 7175 | ErrorDetailsCallback details_callback) override { |
| 7176 | NetworkError network_error; |
| 7177 | network_error.origin = origin; |
| 7178 | network_error.error = error; |
| 7179 | std::move(details_callback).Run(&network_error.details); |
| 7180 | errors_.push_back(network_error); |
| 7181 | } |
| 7182 | |
| 7183 | private: |
| 7184 | std::vector<Header> headers_; |
| 7185 | std::vector<NetworkError> errors_; |
| 7186 | }; |
| 7187 | |
| 7188 | std::unique_ptr<test_server::HttpResponse> SendNelHeader( |
| 7189 | const test_server::HttpRequest& request) { |
| 7190 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7191 | new test_server::BasicHttpResponse); |
| 7192 | http_response->set_code(HTTP_OK); |
| 7193 | http_response->AddCustomHeader(NetworkErrorLoggingDelegate::kHeaderName, |
| 7194 | "foo"); |
| 7195 | return std::move(http_response); |
| 7196 | } |
| 7197 | |
| 7198 | } // namespace |
| 7199 | |
| 7200 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderNoDelegate) { |
| 7201 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7202 | ASSERT_TRUE(http_test_server()->Start()); |
| 7203 | GURL request_url = http_test_server()->GetURL("/"); |
| 7204 | |
| 7205 | TestNetworkDelegate network_delegate; |
| 7206 | TestURLRequestContext context(true); |
| 7207 | context.set_network_delegate(&network_delegate); |
| 7208 | context.Init(); |
| 7209 | |
| 7210 | TestDelegate d; |
| 7211 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7212 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7213 | request->Start(); |
| 7214 | base::RunLoop().Run(); |
| 7215 | } |
| 7216 | |
| 7217 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderHttp) { |
| 7218 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7219 | ASSERT_TRUE(http_test_server()->Start()); |
| 7220 | GURL request_url = http_test_server()->GetURL("/"); |
| 7221 | |
| 7222 | TestNetworkDelegate network_delegate; |
| 7223 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7224 | TestURLRequestContext context(true); |
| 7225 | context.set_network_delegate(&network_delegate); |
| 7226 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7227 | context.Init(); |
| 7228 | |
| 7229 | TestDelegate d; |
| 7230 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7231 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7232 | request->Start(); |
| 7233 | base::RunLoop().Run(); |
| 7234 | |
| 7235 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7236 | } |
| 7237 | |
| 7238 | TEST_F(URLRequestTestHTTP, ProcessNelHeaderHttps) { |
| 7239 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7240 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7241 | ASSERT_TRUE(https_test_server.Start()); |
| 7242 | GURL request_url = https_test_server.GetURL("/"); |
| 7243 | |
| 7244 | TestNetworkDelegate network_delegate; |
| 7245 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7246 | TestURLRequestContext context(true); |
| 7247 | context.set_network_delegate(&network_delegate); |
| 7248 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7249 | context.Init(); |
| 7250 | |
| 7251 | TestDelegate d; |
| 7252 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7253 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7254 | request->Start(); |
| 7255 | base::RunLoop().Run(); |
| 7256 | |
| 7257 | ASSERT_EQ(1u, nel_delegate.headers().size()); |
| 7258 | EXPECT_EQ(url::Origin(request_url), nel_delegate.headers()[0].origin); |
| 7259 | EXPECT_EQ("foo", nel_delegate.headers()[0].value); |
| 7260 | } |
| 7261 | |
| 7262 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderInvalidHttps) { |
| 7263 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7264 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7265 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7266 | ASSERT_TRUE(https_test_server.Start()); |
| 7267 | GURL request_url = https_test_server.GetURL("/"); |
| 7268 | |
| 7269 | TestNetworkDelegate network_delegate; |
| 7270 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7271 | TestURLRequestContext context(true); |
| 7272 | context.set_network_delegate(&network_delegate); |
| 7273 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7274 | context.Init(); |
| 7275 | |
| 7276 | TestDelegate d; |
| 7277 | d.set_allow_certificate_errors(true); |
| 7278 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7279 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7280 | request->Start(); |
| 7281 | base::RunLoop().Run(); |
| 7282 | |
| 7283 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7284 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7285 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7286 | } |
| 7287 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7288 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7289 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7290 | |
| 7291 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7292 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7293 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7294 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7295 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7296 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7297 | |
| 7298 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7299 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7300 | EXPECT_EQ("text/html", mime_type); |
| 7301 | |
| 7302 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7303 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7304 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7305 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7306 | } |
| 7307 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7308 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7309 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7310 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7311 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7312 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7313 | |
| 7314 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7315 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7316 | } |
| 7317 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7318 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7319 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7320 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7321 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7322 | FileProtocolHandler file_protocol_handler( |
| 7323 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7324 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7325 | |
| 7326 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7327 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7328 | } |
| 7329 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7330 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7331 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7332 | |
| 7333 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7334 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7335 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7336 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7337 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7338 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7339 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7340 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7341 | |
| 7342 | // The redirect should have been rejected before reporting it to the caller. |
| 7343 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7344 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7345 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7346 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7347 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7349 | |
| 7350 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7351 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7352 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7353 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7354 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 7355 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7356 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7357 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7358 | |
| 7359 | // The redirect should have been rejected before reporting it to the |
| 7360 | // caller. See https://crbug.com/723796 |
| 7361 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7362 | } |
| 7363 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7364 | // Test that redirects to invalid URLs are rejected. See |
| 7365 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7366 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7367 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7368 | |
| 7369 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7370 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7371 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7372 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7373 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7374 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7375 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7376 | EXPECT_EQ(1, d.response_started_count()); |
| 7377 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7378 | |
| 7379 | // The redirect should have been rejected before reporting it to the caller. |
| 7380 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7381 | } |
| 7382 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7383 | // Make sure redirects are cached, despite not reading their bodies. |
| 7384 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7385 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7386 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7387 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7388 | |
| 7389 | { |
| 7390 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7391 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7392 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7393 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7394 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7395 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7396 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7397 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | { |
| 7401 | TestDelegate d; |
| 7402 | d.set_quit_on_redirect(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7403 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7404 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7405 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7406 | base::RunLoop().Run(); |
| 7407 | |
| 7408 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7409 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7410 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7411 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7412 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7413 | base::RunLoop().Run(); |
| 7414 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7415 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7416 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7417 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | |
| 7421 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7422 | // when the headers are read, since the body won't have been read. |
| 7423 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7424 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7425 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7426 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7427 | |
| 7428 | { |
| 7429 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7430 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7431 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7432 | redirect_to_url); |
| 7433 | |
| 7434 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7435 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7436 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7437 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7438 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7439 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7440 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7441 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7442 | } |
| 7443 | |
| 7444 | { |
| 7445 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7446 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7447 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7448 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7449 | base::RunLoop().Run(); |
| 7450 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7451 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7452 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7453 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7454 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7455 | } |
| 7456 | } |
| 7457 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7458 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7459 | // safe. |
| 7460 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7462 | |
| 7463 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7464 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7465 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7466 | |
| 7467 | TestDelegate d; |
| 7468 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7469 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7470 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7471 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7472 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7473 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7474 | base::RunLoop().Run(); |
| 7475 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7476 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7477 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7478 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7479 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7484 | // some other URL to the whitelist. |
| 7485 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7486 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7487 | |
| 7488 | GURL unsafe_url("data:text/html,something"); |
| 7489 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7490 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7491 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7492 | different_unsafe_url); |
| 7493 | |
| 7494 | TestDelegate d; |
| 7495 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7496 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7497 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7498 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7499 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7500 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7501 | base::RunLoop().Run(); |
| 7502 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7503 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7504 | |
| 7505 | // The redirect should have been rejected before reporting it to the caller. |
| 7506 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7507 | } |
| 7508 | } |
| 7509 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7510 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7511 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7512 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7513 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7514 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7515 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7516 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7517 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7518 | |
| 7519 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7520 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7521 | |
| 7522 | TestDelegate d; |
| 7523 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7524 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7525 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7527 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7528 | base::RunLoop().Run(); |
| 7529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7530 | EXPECT_EQ(2U, r->url_chain().size()); |
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_EQ(original_url, r->original_url()); |
| 7533 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7534 | } |
| 7535 | } |
| 7536 | |
| 7537 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7538 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7539 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7540 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7541 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7542 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7543 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7544 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7545 | GURL expected_redirect_url( |
| 7546 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7547 | |
| 7548 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7549 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7550 | |
| 7551 | TestDelegate d; |
| 7552 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7553 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7554 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7555 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7556 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7557 | base::RunLoop().Run(); |
| 7558 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7559 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7560 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7561 | EXPECT_EQ(original_url, r->original_url()); |
| 7562 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7563 | } |
| 7564 | } |
| 7565 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7566 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7567 | // URL should not be changed. In particular, the reference fragment should not |
| 7568 | // be modified. |
| 7569 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7570 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7571 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7572 | GURL original_url( |
| 7573 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7574 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7575 | |
| 7576 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7577 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7578 | |
| 7579 | TestDelegate d; |
| 7580 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7581 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7582 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7583 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7584 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7585 | base::RunLoop().Run(); |
| 7586 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7587 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7588 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7589 | EXPECT_EQ(original_url, r->original_url()); |
| 7590 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7591 | } |
| 7592 | } |
| 7593 | |
| 7594 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7595 | // the reference fragment of the target URL must not be modified. |
| 7596 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7597 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7598 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7599 | GURL original_url( |
| 7600 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7601 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7602 | |
| 7603 | TestDelegate d; |
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] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7606 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7607 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7608 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7609 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7610 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7611 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7612 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7613 | base::RunLoop().Run(); |
| 7614 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7615 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7616 | EXPECT_EQ(original_url, r->original_url()); |
| 7617 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7618 | } |
| 7619 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7620 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7621 | ASSERT_TRUE(http_test_server()->Start()); |
| 7622 | |
| 7623 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7624 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7625 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7626 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7627 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7628 | req->SetReferrer(referrer); |
| 7629 | req->Start(); |
| 7630 | base::RunLoop().Run(); |
| 7631 | |
| 7632 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7633 | } |
| 7634 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7635 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7636 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7637 | |
| 7638 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7639 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7640 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7641 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7642 | req->SetReferrer("http://user:[email protected]/"); |
| 7643 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7644 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7645 | |
| 7646 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7647 | } |
| 7648 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7649 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7650 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7651 | |
| 7652 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7653 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7654 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7655 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7656 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7657 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7658 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7659 | |
| 7660 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7661 | } |
| 7662 | |
| 7663 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7664 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7665 | |
| 7666 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7667 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7668 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7669 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7670 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7671 | req->SetReferrer(""); |
| 7672 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7673 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7674 | |
| 7675 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7676 | } |
| 7677 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7678 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7679 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7680 | |
| 7681 | TestDelegate d; |
| 7682 | { |
| 7683 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7684 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7685 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7686 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7687 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7688 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7689 | |
| 7690 | EXPECT_EQ(1, d.response_started_count()); |
| 7691 | EXPECT_EQ(0, d.bytes_received()); |
| 7692 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7693 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7694 | } |
| 7695 | } |
| 7696 | |
| 7697 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7698 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7699 | |
| 7700 | TestDelegate d; |
| 7701 | { |
| 7702 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7703 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7704 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7705 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7706 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7707 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7708 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7709 | |
| 7710 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7711 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7712 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7713 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7714 | |
| 7715 | EXPECT_EQ(1, d.response_started_count()); |
| 7716 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7717 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7718 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7719 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7720 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7721 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7722 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7723 | |
| 7724 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7725 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7726 | EXPECT_EQ(contents, d.data_received()); |
| 7727 | } |
| 7728 | } |
| 7729 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7730 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7731 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7732 | |
| 7733 | TestDelegate d; |
| 7734 | { |
| 7735 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7736 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7737 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7738 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7739 | |
| 7740 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7741 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7742 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7743 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7744 | |
| 7745 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7746 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7747 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7748 | d.ClearFullRequestHeaders(); |
| 7749 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7750 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7751 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7752 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7753 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7754 | EXPECT_EQ(1, d.response_started_count()); |
| 7755 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7756 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7757 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7758 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7759 | |
| 7760 | base::FilePath path; |
| 7761 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7762 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7763 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7764 | |
| 7765 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7766 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7767 | EXPECT_EQ(contents, d.data_received()); |
| 7768 | } |
| 7769 | } |
| 7770 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7771 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7772 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7773 | |
| 7774 | TestDelegate d; |
| 7775 | { |
| 7776 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7777 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7778 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7779 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7780 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7781 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7782 | |
| 7783 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7784 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7785 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7786 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7787 | |
| 7788 | EXPECT_EQ(1, d.response_started_count()); |
| 7789 | EXPECT_EQ(0, d.bytes_received()); |
| 7790 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7791 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7792 | } |
| 7793 | } |
| 7794 | |
| 7795 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7796 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7797 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7798 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7799 | { |
| 7800 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7801 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7802 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7803 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7804 | HttpRequestHeaders headers; |
| 7805 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7806 | req->SetExtraRequestHeaders(headers); |
| 7807 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7808 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7809 | |
| 7810 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7811 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7812 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7813 | } |
| 7814 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7815 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7816 | { |
| 7817 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7818 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7819 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7820 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7821 | HttpRequestHeaders headers; |
| 7822 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7823 | req->SetExtraRequestHeaders(headers); |
| 7824 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7825 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7826 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7827 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7828 | |
| 7829 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7830 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7831 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7832 | } |
| 7833 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7834 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7835 | { |
| 7836 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7837 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7838 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7839 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7840 | HttpRequestHeaders headers; |
| 7841 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7842 | req->SetExtraRequestHeaders(headers); |
| 7843 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7844 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7845 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7846 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7847 | |
| 7848 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7849 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7850 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7851 | } |
| 7852 | } |
| 7853 | |
| 7854 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7855 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7856 | |
| 7857 | // populate the cache |
| 7858 | { |
| 7859 | TestDelegate d; |
| 7860 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7861 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7862 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7863 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7864 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7865 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7866 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7867 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7868 | |
| 7869 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7870 | } |
| 7871 | |
| 7872 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7873 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7874 | // response should be fetched from the cache. |
| 7875 | { |
| 7876 | TestDelegate d; |
| 7877 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7878 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7879 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7880 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7881 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7882 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7883 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7884 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7885 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7886 | |
| 7887 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7888 | |
| 7889 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7890 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7891 | } |
| 7892 | } |
| 7893 | |
| 7894 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7895 | // http://crbug.com/6450 |
| 7896 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7897 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7898 | |
| 7899 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7900 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7901 | |
| 7902 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7903 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7904 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7905 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7906 | TestURLRequestContext context(true); |
| 7907 | context.set_network_delegate(&network_delegate); |
| 7908 | context.Init(); |
| 7909 | |
| 7910 | TestDelegate d; |
| 7911 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7912 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7913 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7914 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 7915 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7916 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7917 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7918 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7919 | |
| 7920 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7921 | |
| 7922 | // Make sure we sent the cookie in the restarted transaction. |
| 7923 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7924 | != std::string::npos); |
| 7925 | } |
| 7926 | |
| 7927 | // Same test as above, except this time the restart is initiated earlier |
| 7928 | // (without user intervention since identity is embedded in the URL). |
| 7929 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7930 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7931 | TestURLRequestContext context(true); |
| 7932 | context.set_network_delegate(&network_delegate); |
| 7933 | context.Init(); |
| 7934 | |
| 7935 | TestDelegate d; |
| 7936 | |
| 7937 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 7938 | replacements.SetUsernameStr("user2"); |
| 7939 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7940 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 7941 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7942 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7943 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7944 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7945 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7946 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7947 | |
| 7948 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 7949 | |
| 7950 | // Make sure we sent the cookie in the restarted transaction. |
| 7951 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7952 | != std::string::npos); |
| 7953 | } |
| 7954 | } |
| 7955 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7956 | // Tests that load timing works as expected with auth and the cache. |
| 7957 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7958 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7959 | |
| 7960 | // populate the cache |
| 7961 | { |
| 7962 | TestDelegate d; |
| 7963 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7964 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7965 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7966 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7967 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7968 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7969 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7970 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7971 | |
| 7972 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7973 | |
| 7974 | LoadTimingInfo load_timing_info_before_auth; |
| 7975 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 7976 | &load_timing_info_before_auth)); |
| 7977 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 7978 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 7979 | |
| 7980 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7981 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7982 | // The test server does not support keep alive sockets, so the second |
| 7983 | // request with auth should use a new socket. |
| 7984 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 7985 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 7986 | load_timing_info.socket_log_id); |
| 7987 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 7988 | load_timing_info.connect_timing.connect_start); |
| 7989 | } |
| 7990 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7991 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 7992 | // 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] | 7993 | // response should be fetched from the cache. |
| 7994 | { |
| 7995 | TestDelegate d; |
| 7996 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7997 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7998 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7999 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8001 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8002 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8003 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8004 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8005 | |
| 8006 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8007 | |
| 8008 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8009 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8010 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8011 | // Since there was a request that went over the wire, the load timing |
| 8012 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8013 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8014 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8015 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8016 | } |
| 8017 | } |
| 8018 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8019 | // In this test, we do a POST which the server will 302 redirect. |
| 8020 | // The subsequent transaction should use GET, and should not send the |
| 8021 | // Content-Type header. |
| 8022 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8023 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8024 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8025 | |
| 8026 | const char kData[] = "hello world"; |
| 8027 | |
| 8028 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8029 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8030 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8032 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8033 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8034 | |
| 8035 | // Set headers (some of which are specific to the POST). |
| 8036 | HttpRequestHeaders headers; |
| 8037 | headers.AddHeadersFromString( |
| 8038 | "Content-Type: multipart/form-data; " |
| 8039 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 8040 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 8041 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 8042 | "Accept-Language: en-US,en\r\n" |
| 8043 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 8044 | "Content-Length: 11\r\n" |
| 8045 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8046 | req->SetExtraRequestHeaders(headers); |
| 8047 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8048 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8049 | |
| 8050 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8051 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8052 | EXPECT_EQ("text/html", mime_type); |
| 8053 | |
| 8054 | const std::string& data = d.data_received(); |
| 8055 | |
| 8056 | // Check that the post-specific headers were stripped: |
| 8057 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8058 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8059 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8060 | |
| 8061 | // These extra request headers should not have been stripped. |
| 8062 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8063 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8064 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8065 | } |
| 8066 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8067 | // The following tests check that we handle mutating the request for HTTP |
| 8068 | // redirects as expected. |
| 8069 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8070 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8071 | |
| 8072 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8073 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8074 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8075 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8076 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8077 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8078 | |
| 8079 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8080 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8081 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8082 | |
| 8083 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8084 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8085 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8086 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8087 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8091 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8092 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8093 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8094 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8095 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8096 | |
| 8097 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8098 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8099 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8100 | |
| 8101 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8102 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8103 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8104 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8105 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8106 | } |
| 8107 | |
| 8108 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8109 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8110 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8111 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8112 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8113 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8114 | |
| 8115 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8116 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8117 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8118 | |
| 8119 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8120 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8121 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8122 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8123 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8124 | } |
| 8125 | |
| 8126 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8127 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8128 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8129 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8130 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8131 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8132 | |
| 8133 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8134 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8135 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8136 | |
| 8137 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8138 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8139 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8140 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8141 | } |
| 8142 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8143 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8144 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8145 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8146 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8147 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8148 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8149 | |
| 8150 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8151 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8152 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8153 | |
| 8154 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8155 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8156 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8157 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8161 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8162 | // (Like Google Drive). |
| 8163 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8164 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8165 | |
| 8166 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8167 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8168 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8169 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
| 8170 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8171 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8172 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8173 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8174 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8175 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8176 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8177 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8178 | } |
| 8179 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8180 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8181 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8182 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8183 | GURL original_url( |
| 8184 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8185 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8186 | |
| 8187 | TestDelegate d; |
| 8188 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8189 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8190 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8191 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8192 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8193 | base::RunLoop().Run(); |
| 8194 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8195 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8196 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8197 | EXPECT_EQ(original_url, r->original_url()); |
| 8198 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8199 | } |
| 8200 | } |
| 8201 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8202 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8203 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8204 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8205 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8206 | GURL first_party_url("http://example.com"); |
| 8207 | |
| 8208 | TestDelegate d; |
| 8209 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8210 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8211 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8212 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8213 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8214 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8215 | base::RunLoop().Run(); |
| 8216 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8217 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8218 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8219 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8220 | } |
| 8221 | } |
| 8222 | |
| 8223 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8224 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8225 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8226 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8227 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8228 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8229 | |
| 8230 | TestDelegate d; |
| 8231 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8232 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8233 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8234 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8235 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8236 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8237 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8238 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8239 | base::RunLoop().Run(); |
| 8240 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8241 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8242 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8243 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8244 | } |
| 8245 | } |
| 8246 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8247 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8248 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8249 | |
| 8250 | const char kData[] = "hello world"; |
| 8251 | |
| 8252 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8253 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8254 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8255 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8256 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8257 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8258 | HttpRequestHeaders headers; |
| 8259 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8260 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8261 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8262 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8263 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8264 | req.get(), &default_network_delegate_, |
| 8265 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8266 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8267 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8268 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8269 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8270 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8271 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8275 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8276 | |
| 8277 | const char kData[] = "hello world"; |
| 8278 | |
| 8279 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8280 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8281 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8282 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8283 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8284 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8285 | HttpRequestHeaders headers; |
| 8286 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8287 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8288 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8289 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8290 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8291 | req.get(), &default_network_delegate_, |
| 8292 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8293 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8294 | "Very Good Reason")); |
| 8295 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8296 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8297 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8298 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8299 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8300 | EXPECT_EQ(kData, d.data_received()); |
| 8301 | } |
| 8302 | |
| 8303 | // Check that default A-L header is sent. |
| 8304 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8305 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8306 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8307 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8308 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8309 | TestURLRequestContext context(true); |
| 8310 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8311 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8312 | context.Init(); |
| 8313 | |
| 8314 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8315 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8316 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8317 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8318 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8319 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8320 | EXPECT_EQ("en", d.data_received()); |
| 8321 | } |
| 8322 | |
| 8323 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8324 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8325 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8326 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8327 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8328 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8329 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8330 | TestURLRequestContext context(true); |
| 8331 | context.set_network_delegate(&network_delegate); |
| 8332 | context.Init(); |
| 8333 | // We override the language after initialization because empty entries |
| 8334 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8335 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8336 | |
| 8337 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8338 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8339 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8340 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8341 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8342 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8343 | EXPECT_EQ("None", d.data_received()); |
| 8344 | } |
| 8345 | |
| 8346 | // Check that if request overrides the A-L header, the default is not appended. |
| 8347 | // See http://crbug.com/20894 |
| 8348 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
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 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8352 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8353 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8354 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8355 | HttpRequestHeaders headers; |
| 8356 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8357 | req->SetExtraRequestHeaders(headers); |
| 8358 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8359 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8360 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8361 | } |
| 8362 | |
| 8363 | // Check that default A-E header is sent. |
| 8364 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8365 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8366 | |
| 8367 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8368 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8369 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8370 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8371 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8372 | req->SetExtraRequestHeaders(headers); |
| 8373 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8374 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8375 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8376 | } |
| 8377 | |
| 8378 | // Check that if request overrides the A-E header, the default is not appended. |
| 8379 | // See http://crbug.com/47381 |
| 8380 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8381 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8382 | |
| 8383 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8384 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8385 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8386 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8387 | HttpRequestHeaders headers; |
| 8388 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8389 | req->SetExtraRequestHeaders(headers); |
| 8390 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8391 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8392 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8393 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8394 | } |
| 8395 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8396 | // Check that setting the A-C header sends the proper header. |
| 8397 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8398 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8399 | |
| 8400 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8401 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8402 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8403 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8404 | HttpRequestHeaders headers; |
| 8405 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8406 | req->SetExtraRequestHeaders(headers); |
| 8407 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8408 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8409 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8410 | } |
| 8411 | |
| 8412 | // Check that default User-Agent header is sent. |
| 8413 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8414 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8415 | |
| 8416 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8417 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8418 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8419 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8420 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8421 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8422 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8423 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8424 | } |
| 8425 | |
| 8426 | // Check that if request overrides the User-Agent header, |
| 8427 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8428 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8429 | #if defined(OS_IOS) |
| 8430 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8431 | #else |
| 8432 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8433 | #endif |
| 8434 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8435 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8436 | |
| 8437 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8438 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8439 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8440 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8441 | HttpRequestHeaders headers; |
| 8442 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8443 | req->SetExtraRequestHeaders(headers); |
| 8444 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8445 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8446 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8447 | } |
| 8448 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8449 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8450 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8451 | // Accept-Charset headers. |
| 8452 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8453 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8454 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8455 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8456 | TestURLRequestContext context(true); |
| 8457 | context.set_network_delegate(&network_delegate); |
| 8458 | context.Init(); |
| 8459 | // We override the HttpUserAgentSettings after initialization because empty |
| 8460 | // entries get overridden by Init(). |
| 8461 | context.set_http_user_agent_settings(NULL); |
| 8462 | |
| 8463 | struct { |
| 8464 | const char* request; |
| 8465 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8466 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8467 | {"/echoheader?Accept-Charset", "None"}, |
| 8468 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8469 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 8470 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8471 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8472 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8473 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8474 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8475 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8476 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8477 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8478 | << " Request = \"" << tests[i].request << "\""; |
| 8479 | } |
| 8480 | } |
| 8481 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8482 | // Make sure that URLRequest passes on its priority updates to |
| 8483 | // newly-created jobs after the first one. |
| 8484 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8485 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8486 | |
| 8487 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8488 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8489 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8490 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8491 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8492 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8493 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8494 | req.get(), &default_network_delegate_, |
| 8495 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8496 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8497 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8499 | req->SetPriority(LOW); |
| 8500 | req->Start(); |
| 8501 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8502 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8503 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8504 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8505 | req.get(), &default_network_delegate_, &job_priority)); |
| 8506 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8507 | |
| 8508 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8509 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8510 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8511 | } |
| 8512 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8513 | // Check that creating a network request while entering/exiting suspend mode |
| 8514 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8515 | // does not return an HttpTransaction. |
| 8516 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 8517 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8518 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8519 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8520 | network_layer->OnSuspend(); |
| 8521 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8522 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 8523 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 8524 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8525 | |
| 8526 | TestURLRequestContext context(true); |
| 8527 | context.set_http_transaction_factory(&http_cache); |
| 8528 | context.Init(); |
| 8529 | |
| 8530 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8531 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8532 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8533 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8534 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8535 | base::RunLoop().Run(); |
| 8536 | |
| 8537 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8538 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8539 | } |
| 8540 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8541 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8542 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8543 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 8544 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 8545 | public: |
| 8546 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 8547 | : network_session_(network_session) {} |
| 8548 | |
| 8549 | ~FailingHttpTransactionFactory() override {} |
| 8550 | |
| 8551 | // HttpTransactionFactory methods: |
| 8552 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8553 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8554 | return ERR_FAILED; |
| 8555 | } |
| 8556 | |
| 8557 | HttpCache* GetCache() override { return nullptr; } |
| 8558 | |
| 8559 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 8560 | |
| 8561 | private: |
| 8562 | HttpNetworkSession* network_session_; |
| 8563 | |
| 8564 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 8565 | }; |
| 8566 | |
| 8567 | } // namespace |
| 8568 | |
| 8569 | // Check that when a request that fails to create an HttpTransaction can be |
| 8570 | // cancelled while the failure notification is pending, and doesn't send two |
| 8571 | // failure notifications. |
| 8572 | // |
| 8573 | // This currently only happens when in suspend mode and there's no cache, but |
| 8574 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 8575 | // behaviors. |
| 8576 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 8577 | FailingHttpTransactionFactory http_transaction_factory( |
| 8578 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8579 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8580 | context.set_http_transaction_factory(&http_transaction_factory); |
| 8581 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8582 | context.Init(); |
| 8583 | |
| 8584 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8585 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8586 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8587 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8588 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 8589 | // try to create an HttpNetworkTransaction synchronously on start). |
| 8590 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8591 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8592 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8593 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8594 | // Run pending error task, if there is one. |
| 8595 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8596 | |
| 8597 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8598 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8599 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8600 | |
| 8601 | // NetworkDelegate should see the cancellation, but not the error. |
| 8602 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 8603 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8604 | } |
| 8605 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8606 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8607 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8608 | |
| 8609 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8610 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8611 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8612 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8613 | |
| 8614 | req->Start(); |
| 8615 | base::RunLoop().Run(); |
| 8616 | |
| 8617 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8618 | } |
| 8619 | |
| 8620 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8621 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8622 | |
| 8623 | // Populate the cache. |
| 8624 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8625 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8626 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8627 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8628 | req->Start(); |
| 8629 | base::RunLoop().Run(); |
| 8630 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8631 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8632 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8633 | EXPECT_FALSE(req->response_info().was_cached); |
| 8634 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8635 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8636 | DEFAULT_PRIORITY, &d, |
| 8637 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8638 | req->Start(); |
| 8639 | base::RunLoop().Run(); |
| 8640 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8641 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8642 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8643 | EXPECT_TRUE(req->response_info().was_cached); |
| 8644 | } |
| 8645 | |
| 8646 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8647 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8648 | |
| 8649 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8650 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8651 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8652 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 8653 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8654 | |
| 8655 | req->Start(); |
| 8656 | base::RunLoop().Run(); |
| 8657 | |
| 8658 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8659 | } |
| 8660 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8661 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8662 | // correctly in the absence of contention. |
| 8663 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 8664 | ASSERT_TRUE(http_test_server()->Start()); |
| 8665 | |
| 8666 | TestDelegate d; |
| 8667 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8668 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8669 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8670 | req->Start(); |
| 8671 | base::RunLoop().Run(); |
| 8672 | |
| 8673 | EXPECT_TRUE(req->status().is_success()); |
| 8674 | } |
| 8675 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8676 | // A class to hold state for responding to USER_NOTIFY callbacks from |
| 8677 | // BlockingNetworkDelegate. It also accepts a RunLoop that will be |
| 8678 | // signaled via QuitWhenIdle() when any request is blocked. |
| 8679 | // |
| 8680 | class NotificationCallbackHandler { |
| 8681 | public: |
| 8682 | // Default constructed object doesn't block anything. |
| 8683 | NotificationCallbackHandler() : run_loop_(nullptr) {} |
| 8684 | |
| 8685 | void AddURLRequestToBlockList(const URLRequest* request) { |
| 8686 | requests_to_block_.insert(request); |
| 8687 | } |
| 8688 | |
| 8689 | Error ShouldBlockRequest(const CompletionCallback& callback, |
| 8690 | const URLRequest* request) { |
| 8691 | if (requests_to_block_.find(request) == requests_to_block_.end()) { |
| 8692 | return OK; |
| 8693 | } |
| 8694 | |
| 8695 | DCHECK(blocked_callbacks_.find(request) == blocked_callbacks_.end()); |
| 8696 | blocked_callbacks_[request] = callback; |
| 8697 | if (run_loop_ && blocked_callbacks_.size() == requests_to_block_.size()) |
| 8698 | run_loop_->QuitWhenIdle(); |
| 8699 | return ERR_IO_PENDING; |
| 8700 | } |
| 8701 | |
| 8702 | // Erases object's memory of blocked callbacks as a side effect. |
| 8703 | void GetBlockedCallbacks( |
| 8704 | std::map<const URLRequest*, CompletionCallback>* blocked_callbacks) { |
| 8705 | blocked_callbacks_.swap(*blocked_callbacks); |
| 8706 | } |
| 8707 | |
| 8708 | // Set a RunLoop that, if non-null, will be signaled if any request |
| 8709 | // is blocked. It is the callers responsibility to make sure the |
| 8710 | // passed object lives past the destruction of this class or |
| 8711 | // next call to SetRunLoop(). |
| 8712 | void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 8713 | |
| 8714 | private: |
| 8715 | std::set<const URLRequest*> requests_to_block_; |
| 8716 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks_; |
| 8717 | |
| 8718 | base::RunLoop* run_loop_; |
| 8719 | |
| 8720 | DISALLOW_COPY_AND_ASSIGN(NotificationCallbackHandler); |
| 8721 | }; |
| 8722 | |
| 8723 | TEST_F(URLRequestTestHTTP, MultiThrottledPriority) { |
| 8724 | ASSERT_TRUE(http_test_server()->Start()); |
| 8725 | |
| 8726 | base::RunLoop run_until_request_blocked; |
| 8727 | |
| 8728 | NotificationCallbackHandler notification_handler; |
| 8729 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8730 | BlockingNetworkDelegate network_delegate( |
| 8731 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8732 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8733 | network_delegate.set_notification_callback( |
| 8734 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8735 | // Both objects are owned by this function, and |
| 8736 | // |*network_delegate| will be destroyed first, so |
| 8737 | // it's safe to pass it an unretained pointer. |
| 8738 | base::Unretained(¬ification_handler))); |
| 8739 | |
| 8740 | TestURLRequestContext context(true); |
| 8741 | context.set_network_delegate(&network_delegate); |
| 8742 | context.Init(); |
| 8743 | |
| 8744 | // Use different test URLs to make sure all three requests turn into |
| 8745 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8746 | // the requests may be waited on separately. |
| 8747 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8748 | std::unique_ptr<URLRequest> req1( |
| 8749 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8750 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8751 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8752 | |
| 8753 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8754 | std::unique_ptr<URLRequest> req2( |
| 8755 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8756 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8757 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8758 | |
| 8759 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8760 | std::unique_ptr<URLRequest> req3( |
| 8761 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8762 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8763 | req1->Start(); |
| 8764 | req2->Start(); |
| 8765 | req3->Start(); |
| 8766 | run_until_request_blocked.Run(); |
| 8767 | notification_handler.SetRunLoop(nullptr); |
| 8768 | |
| 8769 | // The first two requests should be blocked based on the notification |
| 8770 | // callback, and their status should have blocked the third request |
| 8771 | // through throttling. |
| 8772 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8773 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8774 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8775 | |
| 8776 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8777 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8778 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8779 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8780 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8781 | |
| 8782 | // Unblocking one of the requests blocked on the notification callback |
| 8783 | // should let it complete, which should then let the third request |
| 8784 | // complete. Unblock the second request, then wait for the third |
| 8785 | // request to complete. |
| 8786 | // TODO(rdsmith): Find something to wait on other than the third |
| 8787 | // requests completion; if there's a bug in throttling, that will |
| 8788 | // result in this test hanging rather than failing quickly. |
| 8789 | d1.set_quit_on_complete(false); |
| 8790 | d2.set_quit_on_complete(false); |
| 8791 | d3.set_quit_on_complete(true); |
| 8792 | blocked_callbacks[req2.get()].Run(OK); |
| 8793 | base::RunLoop().Run(); |
| 8794 | |
| 8795 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8796 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8797 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8798 | // req3 is only unblocked after req2 completes, so req2's |
| 8799 | // success is guaranteed at this point in the function. |
| 8800 | EXPECT_EQ(URLRequestStatus::SUCCESS, req2->status().status()); |
| 8801 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8802 | } |
| 8803 | |
| 8804 | // Confirm that failing a request unblocks following requests. |
| 8805 | TEST_F(URLRequestTestHTTP, ThrottledFailure) { |
| 8806 | ASSERT_TRUE(http_test_server()->Start()); |
| 8807 | |
| 8808 | base::RunLoop run_until_request_blocked; |
| 8809 | |
| 8810 | NotificationCallbackHandler notification_handler; |
| 8811 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8812 | BlockingNetworkDelegate network_delegate( |
| 8813 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8814 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8815 | network_delegate.set_notification_callback( |
| 8816 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8817 | // Both objects are owned by this function, and |
| 8818 | // |*network_delegate| will be destroyed first, so |
| 8819 | // it's safe to pass it an unretained pointer. |
| 8820 | base::Unretained(¬ification_handler))); |
| 8821 | |
| 8822 | TestURLRequestContext context(true); |
| 8823 | context.set_network_delegate(&network_delegate); |
| 8824 | context.Init(); |
| 8825 | |
| 8826 | // Use different test URLs to make sure all three requests turn into |
| 8827 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8828 | // the requests may be waited on separately. |
| 8829 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8830 | std::unique_ptr<URLRequest> req1( |
| 8831 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8832 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8833 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8834 | |
| 8835 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8836 | std::unique_ptr<URLRequest> req2( |
| 8837 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8838 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8839 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8840 | |
| 8841 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8842 | std::unique_ptr<URLRequest> req3( |
| 8843 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8844 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8845 | req1->Start(); |
| 8846 | req2->Start(); |
| 8847 | req3->Start(); |
| 8848 | run_until_request_blocked.Run(); |
| 8849 | notification_handler.SetRunLoop(nullptr); |
| 8850 | |
| 8851 | // The first two requests should be blocked based on the notification |
| 8852 | // callback, and their status should have blocked the third request |
| 8853 | // through throttling. |
| 8854 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8855 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8856 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8857 | |
| 8858 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8859 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8860 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8861 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8862 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8863 | |
| 8864 | // Confirm canceling one of the outstanding requests allows the |
| 8865 | // blocked request to complete. |
| 8866 | |
| 8867 | // TODO(rdsmith): Find something to wait on other than the third |
| 8868 | // requests completion; if there's a bug in throttling, that will |
| 8869 | // result in this test hanging rather than failing quickly. |
| 8870 | d1.set_quit_on_complete(false); |
| 8871 | d2.set_quit_on_complete(false); |
| 8872 | d3.set_quit_on_complete(true); |
| 8873 | req2->Cancel(); |
| 8874 | base::RunLoop().Run(); |
| 8875 | |
| 8876 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8877 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8878 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8879 | EXPECT_EQ(URLRequestStatus::CANCELED, req2->status().status()); |
| 8880 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8881 | } |
| 8882 | |
| 8883 | TEST_F(URLRequestTestHTTP, ThrottledRepriUnblock) { |
| 8884 | ASSERT_TRUE(http_test_server()->Start()); |
| 8885 | |
| 8886 | base::RunLoop run_until_request_blocked; |
| 8887 | |
| 8888 | NotificationCallbackHandler notification_handler; |
| 8889 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8890 | BlockingNetworkDelegate network_delegate( |
| 8891 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8892 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8893 | network_delegate.set_notification_callback( |
| 8894 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8895 | // Both objects are owned by this function, and |
| 8896 | // |*network_delegate| will be destroyed first, so |
| 8897 | // it's safe to pass it an unretained pointer. |
| 8898 | base::Unretained(¬ification_handler))); |
| 8899 | |
| 8900 | TestURLRequestContext context(true); |
| 8901 | context.set_network_delegate(&network_delegate); |
| 8902 | context.Init(); |
| 8903 | |
| 8904 | // Use different test URLs to make sure all three requests turn into |
| 8905 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8906 | // the requests may be waited on separately. |
| 8907 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8908 | std::unique_ptr<URLRequest> req1( |
| 8909 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8910 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8911 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8912 | |
| 8913 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8914 | std::unique_ptr<URLRequest> req2( |
| 8915 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8916 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8917 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8918 | |
| 8919 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8920 | std::unique_ptr<URLRequest> req3( |
| 8921 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8922 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8923 | req1->Start(); |
| 8924 | req2->Start(); |
| 8925 | req3->Start(); |
| 8926 | run_until_request_blocked.Run(); |
| 8927 | notification_handler.SetRunLoop(nullptr); |
| 8928 | |
| 8929 | // The first two requests should be blocked based on the notification |
| 8930 | // callback, and their status should have blocked the third request |
| 8931 | // through throttling. |
| 8932 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8933 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8934 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8935 | |
| 8936 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8937 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8938 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8939 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8940 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8941 | |
| 8942 | // Confirm raising the priority of the third request allows it to complete. |
| 8943 | |
| 8944 | // TODO(rdsmith): Find something to wait on other than the third |
| 8945 | // requests completion; if there's a bug in throttling, that will |
| 8946 | // result in this test hanging rather than failing quickly. |
| 8947 | d1.set_quit_on_complete(false); |
| 8948 | d2.set_quit_on_complete(false); |
| 8949 | d3.set_quit_on_complete(true); |
| 8950 | req3->SetPriority(IDLE); |
| 8951 | base::RunLoop().Run(); |
| 8952 | |
| 8953 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8954 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8955 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8956 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8957 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8958 | } |
| 8959 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8960 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 8961 | ASSERT_TRUE(http_test_server()->Start()); |
| 8962 | |
| 8963 | TestDelegate d; |
| 8964 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8965 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 8966 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8967 | req->Start(); |
| 8968 | base::RunLoop().Run(); |
| 8969 | |
| 8970 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 8971 | } |
| 8972 | |
| 8973 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 8974 | ASSERT_TRUE(http_test_server()->Start()); |
| 8975 | |
| 8976 | TestDelegate d; |
| 8977 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8978 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 8979 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8980 | req->Start(); |
| 8981 | base::RunLoop().Run(); |
| 8982 | |
| 8983 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 8984 | } |
| 8985 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8986 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 8987 | // the delegate isn't called back synchronously. |
| 8988 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 8989 | ASSERT_TRUE(http_test_server()->Start()); |
| 8990 | default_network_delegate_.set_before_start_transaction_fails(); |
| 8991 | |
| 8992 | TestDelegate d; |
| 8993 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8994 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 8995 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8996 | req->Start(); |
| 8997 | DCHECK(!d.response_completed()); |
| 8998 | base::RunLoop().Run(); |
| 8999 | DCHECK(d.response_completed()); |
| 9000 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9001 | } |
| 9002 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9003 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9004 | public: |
| 9005 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9006 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9007 | // tests into the factory tests. |
| 9008 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 9009 | } |
| 9010 | |
| 9011 | void SetUpFactory() override { |
| 9012 | interceptor_ = new MockURLRequestInterceptor(); |
| 9013 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9014 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9015 | } |
| 9016 | |
| 9017 | MockURLRequestInterceptor* interceptor() const { |
| 9018 | return interceptor_; |
| 9019 | } |
| 9020 | |
| 9021 | private: |
| 9022 | MockURLRequestInterceptor* interceptor_; |
| 9023 | }; |
| 9024 | |
| 9025 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9026 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9027 | interceptor()->set_intercept_redirect(true); |
| 9028 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9029 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9030 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9031 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9032 | |
| 9033 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9034 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9035 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9036 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9037 | req->Start(); |
| 9038 | base::RunLoop().Run(); |
| 9039 | |
| 9040 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9041 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9042 | int status = d.request_status(); |
| 9043 | EXPECT_EQ(OK, status); |
| 9044 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9045 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9046 | |
| 9047 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9048 | EXPECT_EQ(1, d.response_started_count()); |
| 9049 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9050 | |
| 9051 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9052 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9053 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9054 | } |
| 9055 | |
| 9056 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9057 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9058 | // Intercept that error and respond with an OK response. |
| 9059 | interceptor()->set_intercept_final_response(true); |
| 9060 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9061 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9062 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9063 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9064 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9065 | |
| 9066 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9067 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9068 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9069 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9070 | req->set_method("GET"); |
| 9071 | req->Start(); |
| 9072 | base::RunLoop().Run(); |
| 9073 | |
| 9074 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9075 | |
| 9076 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9077 | int status = d.request_status(); |
| 9078 | EXPECT_EQ(OK, status); |
| 9079 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9080 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9081 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9082 | EXPECT_EQ(1, d.response_started_count()); |
| 9083 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9084 | |
| 9085 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9086 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9087 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9088 | } |
| 9089 | |
| 9090 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9091 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9092 | // Intercept that error and respond with an OK response. |
| 9093 | interceptor()->set_intercept_final_response(true); |
| 9094 | |
| 9095 | // Intercept with a real URLRequestHttpJob. |
| 9096 | interceptor()->set_use_url_request_http_job(true); |
| 9097 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9098 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9099 | |
| 9100 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9102 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9104 | req->set_method("GET"); |
| 9105 | req->Start(); |
| 9106 | base::RunLoop().Run(); |
| 9107 | |
| 9108 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9109 | |
| 9110 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9111 | int status = d.request_status(); |
| 9112 | EXPECT_EQ(OK, status); |
| 9113 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9114 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9115 | EXPECT_EQ("hello", d.data_received()); |
| 9116 | EXPECT_EQ(1, d.response_started_count()); |
| 9117 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9118 | |
| 9119 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9120 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9121 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9122 | } |
| 9123 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9124 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9125 | public: |
| 9126 | URLRequestTestReferrerPolicy() {} |
| 9127 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9128 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9129 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9130 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9131 | origin_server_->AddDefaultHandlers( |
| 9132 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9133 | } else { |
| 9134 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9135 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9136 | ASSERT_TRUE(origin_server_->Start()); |
| 9137 | } |
| 9138 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9139 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9140 | net::EmbeddedTestServer::Type dest_type) { |
| 9141 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9142 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9143 | origin_server_->AddDefaultHandlers( |
| 9144 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9145 | } else { |
| 9146 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9147 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9148 | ASSERT_TRUE(origin_server_->Start()); |
| 9149 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9150 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9151 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9152 | destination_server_->AddDefaultHandlers( |
| 9153 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9154 | } else { |
| 9155 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9156 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9157 | ASSERT_TRUE(destination_server_->Start()); |
| 9158 | } |
| 9159 | |
| 9160 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9161 | const GURL& referrer, |
| 9162 | const GURL& expected) { |
| 9163 | // Create and execute the request: we'll only have a |destination_server_| |
| 9164 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9165 | // |origin_server_| for both endpoints. |
| 9166 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9167 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9168 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9169 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9170 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9171 | |
| 9172 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9173 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9174 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9175 | req->set_referrer_policy(policy); |
| 9176 | req->SetReferrer(referrer.spec()); |
| 9177 | req->Start(); |
| 9178 | base::RunLoop().Run(); |
| 9179 | |
| 9180 | EXPECT_EQ(1, d.response_started_count()); |
| 9181 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9182 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9183 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9184 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9185 | |
| 9186 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9187 | if (expected.is_empty()) |
| 9188 | EXPECT_EQ("None", d.data_received()); |
| 9189 | else |
| 9190 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9191 | } |
| 9192 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9193 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9194 | |
| 9195 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9196 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9197 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9198 | }; |
| 9199 | |
| 9200 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9201 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9202 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9203 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9204 | VerifyReferrerAfterRedirect( |
| 9205 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9206 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9207 | |
| 9208 | VerifyReferrerAfterRedirect( |
| 9209 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9210 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9211 | |
| 9212 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9213 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9214 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9215 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9216 | referrer); |
| 9217 | |
| 9218 | // The original referrer set on the request is expected to obey the referrer |
| 9219 | // policy and already be stripped to the origin; thus this test case just |
| 9220 | // checks that this policy doesn't cause the referrer to change when following |
| 9221 | // a redirect. |
| 9222 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9223 | referrer.GetOrigin()); |
| 9224 | |
| 9225 | VerifyReferrerAfterRedirect( |
| 9226 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9227 | referrer); |
| 9228 | |
| 9229 | // The original referrer set on the request is expected to obey the referrer |
| 9230 | // policy and already be stripped to the origin; thus this test case just |
| 9231 | // checks that this policy doesn't cause the referrer to change when following |
| 9232 | // a redirect. |
| 9233 | VerifyReferrerAfterRedirect( |
| 9234 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9235 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9236 | |
| 9237 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9238 | } |
| 9239 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9240 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9241 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9242 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9243 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9244 | |
| 9245 | VerifyReferrerAfterRedirect( |
| 9246 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9247 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9248 | |
| 9249 | VerifyReferrerAfterRedirect( |
| 9250 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9251 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9252 | |
| 9253 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9254 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9255 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9256 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9257 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9258 | referrer); |
| 9259 | |
| 9260 | // The original referrer set on the request is expected to obey the referrer |
| 9261 | // policy and already be stripped to the origin; thus this test case just |
| 9262 | // checks that this policy doesn't cause the referrer to change when following |
| 9263 | // a redirect. |
| 9264 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9265 | referrer.GetOrigin()); |
| 9266 | |
| 9267 | VerifyReferrerAfterRedirect( |
| 9268 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9269 | |
| 9270 | // The original referrer set on the request is expected to obey the referrer |
| 9271 | // policy and already be stripped to the origin; thus this test case just |
| 9272 | // checks that this policy doesn't cause the referrer to change when following |
| 9273 | // a redirect. |
| 9274 | VerifyReferrerAfterRedirect( |
| 9275 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9276 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9277 | |
| 9278 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9279 | } |
| 9280 | |
| 9281 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9282 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9283 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9284 | |
| 9285 | VerifyReferrerAfterRedirect( |
| 9286 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9287 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9288 | |
| 9289 | VerifyReferrerAfterRedirect( |
| 9290 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9291 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9292 | |
| 9293 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9294 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9295 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9296 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9297 | referrer); |
| 9298 | |
| 9299 | // The original referrer set on the request is expected to obey the referrer |
| 9300 | // policy and already be stripped to the origin; thus this test case just |
| 9301 | // checks that this policy doesn't cause the referrer to change when following |
| 9302 | // a redirect. |
| 9303 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9304 | referrer.GetOrigin()); |
| 9305 | |
| 9306 | VerifyReferrerAfterRedirect( |
| 9307 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9308 | referrer); |
| 9309 | |
| 9310 | // The original referrer set on the request is expected to obey the referrer |
| 9311 | // policy and already be stripped to the origin; thus this test case just |
| 9312 | // checks that this policy doesn't cause the referrer to change when following |
| 9313 | // a redirect. |
| 9314 | VerifyReferrerAfterRedirect( |
| 9315 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9316 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9317 | |
| 9318 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9319 | } |
| 9320 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9321 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9322 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9323 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9324 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9325 | |
| 9326 | VerifyReferrerAfterRedirect( |
| 9327 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9328 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9329 | |
| 9330 | VerifyReferrerAfterRedirect( |
| 9331 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9332 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9333 | |
| 9334 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9335 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9336 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9337 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9338 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9339 | referrer); |
| 9340 | |
| 9341 | // The original referrer set on the request is expected to obey the referrer |
| 9342 | // policy and already be stripped to the origin; thus this test case just |
| 9343 | // checks that this policy doesn't cause the referrer to change when following |
| 9344 | // a redirect. |
| 9345 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9346 | referrer.GetOrigin()); |
| 9347 | |
| 9348 | VerifyReferrerAfterRedirect( |
| 9349 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9350 | |
| 9351 | // The original referrer set on the request is expected to obey the referrer |
| 9352 | // policy and already be stripped to the origin; thus this test case just |
| 9353 | // checks that this policy doesn't cause the referrer to change when following |
| 9354 | // a redirect. |
| 9355 | VerifyReferrerAfterRedirect( |
| 9356 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9357 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9358 | |
| 9359 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9360 | } |
| 9361 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9362 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9363 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9364 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9365 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9366 | |
| 9367 | VerifyReferrerAfterRedirect( |
| 9368 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9369 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9370 | |
| 9371 | VerifyReferrerAfterRedirect( |
| 9372 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9373 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9374 | |
| 9375 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9376 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9377 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9378 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9379 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9380 | referrer); |
| 9381 | |
| 9382 | // The original referrer set on the request is expected to obey the referrer |
| 9383 | // policy and already be stripped to the origin; thus this test case just |
| 9384 | // checks that this policy doesn't cause the referrer to change when following |
| 9385 | // a redirect. |
| 9386 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9387 | referrer.GetOrigin()); |
| 9388 | |
| 9389 | VerifyReferrerAfterRedirect( |
| 9390 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9391 | |
| 9392 | // The original referrer set on the request is expected to obey the referrer |
| 9393 | // policy and already be stripped to the origin; thus this test case just |
| 9394 | // checks that this policy doesn't cause the referrer to change when following |
| 9395 | // a redirect. |
| 9396 | VerifyReferrerAfterRedirect( |
| 9397 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9398 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9399 | |
| 9400 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9401 | } |
| 9402 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9403 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9404 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9405 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9406 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9407 | |
| 9408 | VerifyReferrerAfterRedirect( |
| 9409 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9410 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9411 | |
| 9412 | VerifyReferrerAfterRedirect( |
| 9413 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9414 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9415 | |
| 9416 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9417 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9418 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9419 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9420 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9421 | referrer); |
| 9422 | |
| 9423 | // The original referrer set on the request is expected to obey the referrer |
| 9424 | // policy and already be stripped to the origin; thus this test case just |
| 9425 | // checks that this policy doesn't cause the referrer to change when following |
| 9426 | // a redirect. |
| 9427 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9428 | referrer.GetOrigin()); |
| 9429 | |
| 9430 | VerifyReferrerAfterRedirect( |
| 9431 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9432 | |
| 9433 | // The original referrer set on the request is expected to obey the referrer |
| 9434 | // policy and already be stripped to the origin, though it should be |
| 9435 | // subsequently cleared during the downgrading redirect. |
| 9436 | VerifyReferrerAfterRedirect( |
| 9437 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9438 | referrer.GetOrigin(), GURL()); |
| 9439 | |
| 9440 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9441 | } |
| 9442 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9443 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9444 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9445 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9446 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9447 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9448 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9449 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9450 | |
| 9451 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9452 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9453 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9454 | }; |
| 9455 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9456 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9457 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9458 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9459 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9460 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9461 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9462 | TestDelegate d; |
| 9463 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9464 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9465 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9466 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9467 | r->Start(); |
| 9468 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9469 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9470 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9471 | |
| 9472 | EXPECT_EQ(1, d.response_started_count()); |
| 9473 | EXPECT_FALSE(d.received_data_before_response()); |
| 9474 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9475 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9476 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9477 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9478 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9479 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9480 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9481 | } |
| 9482 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9483 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9484 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9485 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9486 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9487 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9488 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9489 | |
| 9490 | bool err_allowed = true; |
| 9491 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9492 | TestDelegate d; |
| 9493 | { |
| 9494 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9495 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9496 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9499 | r->Start(); |
| 9500 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9501 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9502 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9503 | |
| 9504 | EXPECT_EQ(1, d.response_started_count()); |
| 9505 | EXPECT_FALSE(d.received_data_before_response()); |
| 9506 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9507 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9508 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9509 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9510 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9511 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9512 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9513 | } |
| 9514 | } |
| 9515 | } |
| 9516 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9517 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9518 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9519 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9520 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9521 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9522 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9523 | |
| 9524 | // Iterate from false to true, just so that we do the opposite of the |
| 9525 | // previous test in order to increase test coverage. |
| 9526 | bool err_allowed = false; |
| 9527 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9528 | TestDelegate d; |
| 9529 | { |
| 9530 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9531 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9532 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9533 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9534 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9535 | r->Start(); |
| 9536 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9537 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9538 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9539 | |
| 9540 | EXPECT_EQ(1, d.response_started_count()); |
| 9541 | EXPECT_FALSE(d.received_data_before_response()); |
| 9542 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9543 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9544 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9545 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9546 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9547 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9548 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9549 | } |
| 9550 | } |
| 9551 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9552 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9553 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9554 | // security state. (see http://crbug.com/550977). |
| 9555 | #if !defined(OS_IOS) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9556 | // This tests that a load of www.google.com with a certificate error sets |
| 9557 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 9558 | // the interstitial to be fatal. |
| 9559 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 9560 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 9561 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 9562 | #endif |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9563 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9564 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9565 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9566 | ASSERT_TRUE(test_server.Start()); |
| 9567 | |
| 9568 | // We require that the URL be www.google.com in order to pick up the |
| 9569 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 9570 | // have to use a MockHostResolver in order to direct www.google.com to the |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9571 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9572 | |
| 9573 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9574 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9575 | TestURLRequestContext context(true); |
| 9576 | context.set_network_delegate(&network_delegate); |
| 9577 | context.set_host_resolver(&host_resolver); |
| 9578 | TransportSecurityState transport_security_state; |
| 9579 | context.set_transport_security_state(&transport_security_state); |
| 9580 | context.Init(); |
| 9581 | |
| 9582 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9583 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9584 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9585 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9586 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9587 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9588 | r->Start(); |
| 9589 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9590 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9591 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9592 | |
| 9593 | EXPECT_EQ(1, d.response_started_count()); |
| 9594 | EXPECT_FALSE(d.received_data_before_response()); |
| 9595 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9596 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9597 | } |
| 9598 | |
| 9599 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9600 | // TransportSecurityState. |
| 9601 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 9602 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 9603 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 9604 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9605 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9606 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9607 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9608 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9609 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9610 | ASSERT_TRUE(test_server.Start()); |
| 9611 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9612 | // We require that the URL be www.google.com in order to pick up the static |
| 9613 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 9614 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 9615 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9616 | |
| 9617 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9618 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9619 | TestURLRequestContext context(true); |
| 9620 | context.set_network_delegate(&network_delegate); |
| 9621 | context.set_host_resolver(&host_resolver); |
| 9622 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9623 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9624 | TransportSecurityState::STSState static_sts_state; |
| 9625 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9626 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9627 | "www.google.com", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9628 | context.set_transport_security_state(&transport_security_state); |
| 9629 | context.Init(); |
| 9630 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9631 | TransportSecurityState::STSState dynamic_sts_state; |
| 9632 | TransportSecurityState::PKPState dynamic_pkp_state; |
| 9633 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState("www.google.com", |
| 9634 | &dynamic_sts_state)); |
| 9635 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState("www.google.com", |
| 9636 | &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9637 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9638 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9639 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9640 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9641 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9642 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9643 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9644 | r->Start(); |
| 9645 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9646 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9647 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9648 | |
| 9649 | EXPECT_EQ(1, d.response_started_count()); |
| 9650 | EXPECT_FALSE(d.received_data_before_response()); |
| 9651 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9652 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9653 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9654 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9655 | TransportSecurityState::STSState new_static_sts_state; |
| 9656 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9657 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9658 | "www.google.com", &new_static_sts_state, &new_static_pkp_state)); |
| 9659 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9660 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9661 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9662 | "www.google.com", &new_dynamic_sts_state)); |
| 9663 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9664 | "www.google.com", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9665 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9666 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9667 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9668 | static_sts_state.include_subdomains); |
| 9669 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9670 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9671 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9672 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9673 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9674 | } |
| 9675 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9676 | // Make sure HSTS preserves a POST request's method and body. |
| 9677 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9678 | static const char kData[] = "hello world"; |
| 9679 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9680 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9681 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9682 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9683 | ASSERT_TRUE(test_server.Start()); |
| 9684 | |
| 9685 | |
| 9686 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9687 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9688 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9689 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9690 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9691 | |
| 9692 | // Force https for www.somewhere.com. |
| 9693 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9694 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9695 | bool include_subdomains = false; |
| 9696 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9697 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9698 | |
| 9699 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9700 | |
| 9701 | TestURLRequestContext context(true); |
| 9702 | context.set_host_resolver(&host_resolver); |
| 9703 | context.set_transport_security_state(&transport_security_state); |
| 9704 | context.set_network_delegate(&network_delegate); |
| 9705 | context.Init(); |
| 9706 | |
| 9707 | TestDelegate d; |
| 9708 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9709 | // cause a certificate error. Ignore the error. |
| 9710 | d.set_allow_certificate_errors(true); |
| 9711 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9712 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9713 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9714 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9715 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9716 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9717 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9718 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9719 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9720 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9722 | EXPECT_EQ("https", req->url().scheme()); |
| 9723 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9724 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9725 | |
| 9726 | LoadTimingInfo load_timing_info; |
| 9727 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9728 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9729 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9730 | } |
| 9731 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9732 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9733 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9734 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9735 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9736 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9737 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9738 | ASSERT_TRUE(test_server.Start()); |
| 9739 | |
| 9740 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9741 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9742 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9743 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9744 | MockHostResolver host_resolver; |
| 9745 | |
| 9746 | TransportSecurityState transport_security_state; |
| 9747 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9748 | bool include_subdomains = false; |
| 9749 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9750 | |
| 9751 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9752 | |
| 9753 | MockCertVerifier cert_verifier; |
| 9754 | cert_verifier.set_default_result(OK); |
| 9755 | |
| 9756 | TestURLRequestContext context(true); |
| 9757 | context.set_host_resolver(&host_resolver); |
| 9758 | context.set_transport_security_state(&transport_security_state); |
| 9759 | context.set_network_delegate(&network_delegate); |
| 9760 | context.set_cert_verifier(&cert_verifier); |
| 9761 | context.Init(); |
| 9762 | |
| 9763 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9764 | test_server.host_port_pair().port())); |
| 9765 | url::Replacements<char> replacements; |
| 9766 | const char kNewScheme[] = "https"; |
| 9767 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9768 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9769 | |
| 9770 | TestDelegate d; |
| 9771 | // Quit on redirect to allow response header inspection upon redirect. |
| 9772 | d.set_quit_on_redirect(true); |
| 9773 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9774 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9775 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9776 | // Set Origin header to simulate a cross-origin request. |
| 9777 | HttpRequestHeaders request_headers; |
| 9778 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9779 | req->SetExtraRequestHeaders(request_headers); |
| 9780 | |
| 9781 | req->Start(); |
| 9782 | base::RunLoop().Run(); |
| 9783 | |
| 9784 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9785 | |
| 9786 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9787 | std::string redirect_location; |
| 9788 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 9789 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9790 | |
| 9791 | std::string received_cors_header; |
| 9792 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 9793 | &received_cors_header)); |
| 9794 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9795 | } |
| 9796 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9797 | namespace { |
| 9798 | |
| 9799 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 9800 | public: |
| 9801 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 9802 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9803 | void OnCertificateRequested(URLRequest* request, |
| 9804 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9805 | on_certificate_requested_count_++; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 9806 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9807 | } |
| 9808 | int on_certificate_requested_count() { |
| 9809 | return on_certificate_requested_count_; |
| 9810 | } |
| 9811 | private: |
| 9812 | int on_certificate_requested_count_; |
| 9813 | }; |
| 9814 | |
| 9815 | } // namespace |
| 9816 | |
| 9817 | // TODO(davidben): Test the rest of the code. Specifically, |
| 9818 | // - Filtering which certificates to select. |
| 9819 | // - Sending a certificate back. |
| 9820 | // - Getting a certificate request in an SSL renegotiation sending the |
| 9821 | // HTTP request. |
| 9822 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9823 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9824 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 9825 | ssl_config.client_cert_type = |
| 9826 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9827 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 9828 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9829 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9830 | ASSERT_TRUE(test_server.Start()); |
| 9831 | |
| 9832 | SSLClientAuthTestDelegate d; |
| 9833 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9834 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9835 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9836 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9837 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9838 | r->Start(); |
| 9839 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9840 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9841 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9842 | |
| 9843 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9844 | EXPECT_FALSE(d.received_data_before_response()); |
| 9845 | EXPECT_EQ(0, d.bytes_received()); |
| 9846 | |
| 9847 | // Send no certificate. |
| 9848 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 9849 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 9850 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9851 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9852 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9853 | |
| 9854 | EXPECT_EQ(1, d.response_started_count()); |
| 9855 | EXPECT_FALSE(d.received_data_before_response()); |
| 9856 | EXPECT_NE(0, d.bytes_received()); |
| 9857 | } |
| 9858 | } |
| 9859 | |
| 9860 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 9861 | // Test that we attempt a session resume when making two connections to the |
| 9862 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9863 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9864 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9865 | SpawnedTestServer test_server( |
| 9866 | SpawnedTestServer::TYPE_HTTPS, |
| 9867 | ssl_options, |
| 9868 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9869 | ASSERT_TRUE(test_server.Start()); |
| 9870 | |
| 9871 | SSLClientSocket::ClearSessionCache(); |
| 9872 | |
| 9873 | { |
| 9874 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9875 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9876 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9877 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9878 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9879 | r->Start(); |
| 9880 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9881 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9882 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9883 | |
| 9884 | EXPECT_EQ(1, d.response_started_count()); |
| 9885 | } |
| 9886 | |
| 9887 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 9888 | CloseAllConnections(); |
| 9889 | |
| 9890 | { |
| 9891 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9892 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9893 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9894 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9895 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9896 | r->Start(); |
| 9897 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9898 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9899 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9900 | |
| 9901 | // The response will look like; |
| 9902 | // insert abc |
| 9903 | // lookup abc |
| 9904 | // insert xyz |
| 9905 | // |
| 9906 | // With a newline at the end which makes the split think that there are |
| 9907 | // four lines. |
| 9908 | |
| 9909 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9910 | std::vector<std::string> lines = base::SplitString( |
| 9911 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9912 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 9913 | |
| 9914 | std::string session_id; |
| 9915 | |
| 9916 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9917 | std::vector<std::string> parts = base::SplitString( |
| 9918 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9919 | ASSERT_EQ(2u, parts.size()); |
| 9920 | if (i == 0) { |
| 9921 | EXPECT_EQ("insert", parts[0]); |
| 9922 | session_id = parts[1]; |
| 9923 | } else { |
| 9924 | EXPECT_EQ("lookup", parts[0]); |
| 9925 | EXPECT_EQ(session_id, parts[1]); |
| 9926 | } |
| 9927 | } |
| 9928 | } |
| 9929 | } |
| 9930 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9931 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 9932 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 9933 | // that exactly two different sessions were inserted, with no lookups etc. |
| 9934 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9935 | std::vector<std::string> lines = base::SplitString( |
| 9936 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9937 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 9938 | |
| 9939 | std::string session_id; |
| 9940 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9941 | std::vector<std::string> parts = base::SplitString( |
| 9942 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9943 | ASSERT_EQ(2u, parts.size()); |
| 9944 | EXPECT_EQ("insert", parts[0]); |
| 9945 | if (i == 0) { |
| 9946 | session_id = parts[1]; |
| 9947 | } else { |
| 9948 | EXPECT_NE(session_id, parts[1]); |
| 9949 | } |
| 9950 | } |
| 9951 | } |
| 9952 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9953 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 9954 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 9955 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9956 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9957 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9958 | SpawnedTestServer test_server( |
| 9959 | SpawnedTestServer::TYPE_HTTPS, |
| 9960 | ssl_options, |
| 9961 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9962 | ASSERT_TRUE(test_server.Start()); |
| 9963 | |
| 9964 | SSLClientSocket::ClearSessionCache(); |
| 9965 | |
| 9966 | { |
| 9967 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9968 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9969 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9970 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9972 | r->Start(); |
| 9973 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9974 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9975 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9976 | |
| 9977 | EXPECT_EQ(1, d.response_started_count()); |
| 9978 | } |
| 9979 | |
| 9980 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9981 | HttpNetworkSession::Context session_context; |
| 9982 | session_context.host_resolver = default_context_.host_resolver(); |
| 9983 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 9984 | session_context.transport_security_state = |
| 9985 | default_context_.transport_security_state(); |
| 9986 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9987 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9988 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
| 9989 | session_context.proxy_service = default_context_.proxy_service(); |
| 9990 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 9991 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9992 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9993 | session_context.http_server_properties = |
| 9994 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9995 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9996 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 9997 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9998 | std::unique_ptr<HttpCache> cache( |
| 9999 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10000 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10001 | |
| 10002 | default_context_.set_http_transaction_factory(cache.get()); |
| 10003 | |
| 10004 | { |
| 10005 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10006 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10007 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10008 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10009 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10010 | r->Start(); |
| 10011 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10012 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10013 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10014 | |
| 10015 | // The response will look like; |
| 10016 | // insert abc |
| 10017 | // insert xyz |
| 10018 | // |
| 10019 | // With a newline at the end which makes the split think that there are |
| 10020 | // three lines. |
| 10021 | |
| 10022 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10023 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10024 | } |
| 10025 | } |
| 10026 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10027 | class HTTPSFallbackTest : public testing::Test { |
| 10028 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10029 | HTTPSFallbackTest() : context_(true) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10030 | ssl_config_service_ = new TestSSLConfigService( |
| 10031 | true /* check for EV */, false /* online revocation checking */, |
| 10032 | false /* require rev. checking for local anchors */, |
| 10033 | false /* token binding enabled */); |
| 10034 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10035 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10036 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10037 | |
| 10038 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10039 | TestSSLConfigService* ssl_config_service() { |
| 10040 | return ssl_config_service_.get(); |
| 10041 | } |
| 10042 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10043 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10044 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10045 | context_.Init(); |
| 10046 | delegate_.set_allow_certificate_errors(true); |
| 10047 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10048 | SpawnedTestServer test_server( |
| 10049 | SpawnedTestServer::TYPE_HTTPS, |
| 10050 | ssl_options, |
| 10051 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10052 | ASSERT_TRUE(test_server.Start()); |
| 10053 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10054 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10055 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10056 | request_->Start(); |
| 10057 | |
| 10058 | base::RunLoop().Run(); |
| 10059 | } |
| 10060 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10061 | void ExpectConnection(int version) { |
| 10062 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10063 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10064 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10065 | request_->ssl_info().connection_status)); |
| 10066 | } |
| 10067 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10068 | void ExpectFailure(int error) { |
| 10069 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10070 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10071 | } |
| 10072 | |
| 10073 | private: |
| 10074 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10075 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10076 | std::unique_ptr<URLRequest> request_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10077 | scoped_refptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10078 | }; |
| 10079 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10080 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10081 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10082 | SpawnedTestServer::SSLOptions ssl_options( |
| 10083 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10084 | ssl_options.tls_intolerant = |
| 10085 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10086 | |
| 10087 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10088 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10089 | } |
| 10090 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10091 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10092 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10093 | SpawnedTestServer::SSLOptions ssl_options( |
| 10094 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10095 | ssl_options.tls_intolerant = |
| 10096 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10097 | |
| 10098 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10099 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10100 | } |
| 10101 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10102 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10103 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10104 | SpawnedTestServer::SSLOptions ssl_options( |
| 10105 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10106 | ssl_options.tls_intolerant = |
| 10107 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10108 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10109 | |
| 10110 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10111 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10112 | } |
| 10113 | |
| 10114 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10115 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10116 | SpawnedTestServer::SSLOptions ssl_options( |
| 10117 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10118 | ssl_options.tls_intolerant = |
| 10119 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10120 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10121 | |
| 10122 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10123 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10124 | } |
| 10125 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10126 | class HTTPSSessionTest : public testing::Test { |
| 10127 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10128 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10129 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10130 | |
| 10131 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10132 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10133 | default_context_.Init(); |
| 10134 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10135 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10136 | |
| 10137 | protected: |
| 10138 | MockCertVerifier cert_verifier_; |
| 10139 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10140 | TestURLRequestContext default_context_; |
| 10141 | }; |
| 10142 | |
| 10143 | // Tests that session resumption is not attempted if an invalid certificate |
| 10144 | // is presented. |
| 10145 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10146 | SpawnedTestServer::SSLOptions ssl_options; |
| 10147 | ssl_options.record_resume = true; |
| 10148 | SpawnedTestServer test_server( |
| 10149 | SpawnedTestServer::TYPE_HTTPS, |
| 10150 | ssl_options, |
| 10151 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10152 | ASSERT_TRUE(test_server.Start()); |
| 10153 | |
| 10154 | SSLClientSocket::ClearSessionCache(); |
| 10155 | |
| 10156 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10157 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10158 | { |
| 10159 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10160 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10161 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10162 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10163 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10164 | r->Start(); |
| 10165 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10166 | |
| 10167 | base::RunLoop().Run(); |
| 10168 | |
| 10169 | EXPECT_EQ(1, d.response_started_count()); |
| 10170 | } |
| 10171 | |
| 10172 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10173 | CloseAllConnections(); |
| 10174 | |
| 10175 | // Now change the certificate to be acceptable (so that the response is |
| 10176 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10177 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10178 | { |
| 10179 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10180 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10181 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10182 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10183 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10184 | r->Start(); |
| 10185 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10186 | |
| 10187 | base::RunLoop().Run(); |
| 10188 | |
| 10189 | // The response will look like; |
| 10190 | // insert abc |
| 10191 | // insert xyz |
| 10192 | // |
| 10193 | // With a newline at the end which makes the split think that there are |
| 10194 | // three lines. |
| 10195 | // |
| 10196 | // If a session was presented (eg: a bug), then the response would look |
| 10197 | // like; |
| 10198 | // insert abc |
| 10199 | // lookup abc |
| 10200 | // insert xyz |
| 10201 | |
| 10202 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10203 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10204 | } |
| 10205 | } |
| 10206 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10207 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10208 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10209 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10210 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10211 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10212 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10213 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10214 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10215 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10216 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10217 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10218 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10219 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10220 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10221 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10222 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10223 | // This is the policy OID contained in the certificates that testserver |
| 10224 | // generates. |
| 10225 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10226 | |
| 10227 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10228 | public: |
| 10229 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10230 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10231 | ev_test_policy_( |
| 10232 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10233 | kOCSPTestCertFingerprint, |
| 10234 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10235 | } |
| 10236 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10237 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10238 | context_.SetCTPolicyEnforcer( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10239 | std::make_unique<AllowAnyCertCTPolicyEnforcer>()); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10240 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10241 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10242 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10243 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10244 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10245 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10246 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10247 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10248 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10249 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10250 | EnsureNSSHttpIOInit(); |
| 10251 | #endif |
| 10252 | } |
| 10253 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10254 | void DoConnectionWithDelegate( |
| 10255 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10256 | TestDelegate* delegate, |
| 10257 | SSLInfo* out_ssl_info) { |
| 10258 | // Always overwrite |out_ssl_info|. |
| 10259 | out_ssl_info->Reset(); |
| 10260 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10261 | SpawnedTestServer test_server( |
| 10262 | SpawnedTestServer::TYPE_HTTPS, |
| 10263 | ssl_options, |
| 10264 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10265 | ASSERT_TRUE(test_server.Start()); |
| 10266 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10267 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10268 | std::unique_ptr<URLRequest> r( |
| 10269 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10270 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10271 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10272 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10273 | base::RunLoop().Run(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10274 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10275 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10276 | *out_ssl_info = r->ssl_info(); |
| 10277 | } |
| 10278 | |
| 10279 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10280 | CertStatus* out_cert_status) { |
| 10281 | // Always overwrite |out_cert_status|. |
| 10282 | *out_cert_status = 0; |
| 10283 | |
| 10284 | TestDelegate d; |
| 10285 | SSLInfo ssl_info; |
| 10286 | ASSERT_NO_FATAL_FAILURE( |
| 10287 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10288 | |
| 10289 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10290 | } |
| 10291 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10292 | ~HTTPSOCSPTest() override { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10293 | #if defined(USE_NSS_CERTS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10294 | ShutdownNSSHttpIO(); |
| 10295 | #endif |
| 10296 | } |
| 10297 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10298 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10299 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 10300 | public: |
| 10301 | AllowAnyCertCTPolicyEnforcer() = default; |
| 10302 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 10303 | |
| 10304 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 10305 | X509Certificate* cert, |
| 10306 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10307 | const NetLogWithSource& net_log) override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10308 | return ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS; |
| 10309 | } |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10310 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10311 | // SetupContext configures the URLRequestContext that will be used for making |
| 10312 | // connetions to testserver. This can be overridden in test subclasses for |
| 10313 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10314 | virtual void SetupContext() { |
| 10315 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 10316 | true /* check for EV */, true /* online revocation checking */, |
| 10317 | false /* require rev. checking for local |
| 10318 | anchors */, |
| 10319 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10320 | } |
| 10321 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10322 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10323 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10324 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10325 | }; |
| 10326 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10327 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10328 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10329 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10330 | // have that ability on other platforms. |
| 10331 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10332 | #else |
| 10333 | return 0; |
| 10334 | #endif |
| 10335 | } |
| 10336 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10337 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10338 | // operating system supports revocation checking and can distinguish between |
| 10339 | // situations where a given certificate lacks any revocation information (eg: |
| 10340 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10341 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10342 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10343 | // skipped. |
| 10344 | static bool SystemSupportsHardFailRevocationChecking() { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10345 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10346 | return true; |
| 10347 | #else |
| 10348 | return false; |
| 10349 | #endif |
| 10350 | } |
| 10351 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10352 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10353 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10354 | // several tests are effected because our testing EV certificate won't be |
| 10355 | // recognised as EV. |
| 10356 | static bool SystemUsesChromiumEVMetadata() { |
davidben | 965721b | 2017-05-03 19:40:31 | [diff] [blame] | 10357 | #if defined(OS_ANDROID) |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 10358 | // On Android, we use the system to tell us whether a certificate is EV or not |
| 10359 | // and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10360 | return false; |
| 10361 | #else |
| 10362 | return true; |
| 10363 | #endif |
| 10364 | } |
| 10365 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10366 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10367 | // check failure as a result of checking a test EV cert, which will not |
| 10368 | // actually trigger an online revocation check on some platforms. |
| 10369 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10370 | if (SystemUsesChromiumEVMetadata()) { |
| 10371 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10372 | } else { |
| 10373 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10374 | // be enabled, and thus there will not be a revocation check to fail. |
| 10375 | return 0u; |
| 10376 | } |
| 10377 | } |
| 10378 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10379 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | 884169a0 | 2017-09-06 18:58:02 | [diff] [blame] | 10380 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10381 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
Sergey Ulanov | 884169a0 | 2017-09-06 18:58:02 | [diff] [blame] | 10382 | // TODO(crbug.com/762380): Enable on Fuchsia once it's implemented. |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10383 | return false; |
| 10384 | #else |
| 10385 | return true; |
| 10386 | #endif |
| 10387 | } |
| 10388 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10389 | static bool SystemSupportsOCSPStapling() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 10390 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10391 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10392 | #else |
| 10393 | return false; |
| 10394 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10395 | } |
| 10396 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10397 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10398 | if (!SystemSupportsOCSP()) { |
| 10399 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10400 | return; |
| 10401 | } |
| 10402 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10403 | SpawnedTestServer::SSLOptions ssl_options( |
| 10404 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10405 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10406 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10407 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10408 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10409 | |
| 10410 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10411 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10412 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10413 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10414 | |
| 10415 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10416 | } |
| 10417 | |
| 10418 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10419 | if (!SystemSupportsOCSP()) { |
| 10420 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10421 | return; |
| 10422 | } |
| 10423 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10424 | SpawnedTestServer::SSLOptions ssl_options( |
| 10425 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10426 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10427 | |
| 10428 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10429 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10430 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10431 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10432 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10433 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10434 | } |
| 10435 | |
| 10436 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10437 | if (!SystemSupportsOCSP()) { |
| 10438 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10439 | return; |
| 10440 | } |
| 10441 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10442 | SpawnedTestServer::SSLOptions ssl_options( |
| 10443 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10444 | ssl_options.ocsp_status = |
| 10445 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10446 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10447 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10448 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10449 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10450 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10451 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10452 | |
| 10453 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10454 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10455 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10456 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10457 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10458 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10459 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10460 | LOG(WARNING) |
| 10461 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10462 | return; |
| 10463 | } |
| 10464 | |
| 10465 | SpawnedTestServer::SSLOptions ssl_options( |
| 10466 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10467 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10468 | ssl_options.staple_ocsp_response = true; |
| 10469 | ssl_options.ocsp_server_unavailable = true; |
| 10470 | |
| 10471 | CertStatus cert_status; |
| 10472 | DoConnection(ssl_options, &cert_status); |
| 10473 | |
| 10474 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10475 | |
| 10476 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10477 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10478 | |
| 10479 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10480 | } |
| 10481 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10482 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 10483 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10484 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 10485 | #else |
| 10486 | #define MAYBE_RevokedStapled RevokedStapled |
| 10487 | #endif |
| 10488 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10489 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10490 | LOG(WARNING) |
| 10491 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10492 | return; |
| 10493 | } |
| 10494 | |
| 10495 | SpawnedTestServer::SSLOptions ssl_options( |
| 10496 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10497 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10498 | ssl_options.staple_ocsp_response = true; |
| 10499 | ssl_options.ocsp_server_unavailable = true; |
| 10500 | |
| 10501 | CertStatus cert_status; |
| 10502 | DoConnection(ssl_options, &cert_status); |
| 10503 | |
| 10504 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10505 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10506 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10507 | } |
| 10508 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10509 | TEST_F(HTTPSOCSPTest, ExpectStapleReportSentOnMissing) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 10510 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 10511 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 10512 | #endif |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10513 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10514 | https_test_server.SetSSLConfig( |
| 10515 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10516 | https_test_server.ServeFilesFromSourceDirectory( |
| 10517 | base::FilePath(kTestFilePath)); |
| 10518 | ASSERT_TRUE(https_test_server.Start()); |
| 10519 | |
| 10520 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10521 | // but not provide any OCSP information. |
| 10522 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10523 | ASSERT_TRUE(cert); |
| 10524 | MockCertVerifier cert_verifier; |
| 10525 | CertVerifyResult verify_result; |
| 10526 | verify_result.verified_cert = cert; |
| 10527 | verify_result.is_issued_by_known_root = true; |
| 10528 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10529 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10530 | |
| 10531 | // Catch the Expect-Staple report. |
| 10532 | TransportSecurityState transport_security_state; |
| 10533 | MockCertificateReportSender mock_report_sender; |
| 10534 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10535 | |
| 10536 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10537 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10538 | MockHostResolver host_resolver; |
| 10539 | TestNetworkDelegate network_delegate; |
| 10540 | TestURLRequestContext context(true); |
| 10541 | context.set_host_resolver(&host_resolver); |
| 10542 | context.set_transport_security_state(&transport_security_state); |
| 10543 | context.set_network_delegate(&network_delegate); |
| 10544 | context.set_cert_verifier(&cert_verifier); |
| 10545 | context.Init(); |
| 10546 | |
| 10547 | // Now send a request to trigger the violation. |
| 10548 | TestDelegate d; |
| 10549 | GURL url = https_test_server.GetURL("/"); |
| 10550 | GURL::Replacements replace_host; |
| 10551 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10552 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10553 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10554 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10555 | violating_request->Start(); |
| 10556 | base::RunLoop().Run(); |
| 10557 | |
| 10558 | // Confirm a report was sent. |
| 10559 | EXPECT_FALSE(mock_report_sender.latest_report().empty()); |
| 10560 | EXPECT_EQ(GURL(kExpectStapleReportURI), |
| 10561 | mock_report_sender.latest_report_uri()); |
| 10562 | } |
| 10563 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10564 | // Tests that Expect-Staple reports are not sent for connections on which there |
| 10565 | // is a certificate error. |
| 10566 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnMissingWithCertError) { |
| 10567 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10568 | https_test_server.SetSSLConfig( |
| 10569 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10570 | https_test_server.ServeFilesFromSourceDirectory( |
| 10571 | base::FilePath(kTestFilePath)); |
| 10572 | ASSERT_TRUE(https_test_server.Start()); |
| 10573 | |
| 10574 | // Set up a MockCertVerifier to report an error for the certificate |
| 10575 | // and indicate that there was no stapled OCSP response. |
| 10576 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10577 | ASSERT_TRUE(cert); |
| 10578 | MockCertVerifier cert_verifier; |
| 10579 | CertVerifyResult verify_result; |
| 10580 | verify_result.cert_status = CERT_STATUS_DATE_INVALID; |
| 10581 | verify_result.verified_cert = cert; |
| 10582 | verify_result.is_issued_by_known_root = true; |
| 10583 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10584 | cert_verifier.AddResultForCert(cert.get(), verify_result, |
| 10585 | ERR_CERT_DATE_INVALID); |
| 10586 | |
| 10587 | // Set up a mock report sender so that the test can check that an |
| 10588 | // Expect-Staple report is not sent. |
| 10589 | TransportSecurityState transport_security_state; |
| 10590 | MockCertificateReportSender mock_report_sender; |
| 10591 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10592 | |
| 10593 | TestNetworkDelegate network_delegate; |
| 10594 | TestURLRequestContext context(true); |
| 10595 | |
| 10596 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10597 | MockHostResolver host_resolver; |
| 10598 | context.set_host_resolver(&host_resolver); |
| 10599 | |
| 10600 | context.set_transport_security_state(&transport_security_state); |
| 10601 | context.set_network_delegate(&network_delegate); |
| 10602 | context.set_cert_verifier(&cert_verifier); |
| 10603 | context.Init(); |
| 10604 | |
| 10605 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10606 | // |verify_result| used with the |cert_verifier| will indicate a certificate |
| 10607 | // error, an Expect-Staple report should not be sent. |
| 10608 | TestDelegate d; |
| 10609 | GURL url = https_test_server.GetURL("/"); |
| 10610 | GURL::Replacements replace_host; |
| 10611 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10612 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10613 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10614 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10615 | violating_request->Start(); |
| 10616 | base::RunLoop().Run(); |
| 10617 | |
| 10618 | // Confirm a report was not sent. |
| 10619 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10620 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10621 | } |
| 10622 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10623 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnValid) { |
| 10624 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10625 | https_test_server.SetSSLConfig( |
| 10626 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10627 | https_test_server.ServeFilesFromSourceDirectory( |
| 10628 | base::FilePath(kTestFilePath)); |
| 10629 | ASSERT_TRUE(https_test_server.Start()); |
| 10630 | |
| 10631 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10632 | // and provide GOOD revocation status. |
| 10633 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10634 | ASSERT_TRUE(cert); |
| 10635 | MockCertVerifier cert_verifier; |
| 10636 | CertVerifyResult verify_result; |
| 10637 | verify_result.verified_cert = cert; |
| 10638 | verify_result.is_issued_by_known_root = true; |
| 10639 | verify_result.ocsp_result.response_status = OCSPVerifyResult::PROVIDED; |
| 10640 | verify_result.ocsp_result.revocation_status = OCSPRevocationStatus::GOOD; |
| 10641 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10642 | |
| 10643 | // Catch the Expect-Staple report. |
| 10644 | TransportSecurityState transport_security_state; |
| 10645 | MockCertificateReportSender mock_report_sender; |
| 10646 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10647 | |
| 10648 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10649 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10650 | MockHostResolver host_resolver; |
| 10651 | TestNetworkDelegate network_delegate; |
| 10652 | TestURLRequestContext context(true); |
| 10653 | context.set_host_resolver(&host_resolver); |
| 10654 | context.set_transport_security_state(&transport_security_state); |
| 10655 | context.set_network_delegate(&network_delegate); |
| 10656 | context.set_cert_verifier(&cert_verifier); |
| 10657 | context.Init(); |
| 10658 | |
| 10659 | // This request should not not trigger an Expect-Staple violation. |
| 10660 | TestDelegate d; |
| 10661 | GURL url = https_test_server.GetURL("/"); |
| 10662 | GURL::Replacements replace_host; |
| 10663 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10664 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10665 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10666 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10667 | ok_request->Start(); |
| 10668 | base::RunLoop().Run(); |
| 10669 | |
| 10670 | // Check that no report was sent. |
| 10671 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10672 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10673 | } |
| 10674 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10675 | // Tests that an Expect-Staple report is not sent when OCSP details are not |
| 10676 | // checked on the connection. |
| 10677 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnNotChecked) { |
| 10678 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10679 | https_test_server.SetSSLConfig( |
| 10680 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10681 | https_test_server.ServeFilesFromSourceDirectory( |
| 10682 | base::FilePath(kTestFilePath)); |
| 10683 | ASSERT_TRUE(https_test_server.Start()); |
| 10684 | |
| 10685 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10686 | // and set |ocsp_result| to indicate that OCSP stapling details were not |
| 10687 | // checked on the connection. |
| 10688 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10689 | ASSERT_TRUE(cert); |
| 10690 | MockCertVerifier cert_verifier; |
| 10691 | CertVerifyResult verify_result; |
| 10692 | verify_result.verified_cert = cert; |
| 10693 | verify_result.is_issued_by_known_root = true; |
| 10694 | verify_result.ocsp_result.response_status = OCSPVerifyResult::NOT_CHECKED; |
| 10695 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10696 | |
| 10697 | // Set up a mock report sender so that the test can check that an |
| 10698 | // Expect-Staple report is not sent. |
| 10699 | TransportSecurityState transport_security_state; |
| 10700 | MockCertificateReportSender mock_report_sender; |
| 10701 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10702 | |
| 10703 | TestNetworkDelegate network_delegate; |
| 10704 | TestURLRequestContext context(true); |
| 10705 | |
| 10706 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10707 | MockHostResolver host_resolver; |
| 10708 | context.set_host_resolver(&host_resolver); |
| 10709 | |
| 10710 | context.set_transport_security_state(&transport_security_state); |
| 10711 | context.set_network_delegate(&network_delegate); |
| 10712 | context.set_cert_verifier(&cert_verifier); |
| 10713 | context.Init(); |
| 10714 | |
| 10715 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10716 | // |verify_result| used with the |cert_verifier| will indicate that OCSP |
| 10717 | // stapling details were not checked on the connection, an Expect-Staple |
| 10718 | // report should not be sent. |
| 10719 | TestDelegate d; |
| 10720 | GURL url = https_test_server.GetURL("/"); |
| 10721 | GURL::Replacements replace_host; |
| 10722 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10723 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10724 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10725 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10726 | ok_request->Start(); |
| 10727 | base::RunLoop().Run(); |
| 10728 | |
| 10729 | // Check that no report was sent. |
| 10730 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10731 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10732 | } |
| 10733 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10734 | static const struct OCSPVerifyTestData { |
| 10735 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 10736 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 10737 | OCSPVerifyResult::ResponseStatus response_status; |
| 10738 | bool has_revocation_status; |
| 10739 | OCSPRevocationStatus cert_status; |
| 10740 | } kOCSPVerifyData[] = { |
| 10741 | |
| 10742 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10743 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10744 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10745 | OCSPVerifyResult::PROVIDED, |
| 10746 | true, |
| 10747 | OCSPRevocationStatus::GOOD}, |
| 10748 | |
| 10749 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10750 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10751 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10752 | OCSPVerifyResult::INVALID_DATE, |
| 10753 | false, |
| 10754 | OCSPRevocationStatus::UNKNOWN}, |
| 10755 | |
| 10756 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10757 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10758 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10759 | OCSPVerifyResult::INVALID_DATE, |
| 10760 | false, |
| 10761 | OCSPRevocationStatus::UNKNOWN}, |
| 10762 | |
| 10763 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10764 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10765 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10766 | OCSPVerifyResult::INVALID_DATE, |
| 10767 | false, |
| 10768 | OCSPRevocationStatus::UNKNOWN}, |
| 10769 | |
| 10770 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10771 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10772 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10773 | OCSPVerifyResult::INVALID_DATE, |
| 10774 | false, |
| 10775 | OCSPRevocationStatus::UNKNOWN}, |
| 10776 | |
| 10777 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 10778 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10779 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10780 | OCSPVerifyResult::ERROR_RESPONSE, |
| 10781 | false, |
| 10782 | OCSPRevocationStatus::UNKNOWN}, |
| 10783 | |
| 10784 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 10785 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10786 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10787 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 10788 | false, |
| 10789 | OCSPRevocationStatus::UNKNOWN}, |
| 10790 | |
| 10791 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 10792 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10793 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10794 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 10795 | false, |
| 10796 | OCSPRevocationStatus::UNKNOWN}, |
| 10797 | |
| 10798 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10799 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10800 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10801 | OCSPVerifyResult::INVALID_DATE, |
| 10802 | false, |
| 10803 | OCSPRevocationStatus::UNKNOWN}, |
| 10804 | |
| 10805 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10806 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10807 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10808 | OCSPVerifyResult::PROVIDED, |
| 10809 | true, |
| 10810 | OCSPRevocationStatus::UNKNOWN}, |
| 10811 | |
| 10812 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10813 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10814 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10815 | OCSPVerifyResult::INVALID_DATE, |
| 10816 | false, |
| 10817 | OCSPRevocationStatus::UNKNOWN}, |
| 10818 | |
| 10819 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10820 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10821 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10822 | OCSPVerifyResult::INVALID_DATE, |
| 10823 | false, |
| 10824 | OCSPRevocationStatus::UNKNOWN}, |
| 10825 | |
| 10826 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10827 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10828 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 10829 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10830 | false, |
| 10831 | OCSPRevocationStatus::UNKNOWN}, |
| 10832 | |
| 10833 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10834 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10835 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10836 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10837 | false, |
| 10838 | OCSPRevocationStatus::UNKNOWN}, |
| 10839 | |
| 10840 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10841 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10842 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10843 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10844 | false, |
| 10845 | OCSPRevocationStatus::UNKNOWN}, |
| 10846 | |
| 10847 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10848 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10849 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10850 | OCSPVerifyResult::PROVIDED, |
| 10851 | true, |
| 10852 | OCSPRevocationStatus::REVOKED}, |
| 10853 | |
| 10854 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10855 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10856 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10857 | OCSPVerifyResult::INVALID_DATE, |
| 10858 | false, |
| 10859 | OCSPRevocationStatus::UNKNOWN}, |
| 10860 | |
| 10861 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10862 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10863 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10864 | OCSPVerifyResult::INVALID_DATE, |
| 10865 | false, |
| 10866 | OCSPRevocationStatus::UNKNOWN}, |
| 10867 | |
| 10868 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10869 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10870 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10871 | OCSPVerifyResult::PROVIDED, |
| 10872 | true, |
| 10873 | OCSPRevocationStatus::GOOD}, |
| 10874 | |
| 10875 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10876 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10877 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10878 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10879 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10880 | OCSPVerifyResult::PROVIDED, |
| 10881 | true, |
| 10882 | OCSPRevocationStatus::GOOD}, |
| 10883 | |
| 10884 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10885 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10886 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10887 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10888 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10889 | OCSPVerifyResult::PROVIDED, |
| 10890 | true, |
| 10891 | OCSPRevocationStatus::GOOD}, |
| 10892 | |
| 10893 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10894 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10895 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10896 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10897 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10898 | OCSPVerifyResult::PROVIDED, |
| 10899 | true, |
| 10900 | OCSPRevocationStatus::GOOD}, |
| 10901 | |
| 10902 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10903 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10904 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10905 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10906 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10907 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10908 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10909 | OCSPVerifyResult::INVALID_DATE, |
| 10910 | false, |
| 10911 | OCSPRevocationStatus::UNKNOWN}, |
| 10912 | |
| 10913 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10914 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10915 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10916 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10917 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10918 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10919 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10920 | OCSPVerifyResult::PROVIDED, |
| 10921 | true, |
| 10922 | OCSPRevocationStatus::REVOKED}, |
| 10923 | |
| 10924 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10925 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10926 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10927 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10928 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10929 | OCSPVerifyResult::PROVIDED, |
| 10930 | true, |
| 10931 | OCSPRevocationStatus::UNKNOWN}, |
| 10932 | |
| 10933 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10934 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10935 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10936 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10937 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10938 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10939 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10940 | OCSPVerifyResult::PROVIDED, |
| 10941 | true, |
| 10942 | OCSPRevocationStatus::UNKNOWN}, |
| 10943 | |
| 10944 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10945 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10946 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10947 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10948 | false, |
| 10949 | OCSPRevocationStatus::UNKNOWN}, |
| 10950 | |
| 10951 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10952 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10953 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10954 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10955 | false, |
| 10956 | OCSPRevocationStatus::UNKNOWN}, |
| 10957 | |
| 10958 | }; |
| 10959 | |
| 10960 | class HTTPSOCSPVerifyTest |
| 10961 | : public HTTPSOCSPTest, |
| 10962 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 10963 | |
| 10964 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 10965 | SpawnedTestServer::SSLOptions ssl_options( |
| 10966 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10967 | OCSPVerifyTestData test = GetParam(); |
| 10968 | |
| 10969 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 10970 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 10971 | ssl_options.staple_ocsp_response = true; |
| 10972 | |
| 10973 | SSLInfo ssl_info; |
| 10974 | OCSPErrorTestDelegate delegate; |
| 10975 | ASSERT_NO_FATAL_FAILURE( |
| 10976 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 10977 | |
| 10978 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 10979 | // URLRequest caches certificate errors. |
| 10980 | if (delegate.have_certificate_errors()) { |
| 10981 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 10982 | ssl_info = delegate.ssl_info(); |
| 10983 | } |
| 10984 | |
| 10985 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 10986 | |
| 10987 | if (test.has_revocation_status) |
| 10988 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 10989 | } |
| 10990 | |
| 10991 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 10992 | HTTPSOCSPVerifyTest, |
| 10993 | testing::ValuesIn(kOCSPVerifyData)); |
| 10994 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10995 | static bool SystemSupportsAIA() { |
Sergey Ulanov | 884169a0 | 2017-09-06 18:58:02 | [diff] [blame] | 10996 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
| 10997 | // TODO(crbug.com/762380): Enable on Fuchsia once it's implemented. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10998 | return false; |
| 10999 | #else |
| 11000 | return true; |
| 11001 | #endif |
| 11002 | } |
| 11003 | |
| 11004 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11005 | public: |
| 11006 | void SetupContext() override { |
| 11007 | context_.set_ssl_config_service(new TestSSLConfigService( |
| 11008 | false /* check for EV */, false /* online revocation checking */, |
| 11009 | false /* require rev. checking for local anchors */, |
| 11010 | false /* token binding enabled */)); |
| 11011 | } |
| 11012 | }; |
| 11013 | |
| 11014 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11015 | SpawnedTestServer::SSLOptions ssl_options( |
| 11016 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11017 | SpawnedTestServer test_server( |
| 11018 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11019 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11020 | ASSERT_TRUE(test_server.Start()); |
| 11021 | |
| 11022 | TestDelegate d; |
| 11023 | d.set_allow_certificate_errors(true); |
| 11024 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11025 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11026 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11027 | |
| 11028 | r->Start(); |
| 11029 | EXPECT_TRUE(r->is_pending()); |
| 11030 | |
| 11031 | base::RunLoop().Run(); |
| 11032 | |
| 11033 | EXPECT_EQ(1, d.response_started_count()); |
| 11034 | |
| 11035 | CertStatus cert_status = r->ssl_info().cert_status; |
| 11036 | if (SystemSupportsAIA()) { |
| 11037 | EXPECT_EQ(OK, d.request_status()); |
| 11038 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11039 | ASSERT_TRUE(r->ssl_info().cert); |
| 11040 | EXPECT_EQ(2u, r->ssl_info().cert->GetIntermediateCertificates().size()); |
| 11041 | } else { |
| 11042 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, |
| 11043 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11044 | } |
| 11045 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
| 11046 | EXPECT_EQ( |
| 11047 | 0u, r->ssl_info().unverified_cert->GetIntermediateCertificates().size()); |
| 11048 | } |
| 11049 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11050 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11051 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11052 | void SetupContext() override { |
| 11053 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11054 | false /* check for EV */, false /* online revocation checking */, |
| 11055 | true /* require rev. checking for local |
| 11056 | anchors */, |
| 11057 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11058 | } |
| 11059 | }; |
| 11060 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11061 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11062 | if (!SystemSupportsOCSP()) { |
| 11063 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11064 | return; |
| 11065 | } |
| 11066 | |
| 11067 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11068 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11069 | << "revocation checking"; |
| 11070 | return; |
| 11071 | } |
| 11072 | |
| 11073 | SpawnedTestServer::SSLOptions ssl_options( |
| 11074 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11075 | ssl_options.ocsp_status = |
| 11076 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11077 | |
| 11078 | CertStatus cert_status; |
| 11079 | DoConnection(ssl_options, &cert_status); |
| 11080 | |
| 11081 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 11082 | cert_status & CERT_STATUS_REVOKED); |
| 11083 | |
| 11084 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11085 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11086 | } |
| 11087 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11088 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11089 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11090 | void SetupContext() override { |
| 11091 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11092 | true /* check for EV */, false /* online revocation checking */, |
| 11093 | false /* require rev. checking for local |
| 11094 | anchors */, |
| 11095 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11096 | } |
| 11097 | }; |
| 11098 | |
| 11099 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11100 | if (!SystemSupportsOCSP()) { |
| 11101 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11102 | return; |
| 11103 | } |
| 11104 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11105 | SpawnedTestServer::SSLOptions ssl_options( |
| 11106 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11107 | ssl_options.ocsp_status = |
| 11108 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11109 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11110 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11111 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11112 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11113 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11114 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11115 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11116 | |
| 11117 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11118 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11119 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11120 | } |
| 11121 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11122 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11123 | if (!SystemSupportsOCSP()) { |
| 11124 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11125 | return; |
| 11126 | } |
| 11127 | |
| 11128 | SpawnedTestServer::SSLOptions ssl_options( |
| 11129 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11130 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11131 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11132 | |
| 11133 | CertStatus cert_status; |
| 11134 | DoConnection(ssl_options, &cert_status); |
| 11135 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11136 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11137 | // possible to determine whether the check failed because of actual |
| 11138 | // revocation or because there was an OCSP failure. |
| 11139 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11140 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11141 | #else |
| 11142 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11143 | #endif |
| 11144 | |
| 11145 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11146 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11147 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11148 | } |
| 11149 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11150 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11151 | if (!SystemSupportsOCSP()) { |
| 11152 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11153 | return; |
| 11154 | } |
| 11155 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11156 | SpawnedTestServer::SSLOptions ssl_options( |
| 11157 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11158 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11159 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11160 | |
| 11161 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11162 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11163 | |
| 11164 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11165 | |
| 11166 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11167 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11168 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11169 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11170 | } |
| 11171 | |
| 11172 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11173 | if (!SystemSupportsOCSP()) { |
| 11174 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11175 | return; |
| 11176 | } |
| 11177 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11178 | SpawnedTestServer::SSLOptions ssl_options( |
| 11179 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11180 | ssl_options.ocsp_status = |
| 11181 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11182 | SSLConfigService::SetCRLSet( |
| 11183 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11184 | |
| 11185 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11186 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11187 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11188 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11189 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11190 | |
| 11191 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11192 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11193 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11194 | } |
| 11195 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11196 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11197 | if (!SystemSupportsOCSP()) { |
| 11198 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11199 | return; |
| 11200 | } |
| 11201 | |
| 11202 | SpawnedTestServer::SSLOptions ssl_options( |
| 11203 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11204 | ssl_options.ocsp_status = |
| 11205 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11206 | SSLConfigService::SetCRLSet( |
| 11207 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 11208 | false, &kOCSPTestCertSPKI, ""))); |
| 11209 | |
| 11210 | CertStatus cert_status; |
| 11211 | DoConnection(ssl_options, &cert_status); |
| 11212 | |
| 11213 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11214 | // revocation check for EV. |
| 11215 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11216 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11217 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11218 | EXPECT_FALSE( |
| 11219 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11220 | } |
| 11221 | |
| 11222 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11223 | if (!SystemSupportsOCSP()) { |
| 11224 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11225 | return; |
| 11226 | } |
| 11227 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11228 | SpawnedTestServer::SSLOptions ssl_options( |
| 11229 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11230 | ssl_options.ocsp_status = |
| 11231 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11232 | SSLConfigService::SetCRLSet( |
| 11233 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 11234 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11235 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11236 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11237 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11238 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11239 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11240 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11241 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11242 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11243 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11244 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11245 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11246 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11247 | } |
| 11248 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11249 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 11250 | // Test that when EV verification is requested, but online revocation |
| 11251 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 11252 | // no revocation checking actually happens. |
| 11253 | if (!SystemSupportsOCSP()) { |
| 11254 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11255 | return; |
| 11256 | } |
| 11257 | |
| 11258 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11259 | // checking (as per the user preference) |
| 11260 | ev_test_policy_.reset(); |
| 11261 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11262 | SpawnedTestServer::SSLOptions ssl_options( |
| 11263 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11264 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11265 | SSLConfigService::SetCRLSet( |
| 11266 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11267 | |
| 11268 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11269 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11270 | |
| 11271 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11272 | |
| 11273 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11274 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11275 | } |
| 11276 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11277 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11278 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11279 | void SetupContext() override { |
| 11280 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11281 | false /* check for EV */, false /* online revocation checking */, |
| 11282 | false /* require rev. checking for local |
| 11283 | anchors */, |
| 11284 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11285 | } |
| 11286 | }; |
| 11287 | |
| 11288 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11289 | SpawnedTestServer::SSLOptions ssl_options( |
| 11290 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11291 | ssl_options.ocsp_status = |
| 11292 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11293 | SSLConfigService::SetCRLSet( |
| 11294 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11295 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11296 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11297 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11298 | |
| 11299 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11300 | // we don't fall back to online revocation checks. |
| 11301 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11302 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11303 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11304 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11305 | |
| 11306 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Sergey Ulanov | 884169a0 | 2017-09-06 18:58:02 | [diff] [blame] | 11307 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
| 11308 | // TODO(crbug.com/762380): Enable on Fuchsia once it's implemented. |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11309 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11310 | return; |
| 11311 | #endif |
| 11312 | |
| 11313 | SpawnedTestServer::SSLOptions ssl_options( |
| 11314 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11315 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11316 | ssl_options.cert_serial = 10; |
| 11317 | SSLConfigService::SetCRLSet( |
| 11318 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 11319 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 11320 | |
| 11321 | CertStatus cert_status = 0; |
| 11322 | DoConnection(ssl_options, &cert_status); |
| 11323 | |
| 11324 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11325 | // reflected without online revocation checking. |
| 11326 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11327 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11328 | EXPECT_FALSE( |
| 11329 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11330 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11331 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11332 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11333 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11334 | !defined(OS_FUCHSIA) |
| 11335 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11336 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11337 | // connections reliably. FTP tests are disabled on platforms that use |
| 11338 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11339 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11340 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11341 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11342 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11343 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11344 | // Can't use |default_context_|'s HostResolver to set up the |
| 11345 | // FTPTransactionFactory because it hasn't been created yet. |
| 11346 | default_context_.set_host_resolver(&host_resolver_); |
| 11347 | } |
| 11348 | |
| 11349 | // URLRequestTest interface: |
| 11350 | void SetUpFactory() override { |
| 11351 | // Add FTP support to the default URLRequestContext. |
| 11352 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11353 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11354 | } |
| 11355 | |
| 11356 | std::string GetTestFileContents() { |
| 11357 | base::FilePath path; |
| 11358 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 11359 | path = path.Append(kTestFilePath); |
| 11360 | path = path.AppendASCII(kFtpTestFile); |
| 11361 | std::string contents; |
| 11362 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11363 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11364 | } |
| 11365 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11366 | protected: |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11367 | // Note that this is destroyed before the FtpProtocolHandler that references |
| 11368 | // it, which is owned by the parent class. Since no requests are made during |
| 11369 | // teardown, this works, though it's not great. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11370 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11371 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11372 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11373 | }; |
| 11374 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11375 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11376 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11377 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11378 | |
| 11379 | TestDelegate d; |
| 11380 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11381 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11382 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11383 | r->Start(); |
| 11384 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11385 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11386 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11387 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11388 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11389 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11390 | } |
| 11391 | } |
| 11392 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11393 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11394 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11395 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11396 | TestDelegate d; |
| 11397 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11398 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11399 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11400 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11401 | r->Start(); |
| 11402 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11403 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11404 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11405 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11406 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11407 | EXPECT_EQ(1, d.response_started_count()); |
| 11408 | EXPECT_FALSE(d.received_data_before_response()); |
| 11409 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11410 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11411 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11412 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11413 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11414 | } |
| 11415 | } |
| 11416 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11417 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11418 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11419 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11420 | TestDelegate d; |
| 11421 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11422 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11423 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11424 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11425 | r->Start(); |
| 11426 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11427 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11428 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11429 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11430 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11431 | EXPECT_EQ(1, d.response_started_count()); |
| 11432 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11433 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11434 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11435 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11436 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11437 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11438 | } |
| 11439 | } |
| 11440 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11441 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11442 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11443 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11444 | TestDelegate d; |
| 11445 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11446 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11447 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11448 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11449 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11450 | r->Start(); |
| 11451 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11452 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11453 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11454 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11455 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11456 | EXPECT_EQ(1, d.response_started_count()); |
| 11457 | EXPECT_FALSE(d.received_data_before_response()); |
| 11458 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11459 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11460 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11461 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11462 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11463 | |
| 11464 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11465 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11466 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11467 | } |
| 11468 | } |
| 11469 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11470 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11471 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11472 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11473 | TestDelegate d; |
| 11474 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11475 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11476 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11477 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11478 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11479 | r->Start(); |
| 11480 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11481 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11482 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11483 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11484 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11485 | EXPECT_EQ(1, d.response_started_count()); |
| 11486 | EXPECT_FALSE(d.received_data_before_response()); |
| 11487 | EXPECT_EQ(d.bytes_received(), 0); |
| 11488 | } |
| 11489 | } |
| 11490 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11491 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11492 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11493 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11494 | TestDelegate d; |
| 11495 | // Set correct login credentials. The delegate will be asked for them when |
| 11496 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11497 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11498 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11499 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11500 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11501 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11502 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11503 | r->Start(); |
| 11504 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11505 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11506 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11507 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11508 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11509 | EXPECT_EQ(1, d.response_started_count()); |
| 11510 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11511 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11512 | } |
| 11513 | } |
| 11514 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11515 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11516 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11517 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11518 | TestDelegate d; |
| 11519 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11520 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11521 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11522 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11523 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11524 | r->Start(); |
| 11525 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11526 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11527 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11528 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11529 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11530 | EXPECT_EQ(1, d.response_started_count()); |
| 11531 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11532 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11533 | } |
| 11534 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11535 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11536 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11537 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11538 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11539 | TestDelegate d; |
| 11540 | // Set correct login credentials. The delegate will be asked for them when |
| 11541 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11542 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11543 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11544 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11545 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11546 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11547 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11548 | r->Start(); |
| 11549 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11550 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11551 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11552 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11553 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11554 | EXPECT_EQ(1, d.response_started_count()); |
| 11555 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11556 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11557 | } |
| 11558 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11559 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11560 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11561 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11562 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11563 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11564 | { |
| 11565 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11566 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11567 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11568 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11569 | DEFAULT_PRIORITY, d.get(), 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] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11572 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11573 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11574 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11575 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11576 | EXPECT_EQ(1, d->response_started_count()); |
| 11577 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11578 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11579 | } |
| 11580 | |
| 11581 | d.reset(new TestDelegate); |
| 11582 | { |
| 11583 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11584 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11585 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11586 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11587 | r->Start(); |
| 11588 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11589 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11590 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11591 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11592 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11593 | EXPECT_EQ(1, d->response_started_count()); |
| 11594 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11595 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11596 | } |
| 11597 | } |
| 11598 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11599 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11600 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11601 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11602 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11603 | // Set correct login credentials. The delegate will be asked for them when |
| 11604 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11605 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11606 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11607 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11608 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11609 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11610 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11611 | r->Start(); |
| 11612 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11613 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11614 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11615 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11616 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11617 | EXPECT_EQ(1, d->response_started_count()); |
| 11618 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11619 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11620 | } |
| 11621 | |
| 11622 | // Use a new delegate without explicit credentials. The cached ones should be |
| 11623 | // used. |
| 11624 | d.reset(new TestDelegate); |
| 11625 | { |
| 11626 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 11627 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11628 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11629 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11630 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11631 | r->Start(); |
| 11632 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11633 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11634 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11635 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11636 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11637 | EXPECT_EQ(1, d->response_started_count()); |
| 11638 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11639 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11640 | } |
| 11641 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11642 | |
| 11643 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 11644 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11645 | |
| 11646 | TestDelegate d; |
| 11647 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11648 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 11649 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11650 | req->Start(); |
| 11651 | base::RunLoop().Run(); |
| 11652 | |
| 11653 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 11654 | } |
| 11655 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11656 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11657 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11658 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 11659 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11660 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 11661 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11662 | |
| 11663 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11664 | |
| 11665 | req->Start(); |
| 11666 | base::RunLoop().Run(); |
| 11667 | |
| 11668 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 11669 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11670 | } |
| 11671 | |
| 11672 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 11673 | MockHostResolver host_resolver; |
| 11674 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 11675 | TestURLRequestContext context(true); |
| 11676 | context.set_network_delegate(&network_delegate); |
| 11677 | context.set_host_resolver(&host_resolver); |
| 11678 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 11679 | context.Init(); |
| 11680 | |
| 11681 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11682 | std::unique_ptr<URLRequest> req( |
| 11683 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 11684 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11685 | |
| 11686 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11687 | |
| 11688 | req->Start(); |
| 11689 | base::RunLoop().Run(); |
| 11690 | EXPECT_TRUE(req->response_info().network_accessed); |
| 11691 | } |
| 11692 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11693 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11694 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11695 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11696 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11697 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11698 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 11699 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11700 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11701 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11702 | req.get(), &default_network_delegate_, |
| 11703 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11704 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 11705 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11706 | |
| 11707 | req->Start(); |
| 11708 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11709 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11710 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11711 | EXPECT_EQ(0, d.received_redirect_count()); |
| 11712 | } |
| 11713 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11714 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11715 | ASSERT_TRUE(http_test_server()->Start()); |
| 11716 | TestURLRequestContext context; |
| 11717 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 11718 | TestDelegate delegate; |
| 11719 | HttpRequestHeaders extra_headers; |
| 11720 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11721 | |
| 11722 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11723 | HttpRawRequestHeaders raw_req_headers; |
| 11724 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11725 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11726 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11727 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11728 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11729 | r->SetRequestHeadersCallback(base::Bind( |
| 11730 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 11731 | r->SetResponseHeadersCallback(base::Bind( |
| 11732 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11733 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11734 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11735 | r->Start(); |
| 11736 | while (!delegate.response_started_count()) |
| 11737 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11738 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11739 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11740 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11741 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11742 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11743 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11744 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 11745 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 11746 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 11747 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11748 | } |
| 11749 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11750 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11751 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11752 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11753 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11754 | FAIL() << "Callback should not be called unless request is sent"; |
| 11755 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11756 | r->SetResponseHeadersCallback( |
| 11757 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 11758 | FAIL() << "Callback should not be called unless request is sent"; |
| 11759 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11760 | r->Start(); |
| 11761 | base::RunLoop().Run(); |
| 11762 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11763 | } |
| 11764 | } |
| 11765 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11766 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11767 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11768 | HttpRawRequestHeaders raw_req_headers; |
| 11769 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11770 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11771 | TestURLRequestContext context; |
| 11772 | TestDelegate delegate; |
| 11773 | HttpRequestHeaders extra_headers; |
| 11774 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11775 | delegate.set_quit_on_redirect(true); |
| 11776 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
| 11777 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11778 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11779 | r->SetExtraRequestHeaders(extra_headers); |
| 11780 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11781 | base::Unretained(&raw_req_headers))); |
| 11782 | r->SetResponseHeadersCallback(base::Bind( |
| 11783 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11784 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11785 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11786 | r->Start(); |
| 11787 | base::RunLoop().Run(); |
| 11788 | |
| 11789 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 11790 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11791 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11792 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11793 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11794 | EXPECT_EQ("gzip, deflate", value); |
| 11795 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11796 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 11797 | raw_req_headers.request_line()); |
| 11798 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 11799 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 11800 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11801 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11802 | raw_req_headers = HttpRawRequestHeaders(); |
| 11803 | raw_resp_headers = nullptr; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11804 | r->FollowDeferredRedirect(); |
| 11805 | base::RunLoop().Run(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11806 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11807 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11808 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11809 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11810 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 11811 | raw_req_headers.request_line()); |
| 11812 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11813 | } |
| 11814 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11815 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11816 | TestDelegate request_delegate; |
| 11817 | |
| 11818 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11819 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 11820 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11821 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11822 | FAIL() << "Callback should not be called unless request is sent"; |
| 11823 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11824 | r->SetResponseHeadersCallback( |
| 11825 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 11826 | FAIL() << "Callback should not be called unless request is sent"; |
| 11827 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11828 | r->Start(); |
| 11829 | base::RunLoop().Run(); |
| 11830 | EXPECT_FALSE(r->is_pending()); |
| 11831 | } |
| 11832 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11833 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11834 | ASSERT_TRUE(http_test_server()->Start()); |
| 11835 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 11836 | |
| 11837 | TestURLRequestContext context; |
| 11838 | TestDelegate delegate; |
| 11839 | |
| 11840 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 11841 | HttpRequestHeaders extra_headers; |
| 11842 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11843 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11844 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 11845 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11846 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11847 | using RespHeadersVector = |
| 11848 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 11849 | RespHeadersVector raw_resp_headers; |
| 11850 | |
| 11851 | auto req_headers_callback = base::Bind( |
| 11852 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 11853 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 11854 | }, |
| 11855 | &raw_req_headers); |
| 11856 | auto resp_headers_callback = base::Bind( |
| 11857 | [](RespHeadersVector* vec, |
| 11858 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 11859 | vec->push_back(headers); |
| 11860 | }, |
| 11861 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11862 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11863 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11864 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11865 | r->SetRequestHeadersCallback(req_headers_callback); |
| 11866 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11867 | r->Start(); |
| 11868 | base::RunLoop().Run(); |
| 11869 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11870 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 11871 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11872 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11873 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 11874 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11875 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11876 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 11877 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11878 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11879 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 11880 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 11881 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 11882 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 11883 | |
| 11884 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 11885 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11886 | r2->SetExtraRequestHeaders(extra_headers); |
| 11887 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 11888 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 11889 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 11890 | r2->Start(); |
| 11891 | base::RunLoop().Run(); |
| 11892 | EXPECT_FALSE(r2->is_pending()); |
| 11893 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 11894 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 11895 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 11896 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 11897 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 11898 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11899 | } |
| 11900 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11901 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11902 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 11903 | TestDelegate d; |
| 11904 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11905 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11906 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11907 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11908 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11909 | r->SetResponseHeadersCallback( |
| 11910 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 11911 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 11912 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11913 | r->Start(); |
| 11914 | base::RunLoop().Run(); |
| 11915 | EXPECT_FALSE(r->is_pending()); |
| 11916 | } |
| 11917 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11918 | } // namespace net |