[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 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 171 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 172 | // Test file used in most FTP tests. |
| 173 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 174 | #endif |
| 175 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 176 | // Tests load timing information in the case a fresh connection was used, with |
| 177 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 178 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 179 | int connect_timing_flags) { |
| 180 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 181 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 182 | |
| 183 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 184 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 185 | |
| 186 | EXPECT_LE(load_timing_info.request_start, |
| 187 | load_timing_info.connect_timing.connect_start); |
| 188 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 189 | connect_timing_flags); |
| 190 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 191 | load_timing_info.send_start); |
| 192 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 193 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 194 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 195 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 196 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 197 | } |
| 198 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 199 | // Same as above, but with proxy times. |
| 200 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 201 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 202 | int connect_timing_flags) { |
| 203 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 204 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 205 | |
| 206 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 207 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 208 | |
| 209 | EXPECT_LE(load_timing_info.request_start, |
| 210 | load_timing_info.proxy_resolve_start); |
| 211 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 212 | load_timing_info.proxy_resolve_end); |
| 213 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 214 | load_timing_info.connect_timing.connect_start); |
| 215 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 216 | connect_timing_flags); |
| 217 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 218 | load_timing_info.send_start); |
| 219 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 220 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 221 | } |
| 222 | |
| 223 | // Same as above, but with a reused socket and proxy times. |
| 224 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 225 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 226 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 227 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 228 | |
| 229 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 230 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 231 | |
| 232 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 233 | |
| 234 | EXPECT_LE(load_timing_info.request_start, |
| 235 | load_timing_info.proxy_resolve_start); |
| 236 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 237 | load_timing_info.proxy_resolve_end); |
| 238 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 239 | load_timing_info.send_start); |
| 240 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 241 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 242 | } |
| 243 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 244 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 245 | // Tests load timing information in the case of a cache hit, when no cache |
| 246 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 247 | base::StringPiece TestNetResourceProvider(int key) { |
| 248 | return "header"; |
| 249 | } |
| 250 | |
| 251 | void FillBuffer(char* buffer, size_t len) { |
| 252 | static bool called = false; |
| 253 | if (!called) { |
| 254 | called = true; |
| 255 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 256 | srand(seed); |
| 257 | } |
| 258 | |
| 259 | for (size_t i = 0; i < len; i++) { |
| 260 | buffer[i] = static_cast<char>(rand()); |
| 261 | if (!buffer[i]) |
| 262 | buffer[i] = 'g'; |
| 263 | } |
| 264 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 265 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 266 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 267 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 268 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 269 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 270 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 271 | |
| 272 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 273 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 274 | |
| 275 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 276 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 277 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 278 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 279 | |
| 280 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 281 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 282 | } |
| 283 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 284 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 285 | // Tests load timing in the case that there is no HTTP response. This can be |
| 286 | // used to test in the case of errors or non-HTTP requests. |
| 287 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 288 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 289 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 290 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 291 | |
| 292 | // Only the request times should be non-null. |
| 293 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 294 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 295 | |
| 296 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 297 | |
| 298 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 299 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 300 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 301 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 302 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 303 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 304 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 305 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 306 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 307 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 308 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 309 | public: |
| 310 | TestPowerMonitorSource() {} |
| 311 | ~TestPowerMonitorSource() override {} |
| 312 | |
| 313 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 314 | |
| 315 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 316 | |
| 317 | bool IsOnBatteryPowerImpl() override { return false; } |
| 318 | |
| 319 | private: |
| 320 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 321 | }; |
| 322 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 323 | // Job that allows monitoring of its priority. |
| 324 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 325 | public: |
| 326 | // The latest priority of the job is always written to |request_priority_|. |
| 327 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 328 | NetworkDelegate* network_delegate, |
| 329 | RequestPriority* request_priority) |
| 330 | : URLRequestTestJob(request, network_delegate), |
| 331 | request_priority_(request_priority) { |
| 332 | *request_priority_ = DEFAULT_PRIORITY; |
| 333 | } |
| 334 | |
| 335 | void SetPriority(RequestPriority priority) override { |
| 336 | *request_priority_ = priority; |
| 337 | URLRequestTestJob::SetPriority(priority); |
| 338 | } |
| 339 | |
| 340 | private: |
| 341 | RequestPriority* const request_priority_; |
| 342 | }; |
| 343 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 344 | // Do a case-insensitive search through |haystack| for |needle|. |
| 345 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 346 | std::string::const_iterator it = std::search( |
| 347 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 348 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 349 | return it != haystack.end(); |
| 350 | } |
| 351 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 352 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 353 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 354 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 355 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 358 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 359 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 360 | // -1 means unknown. 0 means no encryption. |
| 361 | EXPECT_GT(ssl_info.security_bits, 0); |
| 362 | |
| 363 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 364 | uint16_t cipher_suite = |
| 365 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 366 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 367 | } |
| 368 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 369 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 370 | const GURL& host_url) { |
| 371 | std::string sent_value; |
| 372 | |
| 373 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 374 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 375 | |
| 376 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 377 | EXPECT_EQ("keep-alive", sent_value); |
| 378 | } |
| 379 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 380 | // A network delegate that allows the user to choose a subset of request stages |
| 381 | // to block in. When blocking, the delegate can do one of the following: |
| 382 | // * synchronously return a pre-specified error code, or |
| 383 | // * asynchronously return that value via an automatically called callback, |
| 384 | // or |
| 385 | // * block and wait for the user to do a callback. |
| 386 | // Additionally, the user may also specify a redirect URL -- then each request |
| 387 | // with the current URL different from the redirect target will be redirected |
| 388 | // to that target, in the on-before-URL-request stage, independent of whether |
| 389 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 390 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 391 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 392 | // Stages in which the delegate can block. |
| 393 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 394 | NOT_BLOCKED = 0, |
| 395 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 396 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 397 | ON_HEADERS_RECEIVED = 1 << 2, |
| 398 | ON_AUTH_REQUIRED = 1 << 3 |
| 399 | }; |
| 400 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 401 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 402 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 403 | enum BlockMode { |
| 404 | SYNCHRONOUS, // No callback, returns specified return values. |
| 405 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 406 | // specified return codes. |
| 407 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 408 | // |auth_retval_| are ignored. In every blocking stage the |
| 409 | // message loop is quit. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 410 | USER_NOTIFY, // User is notified by a provided callback of the |
| 411 | // blocking, and synchronously returns instructions |
| 412 | // for handling it. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 413 | }; |
| 414 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 415 | using NotificationCallback = |
| 416 | base::Callback<Error(const CompletionCallback&, const URLRequest*)>; |
| 417 | |
| 418 | using NotificationAuthCallback = |
| 419 | base::Callback<NetworkDelegate::AuthRequiredResponse(const AuthCallback&, |
| 420 | const URLRequest*)>; |
| 421 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 422 | // Creates a delegate which does not block at all. |
| 423 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 424 | |
| 425 | // For users to trigger a callback returning |response|. |
| 426 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 427 | // Only call if |block_mode_| == USER_CALLBACK. |
| 428 | void DoCallback(int response); |
| 429 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 430 | |
| 431 | // Setters. |
| 432 | void set_retval(int retval) { |
| 433 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 434 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 435 | ASSERT_NE(OK, retval); |
| 436 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 437 | } |
| 438 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 439 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 440 | // |auth_credentials_| will be passed with the response. |
| 441 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 442 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 443 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 444 | auth_retval_ = auth_retval; |
| 445 | } |
| 446 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 447 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 450 | void set_redirect_url(const GURL& url) { |
| 451 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 454 | void set_block_on(int block_on) { |
| 455 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 456 | } |
| 457 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 458 | // Only valid if |block_mode_| == USER_NOTIFY |
| 459 | void set_notification_callback( |
| 460 | const NotificationCallback& notification_callback) { |
| 461 | notification_callback_ = notification_callback; |
| 462 | } |
| 463 | |
| 464 | void set_notification_auth_callback( |
| 465 | const NotificationAuthCallback& notification_auth_callback) { |
| 466 | notification_auth_callback_ = notification_auth_callback; |
| 467 | } |
| 468 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 469 | // Allows the user to check in which state did we block. |
| 470 | Stage stage_blocked_for_callback() const { |
| 471 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 472 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 476 | void RunCallback(int response, const CompletionCallback& callback); |
| 477 | void RunAuthCallback(AuthRequiredResponse response, |
| 478 | const AuthCallback& callback); |
| 479 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 480 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 481 | int OnBeforeURLRequest(URLRequest* request, |
| 482 | const CompletionCallback& callback, |
| 483 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 484 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 485 | int OnBeforeStartTransaction(URLRequest* request, |
| 486 | const CompletionCallback& callback, |
| 487 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 488 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 489 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 490 | URLRequest* request, |
| 491 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 492 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 493 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 494 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 495 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 496 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 497 | URLRequest* request, |
| 498 | const AuthChallengeInfo& auth_info, |
| 499 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 500 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 501 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 502 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 503 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 504 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 505 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 506 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 507 | int MaybeBlockStage(Stage stage, |
| 508 | const URLRequest* request, |
| 509 | const CompletionCallback& callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 510 | |
| 511 | // Configuration parameters, can be adjusted by public methods: |
| 512 | const BlockMode block_mode_; |
| 513 | |
| 514 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 515 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 516 | int retval_; // To be returned in non-auth stages. |
| 517 | AuthRequiredResponse auth_retval_; |
| 518 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 519 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 520 | int block_on_; // Bit mask: in which stages to block. |
| 521 | |
| 522 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 523 | // callback. |
| 524 | AuthCredentials auth_credentials_; |
| 525 | AuthCredentials* target_auth_credentials_; |
| 526 | |
| 527 | // Internal variables, not set by not the user: |
| 528 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 529 | // USER_CALLBACK). |
| 530 | Stage stage_blocked_for_callback_; |
| 531 | |
| 532 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 533 | CompletionCallback callback_; |
| 534 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 535 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 536 | // Callback to request user instructions for blocking. |
| 537 | NotificationCallback notification_callback_; |
| 538 | NotificationAuthCallback notification_auth_callback_; |
| 539 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 540 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 541 | |
| 542 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 543 | }; |
| 544 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 545 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 546 | : block_mode_(block_mode), |
| 547 | retval_(OK), |
| 548 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 549 | block_on_(0), |
| 550 | target_auth_credentials_(NULL), |
| 551 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 552 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 556 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 557 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 558 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 559 | CompletionCallback callback = callback_; |
| 560 | Reset(); |
| 561 | RunCallback(response, callback); |
| 562 | } |
| 563 | |
| 564 | void BlockingNetworkDelegate::DoAuthCallback( |
| 565 | NetworkDelegate::AuthRequiredResponse response) { |
| 566 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 567 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 568 | AuthCallback auth_callback = auth_callback_; |
| 569 | Reset(); |
| 570 | RunAuthCallback(response, auth_callback); |
| 571 | } |
| 572 | |
| 573 | void BlockingNetworkDelegate::RunCallback(int response, |
| 574 | const CompletionCallback& callback) { |
| 575 | callback.Run(response); |
| 576 | } |
| 577 | |
| 578 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 579 | const AuthCallback& callback) { |
| 580 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 581 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 582 | *target_auth_credentials_ = auth_credentials_; |
| 583 | } |
| 584 | callback.Run(response); |
| 585 | } |
| 586 | |
| 587 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 588 | URLRequest* request, |
| 589 | const CompletionCallback& callback, |
| 590 | GURL* new_url) { |
| 591 | if (redirect_url_ == request->url()) |
| 592 | return OK; // We've already seen this request and redirected elsewhere. |
| 593 | |
| 594 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 595 | |
| 596 | if (!redirect_url_.is_empty()) |
| 597 | *new_url = redirect_url_; |
| 598 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 599 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 600 | } |
| 601 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 602 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 603 | URLRequest* request, |
| 604 | const CompletionCallback& callback, |
| 605 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 606 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 607 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 608 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 612 | URLRequest* request, |
| 613 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 614 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 615 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 616 | GURL* allowed_unsafe_redirect_url) { |
| 617 | TestNetworkDelegate::OnHeadersReceived(request, |
| 618 | callback, |
| 619 | original_response_headers, |
| 620 | override_response_headers, |
| 621 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 622 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 623 | return MaybeBlockStage(ON_HEADERS_RECEIVED, request, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 627 | URLRequest* request, |
| 628 | const AuthChallengeInfo& auth_info, |
| 629 | const AuthCallback& callback, |
| 630 | AuthCredentials* credentials) { |
| 631 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 632 | credentials); |
| 633 | // Check that the user has provided callback for the previous blocked stage. |
| 634 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 635 | |
| 636 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 637 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 638 | } |
| 639 | |
| 640 | target_auth_credentials_ = credentials; |
| 641 | |
| 642 | switch (block_mode_) { |
| 643 | case SYNCHRONOUS: |
| 644 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 645 | *target_auth_credentials_ = auth_credentials_; |
| 646 | return auth_retval_; |
| 647 | |
| 648 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 649 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | FROM_HERE, |
| 651 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 652 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 653 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 654 | |
| 655 | case USER_CALLBACK: |
| 656 | auth_callback_ = callback; |
| 657 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 658 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 659 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 660 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 661 | |
| 662 | case USER_NOTIFY: |
| 663 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 664 | // responsibility for running the callback in the future. |
| 665 | return notification_auth_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 666 | } |
| 667 | NOTREACHED(); |
| 668 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 669 | } |
| 670 | |
| 671 | void BlockingNetworkDelegate::Reset() { |
| 672 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 673 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 674 | callback_.Reset(); |
| 675 | auth_callback_.Reset(); |
| 676 | } |
| 677 | |
| 678 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 679 | BlockingNetworkDelegate::Stage stage, |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 680 | const URLRequest* request, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 681 | const CompletionCallback& callback) { |
| 682 | // Check that the user has provided callback for the previous blocked stage. |
| 683 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 684 | |
| 685 | if ((block_on_ & stage) == 0) { |
| 686 | return OK; |
| 687 | } |
| 688 | |
| 689 | switch (block_mode_) { |
| 690 | case SYNCHRONOUS: |
| 691 | EXPECT_NE(OK, retval_); |
| 692 | return retval_; |
| 693 | |
| 694 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 695 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 696 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 697 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 698 | return ERR_IO_PENDING; |
| 699 | |
| 700 | case USER_CALLBACK: |
| 701 | callback_ = callback; |
| 702 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 703 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 704 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 705 | return ERR_IO_PENDING; |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 706 | |
| 707 | case USER_NOTIFY: |
| 708 | // If the callback returns ERR_IO_PENDING, the user has accepted |
| 709 | // responsibility for running the callback in the future. |
| 710 | return notification_callback_.Run(callback, request); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 711 | } |
| 712 | NOTREACHED(); |
| 713 | return 0; |
| 714 | } |
| 715 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 716 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 717 | public: |
| 718 | // Does not own |delegate|. |
| 719 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 720 | NetworkDelegate* delegate) |
| 721 | : TestURLRequestContext(true) { |
| 722 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 723 | set_network_delegate(delegate); |
| 724 | Init(); |
| 725 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 726 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 727 | }; |
| 728 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 729 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 730 | // URI and report to be sent. |
| 731 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 732 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 733 | public: |
| 734 | MockCertificateReportSender() {} |
| 735 | ~MockCertificateReportSender() override {} |
| 736 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 737 | void Send(const GURL& report_uri, |
| 738 | base::StringPiece content_type, |
| 739 | base::StringPiece report, |
| 740 | const base::Callback<void()>& success_callback, |
| 741 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 742 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 743 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 744 | report.CopyToString(&latest_report_); |
| 745 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 746 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 747 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 748 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 749 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 750 | |
| 751 | private: |
| 752 | GURL latest_report_uri_; |
| 753 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 754 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 755 | }; |
| 756 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 757 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 758 | public: |
| 759 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 760 | }; |
| 761 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 762 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 763 | // This is needed because after the certificate failure, the URLRequest will |
| 764 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 765 | // The partial SSLInfo does not have the OCSP information filled out. |
| 766 | class OCSPErrorTestDelegate : public TestDelegate { |
| 767 | public: |
| 768 | void OnSSLCertificateError(URLRequest* request, |
| 769 | const SSLInfo& ssl_info, |
| 770 | bool fatal) override { |
| 771 | ssl_info_ = ssl_info; |
| 772 | on_ssl_certificate_error_called_ = true; |
| 773 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 774 | } |
| 775 | |
| 776 | bool on_ssl_certificate_error_called() { |
| 777 | return on_ssl_certificate_error_called_; |
| 778 | } |
| 779 | |
| 780 | SSLInfo ssl_info() { return ssl_info_; } |
| 781 | |
| 782 | private: |
| 783 | bool on_ssl_certificate_error_called_ = false; |
| 784 | SSLInfo ssl_info_; |
| 785 | }; |
| 786 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 787 | } // namespace |
| 788 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 789 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 790 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 791 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 792 | URLRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 793 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 794 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 795 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 796 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 797 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 798 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 799 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 800 | // URLRequestJobs may post clean-up tasks on destruction. |
| 801 | base::RunLoop().RunUntilIdle(); |
| 802 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 803 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 804 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 805 | SetUpFactory(); |
| 806 | default_context_.set_job_factory(job_factory_.get()); |
| 807 | default_context_.Init(); |
| 808 | PlatformTest::SetUp(); |
| 809 | } |
| 810 | |
| 811 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 812 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 813 | "data", base::WrapUnique(new DataProtocolHandler)); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 814 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 815 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame^] | 816 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 817 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 818 | #endif |
| 819 | } |
| 820 | |
| 821 | TestNetworkDelegate* default_network_delegate() { |
| 822 | return &default_network_delegate_; |
| 823 | } |
| 824 | |
| 825 | const TestURLRequestContext& default_context() const { |
| 826 | return default_context_; |
| 827 | } |
| 828 | |
| 829 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 830 | // Adds the TestJobInterceptor to the default context. |
| 831 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 832 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 833 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 834 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 835 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 836 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 837 | } |
| 838 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 839 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 840 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 841 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 842 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 843 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 844 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 845 | }; |
| 846 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 847 | // This NetworkDelegate is picky about what files are accessible. Only |
| 848 | // whitelisted files are allowed. |
| 849 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 850 | public: |
| 851 | CookieBlockingNetworkDelegate(){}; |
| 852 | |
| 853 | // Adds |directory| to the access white list. |
| 854 | void AddToWhitelist(const base::FilePath& directory) { |
| 855 | whitelist_.insert(directory); |
| 856 | } |
| 857 | |
| 858 | private: |
| 859 | // Returns true if |path| matches the white list. |
| 860 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 861 | for (const auto& directory : whitelist_) { |
| 862 | if (directory == path || directory.IsParent(path)) |
| 863 | return true; |
| 864 | } |
| 865 | return false; |
| 866 | } |
| 867 | |
| 868 | // Returns true only if both |original_path| and |absolute_path| match the |
| 869 | // white list. |
| 870 | bool OnCanAccessFile(const URLRequest& request, |
| 871 | const base::FilePath& original_path, |
| 872 | const base::FilePath& absolute_path) const override { |
| 873 | return (OnCanAccessFileInternal(original_path) && |
| 874 | OnCanAccessFileInternal(absolute_path)); |
| 875 | } |
| 876 | |
| 877 | std::set<base::FilePath> whitelist_; |
| 878 | |
| 879 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 880 | }; |
| 881 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 882 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 883 | TestDelegate d; |
| 884 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 885 | std::unique_ptr<URLRequest> r( |
| 886 | default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 887 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 888 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 889 | r->Start(); |
| 890 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 891 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 892 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 893 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 894 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 895 | EXPECT_FALSE(d.received_data_before_response()); |
| 896 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 897 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 898 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 899 | |
| 900 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 901 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
| 905 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 906 | TestDelegate d; |
| 907 | { |
| 908 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 909 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 910 | GURL("data:image/png;base64," |
| 911 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 912 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 913 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 914 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 915 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 916 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 917 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 918 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 919 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 920 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 921 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 922 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 923 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 924 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 925 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 926 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 927 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 928 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 929 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 930 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 931 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 932 | r->Start(); |
| 933 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 934 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 935 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 936 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 937 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 938 | EXPECT_FALSE(d.received_data_before_response()); |
| 939 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 940 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 941 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 942 | |
| 943 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 944 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 948 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 949 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 950 | base::FilePath app_path; |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 951 | |
| 952 | #if defined(OS_ANDROID) |
| 953 | // Android devices are not guaranteed to be able to read /proc/self/exe |
| 954 | // Use /etc/hosts instead |
| 955 | app_path = base::FilePath("/etc/hosts"); |
| 956 | #else |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | PathService::Get(base::FILE_EXE, &app_path); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 958 | #endif // OS_ANDROID |
| 959 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 960 | GURL app_url = FilePathToFileURL(app_path); |
| 961 | |
| 962 | TestDelegate d; |
| 963 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 964 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 965 | app_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 966 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 967 | r->Start(); |
| 968 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 969 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 970 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 971 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 972 | int64_t file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 973 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 974 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 975 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 976 | EXPECT_EQ(1, d.response_started_count()); |
| 977 | EXPECT_FALSE(d.received_data_before_response()); |
| 978 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 979 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 980 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 981 | |
| 982 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 983 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 987 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 988 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 989 | PathService::Get(base::FILE_EXE, &app_path); |
| 990 | GURL app_url = FilePathToFileURL(app_path); |
| 991 | |
| 992 | TestDelegate d; |
| 993 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 994 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 995 | app_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 996 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 997 | r->Start(); |
| 998 | EXPECT_TRUE(r->is_pending()); |
| 999 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1000 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1001 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1002 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1003 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1004 | } |
| 1005 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1006 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1007 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1008 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1009 | FillBuffer(buffer.get(), buffer_size); |
| 1010 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1011 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1012 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1013 | GURL temp_url = FilePathToFileURL(temp_path); |
lazyboy | 8df84fc | 2017-04-12 02:12:48 | [diff] [blame] | 1014 | EXPECT_EQ(static_cast<int>(buffer_size), |
| 1015 | base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1016 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 1017 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 1018 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1019 | |
| 1020 | const size_t first_byte_position = 500; |
| 1021 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1022 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1023 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1024 | buffer.get() + last_byte_position + 1); |
| 1025 | |
| 1026 | TestDelegate d; |
| 1027 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1028 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1029 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1030 | |
| 1031 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1032 | headers.SetHeader( |
| 1033 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1034 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1035 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1036 | r->SetExtraRequestHeaders(headers); |
| 1037 | r->Start(); |
| 1038 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1039 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1040 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1041 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1042 | EXPECT_EQ(1, d.response_started_count()); |
| 1043 | EXPECT_FALSE(d.received_data_before_response()); |
| 1044 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1045 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1046 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1047 | } |
| 1048 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 1049 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1053 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1054 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1055 | FillBuffer(buffer.get(), buffer_size); |
| 1056 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1057 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1058 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1059 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1060 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1061 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 1062 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 1063 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1064 | |
| 1065 | const size_t first_byte_position = 500; |
| 1066 | const size_t last_byte_position = buffer_size - 1; |
| 1067 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1068 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1069 | buffer.get() + last_byte_position + 1); |
| 1070 | |
| 1071 | TestDelegate d; |
| 1072 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1073 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1074 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1075 | |
| 1076 | HttpRequestHeaders headers; |
| 1077 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1078 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1079 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1080 | r->SetExtraRequestHeaders(headers); |
| 1081 | r->Start(); |
| 1082 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1083 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1084 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1085 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1086 | EXPECT_EQ(1, d.response_started_count()); |
| 1087 | EXPECT_FALSE(d.received_data_before_response()); |
| 1088 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1089 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1090 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1091 | } |
| 1092 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 1093 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1097 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1098 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1099 | FillBuffer(buffer.get(), buffer_size); |
| 1100 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1101 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1102 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1103 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1104 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1105 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 1106 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 1107 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[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 | } |
| 1123 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 1124 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1125 | } |
| 1126 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1127 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 1128 | base::ScopedTempDir temp_dir; |
| 1129 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1130 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 1131 | // now, Mac and Android bots return a path with a symbolic link. |
| 1132 | base::FilePath absolute_temp_dir = |
| 1133 | base::MakeAbsoluteFilePath(temp_dir.GetPath()); |
| 1134 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1135 | base::FilePath test_file; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1136 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, &test_file)); |
| 1137 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1138 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1139 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1140 | // Hence the test should use the directory name of |test_file|, rather than |
| 1141 | // |absolute_temp_dir|, for whitelisting. |
| 1142 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1143 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1144 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1145 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1146 | { |
| 1147 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1148 | CookieBlockingNetworkDelegate network_delegate; |
| 1149 | network_delegate.AddToWhitelist(real_temp_dir); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1150 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1151 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1152 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1153 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1154 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1155 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1156 | EXPECT_FALSE(d.request_failed()); |
| 1157 | EXPECT_EQ(test_data, d.data_received()); |
| 1158 | } |
| 1159 | |
| 1160 | { |
| 1161 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1162 | CookieBlockingNetworkDelegate network_delegate; |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1163 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1164 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1165 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1166 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1167 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1168 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1169 | EXPECT_TRUE(d.request_failed()); |
| 1170 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1171 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1175 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1176 | |
| 1177 | TEST_F(URLRequestTest, SymlinksToFiles) { |
| 1178 | base::ScopedTempDir temp_dir; |
| 1179 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1180 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1181 | base::FilePath absolute_temp_dir = |
| 1182 | base::MakeAbsoluteFilePath(temp_dir.GetPath()); |
| 1183 | |
| 1184 | // Create a good directory (will be whitelisted) and a good file. |
| 1185 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1186 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1187 | base::FilePath good_file; |
| 1188 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1189 | std::string good_data("good"); |
| 1190 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1191 | // See the comment in AllowFileURLs() for why this is done. |
| 1192 | base::FilePath real_good_dir = good_file.DirName(); |
| 1193 | |
| 1194 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1195 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1196 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1197 | base::FilePath bad_file; |
| 1198 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1199 | std::string bad_data("bad"); |
| 1200 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1201 | |
| 1202 | // This symlink will point to the good file. Access to the symlink will be |
| 1203 | // allowed as both the symlink and the destination file are in the same |
| 1204 | // good directory. |
| 1205 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1206 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1207 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1208 | // This symlink will point to the bad file. Even though the symlink is in |
| 1209 | // the good directory, access to the symlink will be rejected since it |
| 1210 | // points to the bad file. |
| 1211 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1212 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1213 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1214 | |
| 1215 | CookieBlockingNetworkDelegate network_delegate; |
| 1216 | network_delegate.AddToWhitelist(real_good_dir); |
| 1217 | { |
| 1218 | TestDelegate d; |
| 1219 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1220 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1221 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1222 | r->Start(); |
| 1223 | base::RunLoop().Run(); |
| 1224 | // good_file_url should be allowed. |
| 1225 | EXPECT_FALSE(d.request_failed()); |
| 1226 | EXPECT_EQ(good_data, d.data_received()); |
| 1227 | } |
| 1228 | |
| 1229 | { |
| 1230 | TestDelegate d; |
| 1231 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1232 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1233 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1234 | r->Start(); |
| 1235 | base::RunLoop().Run(); |
| 1236 | // bad_file_url should be rejected. |
| 1237 | EXPECT_TRUE(d.request_failed()); |
| 1238 | EXPECT_EQ("", d.data_received()); |
| 1239 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | TEST_F(URLRequestTest, SymlinksToDirs) { |
| 1244 | base::ScopedTempDir temp_dir; |
| 1245 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1246 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1247 | base::FilePath absolute_temp_dir = |
| 1248 | base::MakeAbsoluteFilePath(temp_dir.GetPath()); |
| 1249 | |
| 1250 | // Create a good directory (will be whitelisted). |
| 1251 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1252 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1253 | |
| 1254 | // Create a bad directory (will not be whitelisted). |
| 1255 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1256 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1257 | |
| 1258 | // This symlink will point to the good directory. Access to the symlink |
| 1259 | // will be allowed as the symlink is in the good dir that'll be white |
| 1260 | // listed. |
| 1261 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1262 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1263 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1264 | // This symlink will point to the bad directory. Even though the symlink is |
| 1265 | // in the good directory, access to the symlink will be rejected since it |
| 1266 | // points to the bad directory. |
| 1267 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1268 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1269 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1270 | |
| 1271 | CookieBlockingNetworkDelegate network_delegate; |
| 1272 | network_delegate.AddToWhitelist(good_dir); |
| 1273 | { |
| 1274 | TestDelegate d; |
| 1275 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1276 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1277 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1278 | r->Start(); |
| 1279 | base::RunLoop().Run(); |
| 1280 | // good_file_url should be allowed. |
| 1281 | EXPECT_FALSE(d.request_failed()); |
| 1282 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1283 | } |
| 1284 | |
| 1285 | { |
| 1286 | TestDelegate d; |
| 1287 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1288 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1289 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1290 | r->Start(); |
| 1291 | base::RunLoop().Run(); |
| 1292 | // bad_file_url should be rejected. |
| 1293 | EXPECT_TRUE(d.request_failed()); |
| 1294 | EXPECT_EQ("", d.data_received()); |
| 1295 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1296 | } |
| 1297 | } |
| 1298 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1299 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1300 | |
| 1301 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1302 | // Put in mock resource provider. |
| 1303 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1304 | |
| 1305 | TestDelegate d; |
| 1306 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1307 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1308 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1309 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1310 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1311 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1312 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1313 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1314 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1315 | req->Start(); |
| 1316 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1317 | |
| 1318 | d.set_cancel_in_received_data_pending(true); |
| 1319 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1320 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | // Take out mock resource provider. |
| 1324 | NetModule::SetResourceProvider(NULL); |
| 1325 | } |
| 1326 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1327 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1328 | // Verify the general sanity of the the output of the file: |
| 1329 | // directory lister by checking for the output of a known existing |
| 1330 | // file. |
| 1331 | const char sentinel_name[] = "filedir-sentinel"; |
| 1332 | |
| 1333 | base::FilePath path; |
| 1334 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1335 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1336 | |
| 1337 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1338 | std::unique_ptr<URLRequest> req( |
| 1339 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1340 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1341 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1342 | base::RunLoop().Run(); |
| 1343 | |
| 1344 | // Generate entry for the sentinel file. |
| 1345 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1346 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1347 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1348 | EXPECT_GT(info.size, 0); |
| 1349 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1350 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1351 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1352 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1353 | info.last_modified); |
| 1354 | |
| 1355 | ASSERT_LT(0, d.bytes_received()); |
| 1356 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1357 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1358 | // Check for the entry generated for the "sentinel" file. |
| 1359 | const std::string& data = d.data_received(); |
| 1360 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1361 | } |
| 1362 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1363 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1364 | // There is an implicit redirect when loading a file path that matches a |
| 1365 | // directory and does not end with a slash. Ensure that following such |
| 1366 | // redirects does not crash. See http://crbug.com/18686. |
| 1367 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1368 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1369 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1370 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1371 | |
| 1372 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1373 | std::unique_ptr<URLRequest> req( |
| 1374 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1375 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1376 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1377 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1378 | |
| 1379 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1380 | ASSERT_LT(0, d.bytes_received()); |
| 1381 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1382 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | #if defined(OS_WIN) |
| 1386 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1387 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1388 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1389 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1390 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1391 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1392 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1393 | |
| 1394 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1395 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1396 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1397 | #endif // defined(OS_WIN) |
| 1398 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1399 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1400 | |
| 1401 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1402 | TestDelegate d; |
| 1403 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1404 | std::unique_ptr<URLRequest> r( |
| 1405 | default_context_.CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1406 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1408 | r->Start(); |
| 1409 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1410 | |
| 1411 | base::RunLoop().Run(); |
| 1412 | EXPECT_TRUE(d.request_failed()); |
| 1413 | } |
| 1414 | } |
| 1415 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1416 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1417 | TestURLRequestContext context; |
| 1418 | TestNetworkDelegate network_delegate; |
| 1419 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1420 | context.set_network_delegate(&network_delegate); |
| 1421 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1422 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1423 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1424 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1425 | req->SetReferrer("https://somewhere.com/"); |
| 1426 | |
| 1427 | req->Start(); |
| 1428 | base::RunLoop().Run(); |
| 1429 | EXPECT_TRUE(d.request_failed()); |
| 1430 | } |
| 1431 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1432 | #if defined(OS_WIN) |
| 1433 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1434 | base::FilePath app_path; |
| 1435 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1436 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1437 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1438 | |
| 1439 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1440 | |
| 1441 | base::win::ScopedCOMInitializer com_initializer; |
| 1442 | |
| 1443 | // Temporarily create a shortcut for test |
| 1444 | { |
| 1445 | base::win::ScopedComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1446 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1447 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1448 | base::win::ScopedComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1449 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1451 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1452 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1453 | } |
| 1454 | |
| 1455 | TestDelegate d; |
| 1456 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1457 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1458 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1459 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1460 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1461 | r->Start(); |
| 1462 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1463 | |
| 1464 | base::RunLoop().Run(); |
| 1465 | |
| 1466 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1467 | GetFileAttributesEx(app_path.value().c_str(), |
| 1468 | GetFileExInfoStandard, &data); |
| 1469 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1470 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1471 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1472 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1473 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1474 | DWORD read_size; |
| 1475 | BOOL result; |
| 1476 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1477 | &read_size, NULL); |
| 1478 | std::string content(buffer.get(), read_size); |
| 1479 | CloseHandle(file); |
| 1480 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1481 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1482 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1483 | EXPECT_EQ(content, d.data_received()); |
| 1484 | } |
| 1485 | |
| 1486 | // Clean the shortcut |
| 1487 | DeleteFile(lnk_path.c_str()); |
| 1488 | } |
| 1489 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1490 | |
| 1491 | // Custom URLRequestJobs for use with interceptor tests |
| 1492 | class RestartTestJob : public URLRequestTestJob { |
| 1493 | public: |
| 1494 | RestartTestJob(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 { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1498 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1499 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1500 | }; |
| 1501 | |
| 1502 | class CancelTestJob : public URLRequestTestJob { |
| 1503 | public: |
| 1504 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1505 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1506 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1507 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1508 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1509 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1510 | }; |
| 1511 | |
| 1512 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1513 | public: |
| 1514 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1515 | NetworkDelegate* network_delegate) |
| 1516 | : URLRequestTestJob(request, network_delegate, true) { |
| 1517 | } |
| 1518 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1519 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1520 | request_->Cancel(); |
| 1521 | this->NotifyRestartRequired(); |
| 1522 | } |
| 1523 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1524 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1525 | }; |
| 1526 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1527 | // An Interceptor for use with interceptor tests. |
| 1528 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1529 | public: |
| 1530 | // Static getters for canned response header and data strings. |
| 1531 | static std::string ok_data() { |
| 1532 | return URLRequestTestJob::test_data_1(); |
| 1533 | } |
| 1534 | |
| 1535 | static std::string ok_headers() { |
| 1536 | return URLRequestTestJob::test_headers(); |
| 1537 | } |
| 1538 | |
| 1539 | static std::string redirect_data() { |
| 1540 | return std::string(); |
| 1541 | } |
| 1542 | |
| 1543 | static std::string redirect_headers() { |
| 1544 | return URLRequestTestJob::test_redirect_headers(); |
| 1545 | } |
| 1546 | |
| 1547 | static std::string error_data() { |
| 1548 | return std::string("ohhh nooooo mr. bill!"); |
| 1549 | } |
| 1550 | |
| 1551 | static std::string error_headers() { |
| 1552 | return URLRequestTestJob::test_error_headers(); |
| 1553 | } |
| 1554 | |
| 1555 | MockURLRequestInterceptor() |
| 1556 | : intercept_main_request_(false), restart_main_request_(false), |
| 1557 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1558 | simulate_main_network_error_(false), |
| 1559 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1560 | intercept_final_response_(false), cancel_final_request_(false), |
| 1561 | use_url_request_http_job_(false), |
| 1562 | did_intercept_main_(false), did_restart_main_(false), |
| 1563 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1564 | did_simulate_error_main_(false), |
| 1565 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1566 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1567 | } |
| 1568 | |
| 1569 | ~MockURLRequestInterceptor() override { |
| 1570 | } |
| 1571 | |
| 1572 | // URLRequestInterceptor implementation: |
| 1573 | URLRequestJob* MaybeInterceptRequest( |
| 1574 | URLRequest* request, |
| 1575 | NetworkDelegate* network_delegate) const override { |
| 1576 | if (restart_main_request_) { |
| 1577 | restart_main_request_ = false; |
| 1578 | did_restart_main_ = true; |
| 1579 | return new RestartTestJob(request, network_delegate); |
| 1580 | } |
| 1581 | if (cancel_main_request_) { |
| 1582 | cancel_main_request_ = false; |
| 1583 | did_cancel_main_ = true; |
| 1584 | return new CancelTestJob(request, network_delegate); |
| 1585 | } |
| 1586 | if (cancel_then_restart_main_request_) { |
| 1587 | cancel_then_restart_main_request_ = false; |
| 1588 | did_cancel_then_restart_main_ = true; |
| 1589 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1590 | } |
| 1591 | if (simulate_main_network_error_) { |
| 1592 | simulate_main_network_error_ = false; |
| 1593 | did_simulate_error_main_ = true; |
| 1594 | if (use_url_request_http_job_) { |
| 1595 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1596 | } |
| 1597 | // This job will result in error since the requested URL is not one of the |
| 1598 | // URLs supported by these tests. |
| 1599 | return new URLRequestTestJob(request, network_delegate, true); |
| 1600 | } |
| 1601 | if (!intercept_main_request_) |
| 1602 | return nullptr; |
| 1603 | intercept_main_request_ = false; |
| 1604 | did_intercept_main_ = true; |
| 1605 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1606 | network_delegate, |
| 1607 | main_headers_, |
| 1608 | main_data_, |
| 1609 | true); |
| 1610 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1611 | return job; |
| 1612 | } |
| 1613 | |
| 1614 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1615 | NetworkDelegate* network_delegate, |
| 1616 | const GURL& location) const override { |
| 1617 | if (cancel_redirect_request_) { |
| 1618 | cancel_redirect_request_ = false; |
| 1619 | did_cancel_redirect_ = true; |
| 1620 | return new CancelTestJob(request, network_delegate); |
| 1621 | } |
| 1622 | if (!intercept_redirect_) |
| 1623 | return nullptr; |
| 1624 | intercept_redirect_ = false; |
| 1625 | did_intercept_redirect_ = true; |
| 1626 | if (use_url_request_http_job_) { |
| 1627 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1628 | } |
| 1629 | return new URLRequestTestJob(request, |
| 1630 | network_delegate, |
| 1631 | redirect_headers_, |
| 1632 | redirect_data_, |
| 1633 | true); |
| 1634 | } |
| 1635 | |
| 1636 | URLRequestJob* MaybeInterceptResponse( |
| 1637 | URLRequest* request, |
| 1638 | NetworkDelegate* network_delegate) const override { |
| 1639 | if (cancel_final_request_) { |
| 1640 | cancel_final_request_ = false; |
| 1641 | did_cancel_final_ = true; |
| 1642 | return new CancelTestJob(request, network_delegate); |
| 1643 | } |
| 1644 | if (!intercept_final_response_) |
| 1645 | return nullptr; |
| 1646 | intercept_final_response_ = false; |
| 1647 | did_intercept_final_ = true; |
| 1648 | if (use_url_request_http_job_) { |
| 1649 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1650 | } |
| 1651 | return new URLRequestTestJob(request, |
| 1652 | network_delegate, |
| 1653 | final_headers_, |
| 1654 | final_data_, |
| 1655 | true); |
| 1656 | } |
| 1657 | |
| 1658 | void set_intercept_main_request(bool intercept_main_request) { |
| 1659 | intercept_main_request_ = intercept_main_request; |
| 1660 | } |
| 1661 | |
| 1662 | void set_main_headers(const std::string& main_headers) { |
| 1663 | main_headers_ = main_headers; |
| 1664 | } |
| 1665 | |
| 1666 | void set_main_data(const std::string& main_data) { |
| 1667 | main_data_ = main_data; |
| 1668 | } |
| 1669 | |
| 1670 | void set_main_request_load_timing_info( |
| 1671 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1672 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1673 | } |
| 1674 | |
| 1675 | void set_restart_main_request(bool restart_main_request) { |
| 1676 | restart_main_request_ = restart_main_request; |
| 1677 | } |
| 1678 | |
| 1679 | void set_cancel_main_request(bool cancel_main_request) { |
| 1680 | cancel_main_request_ = cancel_main_request; |
| 1681 | } |
| 1682 | |
| 1683 | void set_cancel_then_restart_main_request( |
| 1684 | bool cancel_then_restart_main_request) { |
| 1685 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1686 | } |
| 1687 | |
| 1688 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1689 | simulate_main_network_error_ = simulate_main_network_error; |
| 1690 | } |
| 1691 | |
| 1692 | void set_intercept_redirect(bool intercept_redirect) { |
| 1693 | intercept_redirect_ = intercept_redirect; |
| 1694 | } |
| 1695 | |
| 1696 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1697 | redirect_headers_ = redirect_headers; |
| 1698 | } |
| 1699 | |
| 1700 | void set_redirect_data(const std::string& redirect_data) { |
| 1701 | redirect_data_ = redirect_data; |
| 1702 | } |
| 1703 | |
| 1704 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1705 | cancel_redirect_request_ = cancel_redirect_request; |
| 1706 | } |
| 1707 | |
| 1708 | void set_intercept_final_response(bool intercept_final_response) { |
| 1709 | intercept_final_response_ = intercept_final_response; |
| 1710 | } |
| 1711 | |
| 1712 | void set_final_headers(const std::string& final_headers) { |
| 1713 | final_headers_ = final_headers; |
| 1714 | } |
| 1715 | |
| 1716 | void set_final_data(const std::string& final_data) { |
| 1717 | final_data_ = final_data; |
| 1718 | } |
| 1719 | |
| 1720 | void set_cancel_final_request(bool cancel_final_request) { |
| 1721 | cancel_final_request_ = cancel_final_request; |
| 1722 | } |
| 1723 | |
| 1724 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1725 | use_url_request_http_job_ = use_url_request_http_job; |
| 1726 | } |
| 1727 | |
| 1728 | bool did_intercept_main() const { |
| 1729 | return did_intercept_main_; |
| 1730 | } |
| 1731 | |
| 1732 | bool did_restart_main() const { |
| 1733 | return did_restart_main_; |
| 1734 | } |
| 1735 | |
| 1736 | bool did_cancel_main() const { |
| 1737 | return did_cancel_main_; |
| 1738 | } |
| 1739 | |
| 1740 | bool did_cancel_then_restart_main() const { |
| 1741 | return did_cancel_then_restart_main_; |
| 1742 | } |
| 1743 | |
| 1744 | bool did_simulate_error_main() const { |
| 1745 | return did_simulate_error_main_; |
| 1746 | } |
| 1747 | |
| 1748 | bool did_intercept_redirect() const { |
| 1749 | return did_intercept_redirect_; |
| 1750 | } |
| 1751 | |
| 1752 | bool did_cancel_redirect() const { |
| 1753 | return did_cancel_redirect_; |
| 1754 | } |
| 1755 | |
| 1756 | bool did_intercept_final() const { |
| 1757 | return did_intercept_final_; |
| 1758 | } |
| 1759 | |
| 1760 | bool did_cancel_final() const { |
| 1761 | return did_cancel_final_; |
| 1762 | } |
| 1763 | |
| 1764 | private: |
| 1765 | // Indicate whether to intercept the main request, and if so specify the |
| 1766 | // response to return and the LoadTimingInfo to use. |
| 1767 | mutable bool intercept_main_request_; |
| 1768 | mutable std::string main_headers_; |
| 1769 | mutable std::string main_data_; |
| 1770 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1771 | |
| 1772 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1773 | mutable bool restart_main_request_; |
| 1774 | mutable bool cancel_main_request_; |
| 1775 | mutable bool cancel_then_restart_main_request_; |
| 1776 | mutable bool simulate_main_network_error_; |
| 1777 | |
| 1778 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1779 | // return. |
| 1780 | mutable bool intercept_redirect_; |
| 1781 | mutable std::string redirect_headers_; |
| 1782 | mutable std::string redirect_data_; |
| 1783 | |
| 1784 | // Cancel the request within MaybeInterceptRedirect. |
| 1785 | mutable bool cancel_redirect_request_; |
| 1786 | |
| 1787 | // Indicate whether to intercept the final response, and if so specify the |
| 1788 | // response to return. |
| 1789 | mutable bool intercept_final_response_; |
| 1790 | mutable std::string final_headers_; |
| 1791 | mutable std::string final_data_; |
| 1792 | |
| 1793 | // Cancel the final request within MaybeInterceptResponse. |
| 1794 | mutable bool cancel_final_request_; |
| 1795 | |
| 1796 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1797 | mutable bool use_url_request_http_job_; |
| 1798 | |
| 1799 | // These indicate if the interceptor did something or not. |
| 1800 | mutable bool did_intercept_main_; |
| 1801 | mutable bool did_restart_main_; |
| 1802 | mutable bool did_cancel_main_; |
| 1803 | mutable bool did_cancel_then_restart_main_; |
| 1804 | mutable bool did_simulate_error_main_; |
| 1805 | mutable bool did_intercept_redirect_; |
| 1806 | mutable bool did_cancel_redirect_; |
| 1807 | mutable bool did_intercept_final_; |
| 1808 | mutable bool did_cancel_final_; |
| 1809 | }; |
| 1810 | |
| 1811 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1812 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1813 | public: |
| 1814 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1815 | } |
| 1816 | |
| 1817 | ~URLRequestInterceptorTest() override { |
| 1818 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1819 | base::RunLoop().RunUntilIdle(); |
| 1820 | } |
| 1821 | |
| 1822 | void SetUpFactory() override { |
| 1823 | interceptor_ = new MockURLRequestInterceptor(); |
| 1824 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1825 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | MockURLRequestInterceptor* interceptor() const { |
| 1829 | return interceptor_; |
| 1830 | } |
| 1831 | |
| 1832 | private: |
| 1833 | MockURLRequestInterceptor* interceptor_; |
| 1834 | }; |
| 1835 | |
| 1836 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1837 | // Intercept the main request and respond with a simple response. |
| 1838 | interceptor()->set_intercept_main_request(true); |
| 1839 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1840 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1841 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1842 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1843 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1844 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1845 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1846 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1847 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1848 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1849 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1850 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1851 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1852 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1853 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1854 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1855 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1856 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1857 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1858 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1859 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1860 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1861 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1862 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1863 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1864 | EXPECT_EQ(1, d.response_started_count()); |
| 1865 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1866 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1867 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1868 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1869 | // Intercept the main request and respond with a redirect. |
| 1870 | interceptor()->set_intercept_main_request(true); |
| 1871 | interceptor()->set_main_headers( |
| 1872 | MockURLRequestInterceptor::redirect_headers()); |
| 1873 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1874 | |
| 1875 | // Intercept that redirect and respond with a final OK response. |
| 1876 | interceptor()->set_intercept_redirect(true); |
| 1877 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1878 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1879 | |
| 1880 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1881 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1882 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1883 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1884 | req->set_method("GET"); |
| 1885 | req->Start(); |
| 1886 | base::RunLoop().Run(); |
| 1887 | |
| 1888 | // Check that the interceptor got called as expected. |
| 1889 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1890 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1891 | |
| 1892 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1893 | int status = d.request_status(); |
| 1894 | EXPECT_EQ(OK, status); |
| 1895 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1896 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1897 | |
| 1898 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1899 | EXPECT_EQ(1, d.response_started_count()); |
| 1900 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1901 | } |
| 1902 | |
| 1903 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1904 | // Intercept the main request to generate a server error response. |
| 1905 | interceptor()->set_intercept_main_request(true); |
| 1906 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1907 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1908 | |
| 1909 | // Intercept that error and respond with an OK response. |
| 1910 | interceptor()->set_intercept_final_response(true); |
| 1911 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1912 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1913 | |
| 1914 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1915 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1916 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1917 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1918 | req->set_method("GET"); |
| 1919 | req->Start(); |
| 1920 | base::RunLoop().Run(); |
| 1921 | |
| 1922 | // Check that the interceptor got called as expected. |
| 1923 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1924 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1925 | |
| 1926 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1927 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1928 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1929 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1930 | EXPECT_EQ(1, d.response_started_count()); |
| 1931 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1932 | } |
| 1933 | |
| 1934 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1935 | // Intercept the main request to simulate a network error. |
| 1936 | interceptor()->set_simulate_main_network_error(true); |
| 1937 | |
| 1938 | // Intercept that error and respond with an OK response. |
| 1939 | interceptor()->set_intercept_final_response(true); |
| 1940 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1941 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1942 | |
| 1943 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1944 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1945 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1946 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1947 | req->set_method("GET"); |
| 1948 | req->Start(); |
| 1949 | base::RunLoop().Run(); |
| 1950 | |
| 1951 | // Check that the interceptor got called as expected. |
| 1952 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1953 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1954 | |
| 1955 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1956 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1957 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1958 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1959 | EXPECT_EQ(1, d.response_started_count()); |
| 1960 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1961 | } |
| 1962 | |
| 1963 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1964 | // Restart the main request. |
| 1965 | interceptor()->set_restart_main_request(true); |
| 1966 | |
| 1967 | // then intercept the new main request and respond with an OK response |
| 1968 | interceptor()->set_intercept_main_request(true); |
| 1969 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1970 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1971 | |
| 1972 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1973 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1974 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1975 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1976 | req->set_method("GET"); |
| 1977 | req->Start(); |
| 1978 | base::RunLoop().Run(); |
| 1979 | |
| 1980 | // Check that the interceptor got called as expected. |
| 1981 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1982 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1983 | |
| 1984 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1985 | int status = d.request_status(); |
| 1986 | EXPECT_EQ(OK, status); |
| 1987 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1988 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1989 | |
| 1990 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1991 | EXPECT_EQ(1, d.response_started_count()); |
| 1992 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1993 | } |
| 1994 | |
| 1995 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1996 | // Intercept the main request and cancel from within the restarted job. |
| 1997 | interceptor()->set_cancel_main_request(true); |
| 1998 | |
| 1999 | // Set up to intercept the final response and override it with an OK response. |
| 2000 | interceptor()->set_intercept_final_response(true); |
| 2001 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2002 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2003 | |
| 2004 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2005 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2006 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2007 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2008 | req->set_method("GET"); |
| 2009 | req->Start(); |
| 2010 | base::RunLoop().Run(); |
| 2011 | |
| 2012 | // Check that the interceptor got called as expected. |
| 2013 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2014 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2015 | |
| 2016 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2017 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2021 | // Intercept the main request and respond with a redirect. |
| 2022 | interceptor()->set_intercept_main_request(true); |
| 2023 | interceptor()->set_main_headers( |
| 2024 | MockURLRequestInterceptor::redirect_headers()); |
| 2025 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2026 | |
| 2027 | // Intercept the redirect and cancel from within that job. |
| 2028 | interceptor()->set_cancel_redirect_request(true); |
| 2029 | |
| 2030 | // Set up to intercept the final response and override it with an OK response. |
| 2031 | interceptor()->set_intercept_final_response(true); |
| 2032 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2033 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2034 | |
| 2035 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2036 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2037 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2038 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2039 | req->set_method("GET"); |
| 2040 | req->Start(); |
| 2041 | base::RunLoop().Run(); |
| 2042 | |
| 2043 | // Check that the interceptor got called as expected. |
| 2044 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2045 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2046 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2047 | |
| 2048 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2049 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2053 | // Intercept the main request to simulate a network error. |
| 2054 | interceptor()->set_simulate_main_network_error(true); |
| 2055 | |
| 2056 | // Set up to intercept final the response and cancel from within that job. |
| 2057 | interceptor()->set_cancel_final_request(true); |
| 2058 | |
| 2059 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2060 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2061 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2062 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2063 | req->set_method("GET"); |
| 2064 | req->Start(); |
| 2065 | base::RunLoop().Run(); |
| 2066 | |
| 2067 | // Check that the interceptor got called as expected. |
| 2068 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2069 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2070 | |
| 2071 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2072 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2076 | // Intercept the main request and cancel then restart from within that job. |
| 2077 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2078 | |
| 2079 | // Set up to intercept the final response and override it with an OK response. |
| 2080 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2081 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2082 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2083 | |
| 2084 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2085 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2086 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2087 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2088 | req->set_method("GET"); |
| 2089 | req->Start(); |
| 2090 | base::RunLoop().Run(); |
| 2091 | |
| 2092 | // Check that the interceptor got called as expected. |
| 2093 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2094 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2095 | |
| 2096 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2097 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2101 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2102 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2103 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2104 | int connect_time_flags, |
| 2105 | bool used_proxy) { |
| 2106 | LoadTimingInfo load_timing; |
| 2107 | load_timing.socket_log_id = 1; |
| 2108 | |
| 2109 | if (used_proxy) { |
| 2110 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2111 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2112 | } |
| 2113 | |
| 2114 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2115 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2116 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2117 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2118 | } |
| 2119 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2120 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2121 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2122 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2123 | } |
| 2124 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2125 | |
| 2126 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2127 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2128 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2129 | return load_timing; |
| 2130 | } |
| 2131 | |
| 2132 | // Same as above, but in the case of a reused socket. |
| 2133 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2134 | bool used_proxy) { |
| 2135 | LoadTimingInfo load_timing; |
| 2136 | load_timing.socket_log_id = 1; |
| 2137 | load_timing.socket_reused = true; |
| 2138 | |
| 2139 | if (used_proxy) { |
| 2140 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2141 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2142 | } |
| 2143 | |
| 2144 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2145 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2146 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2147 | return load_timing; |
| 2148 | } |
| 2149 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2150 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2151 | const LoadTimingInfo& job_load_timing, |
| 2152 | const URLRequestContext& context, |
| 2153 | MockURLRequestInterceptor* interceptor) { |
| 2154 | interceptor->set_intercept_main_request(true); |
| 2155 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2156 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2157 | std::unique_ptr<URLRequest> req( |
| 2158 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2159 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2160 | req->Start(); |
| 2161 | base::RunLoop().Run(); |
| 2162 | |
| 2163 | LoadTimingInfo resulting_load_timing; |
| 2164 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2165 | |
| 2166 | // None of these should be modified by the URLRequest. |
| 2167 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2168 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2169 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2170 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 2171 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2172 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2173 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2174 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2175 | |
| 2176 | return resulting_load_timing; |
| 2177 | } |
| 2178 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2179 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2180 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2181 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2182 | LoadTimingInfo job_load_timing = |
| 2183 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2184 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2185 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2186 | RunURLRequestInterceptorLoadTimingTest( |
| 2187 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2188 | |
| 2189 | // Nothing should have been changed by the URLRequest. |
| 2190 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2191 | load_timing_result.proxy_resolve_start); |
| 2192 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2193 | load_timing_result.proxy_resolve_end); |
| 2194 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2195 | load_timing_result.connect_timing.dns_start); |
| 2196 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2197 | load_timing_result.connect_timing.dns_end); |
| 2198 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2199 | load_timing_result.connect_timing.connect_start); |
| 2200 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2201 | load_timing_result.connect_timing.connect_end); |
| 2202 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2203 | load_timing_result.connect_timing.ssl_start); |
| 2204 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2205 | load_timing_result.connect_timing.ssl_end); |
| 2206 | |
| 2207 | // Redundant sanity check. |
| 2208 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2209 | } |
| 2210 | |
| 2211 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2212 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2213 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2214 | LoadTimingInfo job_load_timing = |
| 2215 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2216 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2217 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2218 | RunURLRequestInterceptorLoadTimingTest( |
| 2219 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2220 | |
| 2221 | // Nothing should have been changed by the URLRequest. |
| 2222 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2223 | load_timing_result.proxy_resolve_start); |
| 2224 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2225 | load_timing_result.proxy_resolve_end); |
| 2226 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2227 | load_timing_result.connect_timing.dns_start); |
| 2228 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2229 | load_timing_result.connect_timing.dns_end); |
| 2230 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2231 | load_timing_result.connect_timing.connect_start); |
| 2232 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2233 | load_timing_result.connect_timing.connect_end); |
| 2234 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2235 | load_timing_result.connect_timing.ssl_start); |
| 2236 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2237 | load_timing_result.connect_timing.ssl_end); |
| 2238 | |
| 2239 | // Redundant sanity check. |
| 2240 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2241 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2242 | } |
| 2243 | |
| 2244 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2245 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2246 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2247 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2248 | // |
| 2249 | // 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] | 2250 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2251 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2252 | LoadTimingInfo job_load_timing = |
| 2253 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2254 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2255 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2256 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2257 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2258 | job_load_timing.connect_timing.connect_start = |
| 2259 | now - base::TimeDelta::FromDays(2); |
| 2260 | job_load_timing.connect_timing.connect_end = |
| 2261 | now - base::TimeDelta::FromDays(1); |
| 2262 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2263 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2264 | RunURLRequestInterceptorLoadTimingTest( |
| 2265 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2266 | |
| 2267 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2268 | // request_start. |
| 2269 | EXPECT_EQ(load_timing_result.request_start, |
| 2270 | load_timing_result.proxy_resolve_start); |
| 2271 | EXPECT_EQ(load_timing_result.request_start, |
| 2272 | load_timing_result.proxy_resolve_end); |
| 2273 | EXPECT_EQ(load_timing_result.request_start, |
| 2274 | load_timing_result.connect_timing.dns_start); |
| 2275 | EXPECT_EQ(load_timing_result.request_start, |
| 2276 | load_timing_result.connect_timing.dns_end); |
| 2277 | EXPECT_EQ(load_timing_result.request_start, |
| 2278 | load_timing_result.connect_timing.connect_start); |
| 2279 | EXPECT_EQ(load_timing_result.request_start, |
| 2280 | load_timing_result.connect_timing.connect_end); |
| 2281 | |
| 2282 | // Other times should have been left null. |
| 2283 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2284 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2285 | } |
| 2286 | |
| 2287 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2288 | TEST_F(URLRequestInterceptorTest, |
| 2289 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2290 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2291 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2292 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2293 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2294 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2295 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2296 | RunURLRequestInterceptorLoadTimingTest( |
| 2297 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2298 | |
| 2299 | // Proxy times and connect times should all be replaced with request_start. |
| 2300 | EXPECT_EQ(load_timing_result.request_start, |
| 2301 | load_timing_result.proxy_resolve_start); |
| 2302 | EXPECT_EQ(load_timing_result.request_start, |
| 2303 | load_timing_result.proxy_resolve_end); |
| 2304 | |
| 2305 | // Other times should have been left null. |
| 2306 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2307 | } |
| 2308 | |
| 2309 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2310 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2311 | // not considered reused in this test (May be a preconnect). |
| 2312 | // |
| 2313 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2314 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2315 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2316 | LoadTimingInfo job_load_timing = |
| 2317 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2318 | job_load_timing.connect_timing.connect_start = |
| 2319 | now - base::TimeDelta::FromDays(1); |
| 2320 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2321 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2322 | job_load_timing.connect_timing.connect_end = |
| 2323 | now - base::TimeDelta::FromDays(4); |
| 2324 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2325 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2326 | RunURLRequestInterceptorLoadTimingTest( |
| 2327 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2328 | |
| 2329 | // Connect times, and SSL times should be replaced with request_start. |
| 2330 | EXPECT_EQ(load_timing_result.request_start, |
| 2331 | load_timing_result.connect_timing.connect_start); |
| 2332 | EXPECT_EQ(load_timing_result.request_start, |
| 2333 | load_timing_result.connect_timing.ssl_start); |
| 2334 | EXPECT_EQ(load_timing_result.request_start, |
| 2335 | load_timing_result.connect_timing.ssl_end); |
| 2336 | EXPECT_EQ(load_timing_result.request_start, |
| 2337 | load_timing_result.connect_timing.connect_end); |
| 2338 | |
| 2339 | // Other times should have been left null. |
| 2340 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2341 | } |
| 2342 | |
| 2343 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2344 | // |request_start|, due to reusing a connected socket in the case that there |
| 2345 | // are also proxy times. The connected socket is not considered reused in this |
| 2346 | // test (May be a preconnect). |
| 2347 | // |
| 2348 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2349 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2350 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2351 | LoadTimingInfo job_load_timing = |
| 2352 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2353 | job_load_timing.connect_timing.connect_start = |
| 2354 | now - base::TimeDelta::FromDays(1); |
| 2355 | job_load_timing.connect_timing.connect_end = |
| 2356 | now - base::TimeDelta::FromDays(2); |
| 2357 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2358 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2359 | RunURLRequestInterceptorLoadTimingTest( |
| 2360 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2361 | |
| 2362 | // Connect times should be replaced with proxy_resolve_end. |
| 2363 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2364 | load_timing_result.connect_timing.connect_start); |
| 2365 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2366 | load_timing_result.connect_timing.connect_end); |
| 2367 | |
| 2368 | // Other times should have been left null. |
| 2369 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2370 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2371 | } |
| 2372 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2373 | // Check that two different URL requests have different identifiers. |
| 2374 | TEST_F(URLRequestTest, Identifiers) { |
| 2375 | TestDelegate d; |
| 2376 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2377 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2378 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2379 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2380 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2381 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2382 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2383 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2384 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2385 | } |
| 2386 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2387 | #if defined(OS_IOS) |
| 2388 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2389 | // the network delegate. crbug.com/496743 |
| 2390 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2391 | #else |
| 2392 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2393 | #endif |
| 2394 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2395 | MockHostResolver host_resolver; |
| 2396 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2397 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2398 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2399 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2400 | |
| 2401 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2402 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2403 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2404 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2405 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2406 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2407 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2408 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2409 | |
| 2410 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2411 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2412 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2413 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2414 | |
| 2415 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2416 | EXPECT_THAT(network_delegate.last_error(), |
| 2417 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2418 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2419 | } |
| 2420 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2421 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2422 | // content is empty. |
| 2423 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2424 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2425 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2426 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2427 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2428 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2429 | EXPECT_EQ("", d.data_received()); |
| 2430 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2431 | } |
| 2432 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2433 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2434 | // correctly, both before and after start. |
| 2435 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2436 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2437 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2438 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2439 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2440 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2441 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2442 | req->SetPriority(LOW); |
| 2443 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2444 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | req->Start(); |
| 2446 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2447 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2448 | req->SetPriority(MEDIUM); |
| 2449 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2453 | // Start on it. |
| 2454 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2455 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2456 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2457 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2458 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2459 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2460 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2461 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2462 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2463 | req.get(), &default_network_delegate_, &job_priority)); |
| 2464 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2465 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2466 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2467 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2468 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2469 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2470 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | // Make sure that URLRequest passes on its priority updates to its |
| 2474 | // job. |
| 2475 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2476 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2477 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2478 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2479 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2480 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2481 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2482 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2483 | req.get(), &default_network_delegate_, &job_priority)); |
| 2484 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2485 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2486 | req->SetPriority(LOW); |
| 2487 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2488 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2489 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2490 | req->SetPriority(MEDIUM); |
| 2491 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2492 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2493 | } |
| 2494 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2495 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2496 | // is MAXIMUM_PRIORITY. |
| 2497 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2498 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2499 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2500 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2501 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2502 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2503 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2504 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2505 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2506 | req.get(), &default_network_delegate_, &job_priority)); |
| 2507 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2509 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2510 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2511 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2512 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2513 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2514 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2515 | req->Start(); |
| 2516 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2517 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2518 | } |
| 2519 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2520 | namespace { |
| 2521 | |
| 2522 | // Less verbose way of running a simple testserver for the tests below. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2523 | class LocalHttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2524 | public: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2525 | explicit LocalHttpTestServer(const base::FilePath& document_root) { |
| 2526 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2527 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2528 | |
| 2529 | LocalHttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
| 2530 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2531 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2532 | } // namespace |
| 2533 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2534 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2535 | LocalHttpTestServer test_server; |
| 2536 | ASSERT_TRUE(test_server.Start()); |
| 2537 | |
| 2538 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2539 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2540 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2541 | |
| 2542 | // Set up a cookie. |
| 2543 | { |
| 2544 | TestNetworkDelegate network_delegate; |
| 2545 | context.set_network_delegate(&network_delegate); |
| 2546 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2547 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2548 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2549 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2550 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2551 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2552 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2553 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2554 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2555 | } |
| 2556 | |
| 2557 | // Verify that the cookie is set. |
| 2558 | { |
| 2559 | TestNetworkDelegate network_delegate; |
| 2560 | context.set_network_delegate(&network_delegate); |
| 2561 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2562 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2563 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2564 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2565 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2566 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2567 | |
| 2568 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2569 | != std::string::npos); |
| 2570 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2571 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2572 | } |
| 2573 | } |
| 2574 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2575 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2576 | LocalHttpTestServer test_server; |
| 2577 | ASSERT_TRUE(test_server.Start()); |
| 2578 | |
| 2579 | // Set up a cookie. |
| 2580 | { |
| 2581 | TestNetworkDelegate network_delegate; |
| 2582 | default_context_.set_network_delegate(&network_delegate); |
| 2583 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2584 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2585 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2586 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2587 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2588 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2589 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2590 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2591 | } |
| 2592 | |
| 2593 | // Verify that the cookie is set. |
| 2594 | { |
| 2595 | TestNetworkDelegate network_delegate; |
| 2596 | default_context_.set_network_delegate(&network_delegate); |
| 2597 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2598 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2599 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2600 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2601 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2602 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2603 | |
| 2604 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2605 | != std::string::npos); |
| 2606 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2607 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2608 | } |
| 2609 | |
| 2610 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2611 | { |
| 2612 | TestNetworkDelegate network_delegate; |
| 2613 | default_context_.set_network_delegate(&network_delegate); |
| 2614 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2615 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2616 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2617 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2618 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2619 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2620 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2621 | |
| 2622 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2623 | == std::string::npos); |
| 2624 | |
| 2625 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2626 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2627 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2632 | LocalHttpTestServer test_server; |
| 2633 | ASSERT_TRUE(test_server.Start()); |
| 2634 | |
| 2635 | // Set up a cookie. |
| 2636 | { |
| 2637 | TestNetworkDelegate network_delegate; |
| 2638 | default_context_.set_network_delegate(&network_delegate); |
| 2639 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2640 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2641 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2642 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2643 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2644 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2645 | |
| 2646 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2647 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2648 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2649 | } |
| 2650 | |
| 2651 | // Try to set-up another cookie and update the previous cookie. |
| 2652 | { |
| 2653 | TestNetworkDelegate network_delegate; |
| 2654 | default_context_.set_network_delegate(&network_delegate); |
| 2655 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2656 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2657 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2658 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2659 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2660 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2661 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2662 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2663 | |
| 2664 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2665 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2666 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2667 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2668 | } |
| 2669 | |
| 2670 | // Verify the cookies weren't saved or updated. |
| 2671 | { |
| 2672 | TestNetworkDelegate network_delegate; |
| 2673 | default_context_.set_network_delegate(&network_delegate); |
| 2674 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2675 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2676 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2677 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2678 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2679 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2680 | |
| 2681 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2682 | == std::string::npos); |
| 2683 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2684 | != std::string::npos); |
| 2685 | |
| 2686 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2687 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2688 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2689 | } |
| 2690 | } |
| 2691 | |
| 2692 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2693 | LocalHttpTestServer test_server; |
| 2694 | ASSERT_TRUE(test_server.Start()); |
| 2695 | |
| 2696 | // Set up a cookie. |
| 2697 | { |
| 2698 | TestNetworkDelegate network_delegate; |
| 2699 | default_context_.set_network_delegate(&network_delegate); |
| 2700 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2701 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2702 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2703 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2704 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2705 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2706 | |
| 2707 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2708 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2709 | } |
| 2710 | |
| 2711 | // Verify that the cookie is set. |
| 2712 | { |
| 2713 | TestNetworkDelegate network_delegate; |
| 2714 | default_context_.set_network_delegate(&network_delegate); |
| 2715 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2716 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2717 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2718 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2719 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2720 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2721 | |
| 2722 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2723 | != std::string::npos); |
| 2724 | |
| 2725 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2726 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2727 | } |
| 2728 | |
| 2729 | // Verify that the cookie isn't sent. |
| 2730 | { |
| 2731 | TestNetworkDelegate network_delegate; |
| 2732 | default_context_.set_network_delegate(&network_delegate); |
| 2733 | TestDelegate d; |
| 2734 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2735 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2736 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2737 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2738 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2739 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2740 | |
| 2741 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2742 | == std::string::npos); |
| 2743 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2744 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2745 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2746 | } |
| 2747 | } |
| 2748 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2749 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2750 | #if defined(OS_IOS) |
| 2751 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2752 | #else |
| 2753 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2754 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2755 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2756 | LocalHttpTestServer test_server; |
| 2757 | ASSERT_TRUE(test_server.Start()); |
| 2758 | |
| 2759 | // Set up a cookie. |
| 2760 | { |
| 2761 | TestNetworkDelegate network_delegate; |
| 2762 | default_context_.set_network_delegate(&network_delegate); |
| 2763 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2764 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2765 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2766 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2767 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2768 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2769 | |
| 2770 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2771 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2772 | } |
| 2773 | |
| 2774 | // Try to set-up another cookie and update the previous cookie. |
| 2775 | { |
| 2776 | TestNetworkDelegate network_delegate; |
| 2777 | default_context_.set_network_delegate(&network_delegate); |
| 2778 | TestDelegate d; |
| 2779 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2780 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2781 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2782 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2783 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2784 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2785 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2786 | |
| 2787 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2788 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2789 | } |
| 2790 | |
| 2791 | // Verify the cookies weren't saved or updated. |
| 2792 | { |
| 2793 | TestNetworkDelegate network_delegate; |
| 2794 | default_context_.set_network_delegate(&network_delegate); |
| 2795 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2796 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2797 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2798 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2799 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2800 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2801 | |
| 2802 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2803 | == std::string::npos); |
| 2804 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2805 | != std::string::npos); |
| 2806 | |
| 2807 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2808 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2813 | LocalHttpTestServer test_server; |
| 2814 | ASSERT_TRUE(test_server.Start()); |
| 2815 | |
| 2816 | // Set up an empty cookie. |
| 2817 | { |
| 2818 | TestNetworkDelegate network_delegate; |
| 2819 | default_context_.set_network_delegate(&network_delegate); |
| 2820 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2821 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2822 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2823 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2824 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2825 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2826 | |
| 2827 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2828 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2829 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2834 | LocalHttpTestServer test_server; |
| 2835 | ASSERT_TRUE(test_server.Start()); |
| 2836 | |
| 2837 | // Set up a cookie. |
| 2838 | { |
| 2839 | TestNetworkDelegate network_delegate; |
| 2840 | default_context_.set_network_delegate(&network_delegate); |
| 2841 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2842 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2843 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2844 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2845 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2846 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2847 | |
| 2848 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2849 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2850 | } |
| 2851 | |
| 2852 | // Verify that the cookie is set. |
| 2853 | { |
| 2854 | TestNetworkDelegate network_delegate; |
| 2855 | default_context_.set_network_delegate(&network_delegate); |
| 2856 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2857 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2858 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2859 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2860 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2861 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2862 | |
| 2863 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2864 | != std::string::npos); |
| 2865 | |
| 2866 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2867 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2868 | } |
| 2869 | |
| 2870 | // Verify that the cookie isn't sent. |
| 2871 | { |
| 2872 | TestNetworkDelegate network_delegate; |
| 2873 | default_context_.set_network_delegate(&network_delegate); |
| 2874 | TestDelegate d; |
| 2875 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2876 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2877 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2878 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2879 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2880 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2881 | |
| 2882 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2883 | == std::string::npos); |
| 2884 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2885 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2886 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2891 | LocalHttpTestServer test_server; |
| 2892 | ASSERT_TRUE(test_server.Start()); |
| 2893 | |
| 2894 | // Set up a cookie. |
| 2895 | { |
| 2896 | TestNetworkDelegate network_delegate; |
| 2897 | default_context_.set_network_delegate(&network_delegate); |
| 2898 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2899 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2900 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2901 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2902 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2903 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2904 | |
| 2905 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2906 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2907 | } |
| 2908 | |
| 2909 | // Try to set-up another cookie and update the previous cookie. |
| 2910 | { |
| 2911 | TestNetworkDelegate network_delegate; |
| 2912 | default_context_.set_network_delegate(&network_delegate); |
| 2913 | TestDelegate d; |
| 2914 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2915 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2916 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2917 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2918 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2919 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2920 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2921 | |
| 2922 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2923 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2924 | } |
| 2925 | |
| 2926 | // Verify the cookies weren't saved or updated. |
| 2927 | { |
| 2928 | TestNetworkDelegate network_delegate; |
| 2929 | default_context_.set_network_delegate(&network_delegate); |
| 2930 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2931 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2932 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2933 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2934 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2935 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2936 | |
| 2937 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2938 | == std::string::npos); |
| 2939 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2940 | != std::string::npos); |
| 2941 | |
| 2942 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2943 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2944 | } |
| 2945 | } |
| 2946 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2947 | TEST_F(URLRequestTest, SameSiteCookies) { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2948 | LocalHttpTestServer test_server; |
| 2949 | ASSERT_TRUE(test_server.Start()); |
| 2950 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2951 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2952 | default_context_.set_network_delegate(&network_delegate); |
| 2953 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2954 | const std::string kHost = "example.test"; |
| 2955 | const std::string kSubHost = "subdomain.example.test"; |
| 2956 | const std::string kCrossHost = "cross-origin.test"; |
| 2957 | |
| 2958 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2959 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2960 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2961 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2962 | test_server.GetURL(kHost, |
| 2963 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2964 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2965 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2966 | req->Start(); |
| 2967 | base::RunLoop().Run(); |
| 2968 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2969 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2970 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2971 | } |
| 2972 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2973 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2974 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2975 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2976 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2977 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2978 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2979 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2980 | req->set_initiator(url::Origin(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2981 | req->Start(); |
| 2982 | base::RunLoop().Run(); |
| 2983 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2984 | EXPECT_NE(std::string::npos, |
| 2985 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2986 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2987 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2988 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2989 | } |
| 2990 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2991 | // Verify that both cookies are sent when the request has no initiator (can |
| 2992 | // happen for main frame browser-initiated navigations). |
| 2993 | { |
| 2994 | TestDelegate d; |
| 2995 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2996 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2997 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2998 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2999 | req->Start(); |
| 3000 | base::RunLoop().Run(); |
| 3001 | |
| 3002 | EXPECT_NE(std::string::npos, |
| 3003 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3004 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3005 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3006 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3007 | } |
| 3008 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3009 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3010 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3011 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3012 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3013 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3014 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3015 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3016 | req->set_initiator(url::Origin(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3017 | req->Start(); |
| 3018 | base::RunLoop().Run(); |
| 3019 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3020 | EXPECT_NE(std::string::npos, |
| 3021 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3022 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3023 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3024 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3025 | } |
| 3026 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3027 | // Verify that neither cookie is not sent for cross-site requests. |
| 3028 | { |
| 3029 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3030 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3031 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3032 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3033 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3034 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 3035 | req->Start(); |
| 3036 | base::RunLoop().Run(); |
| 3037 | |
| 3038 | EXPECT_EQ(std::string::npos, |
| 3039 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3040 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3041 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3042 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3043 | } |
| 3044 | |
| 3045 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3046 | // method is "safe". |
| 3047 | { |
| 3048 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3049 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3050 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3051 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3052 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3053 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 3054 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3055 | req->Start(); |
| 3056 | base::RunLoop().Run(); |
| 3057 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3058 | EXPECT_EQ(std::string::npos, |
| 3059 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3060 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3061 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3062 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3063 | } |
| 3064 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3065 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3066 | // method is unsafe (e.g. POST). |
| 3067 | { |
| 3068 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3069 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3070 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3071 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3072 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3073 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3074 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3075 | req->Start(); |
| 3076 | base::RunLoop().Run(); |
| 3077 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3078 | EXPECT_EQ(std::string::npos, |
| 3079 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3080 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3081 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3082 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3083 | } |
| 3084 | } |
| 3085 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3086 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3087 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3088 | EmbeddedTestServer http_server; |
| 3089 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3090 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3091 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3092 | https_server.AddDefaultHandlers( |
| 3093 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3094 | ASSERT_TRUE(http_server.Start()); |
| 3095 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3096 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3097 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3098 | TestURLRequestContext context(true); |
| 3099 | context.set_network_delegate(&network_delegate); |
| 3100 | context.Init(); |
| 3101 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3102 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3103 | { |
| 3104 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3105 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3106 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3107 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3108 | req->Start(); |
| 3109 | base::RunLoop().Run(); |
| 3110 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3111 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3112 | } |
| 3113 | |
| 3114 | // Verify that the cookie is not set. |
| 3115 | { |
| 3116 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3117 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3118 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3119 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3120 | req->Start(); |
| 3121 | base::RunLoop().Run(); |
| 3122 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3123 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3124 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3125 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3126 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3127 | } |
| 3128 | } |
| 3129 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3130 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3131 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3132 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3133 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3134 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3135 | |
| 3136 | TestNetworkDelegate network_delegate; |
| 3137 | TestURLRequestContext context(true); |
| 3138 | context.set_network_delegate(&network_delegate); |
| 3139 | context.Init(); |
| 3140 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3141 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3142 | { |
| 3143 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3144 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3145 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3146 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3147 | req->Start(); |
| 3148 | base::RunLoop().Run(); |
| 3149 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3150 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3151 | } |
| 3152 | |
| 3153 | // Verify that the cookie is not set. |
| 3154 | { |
| 3155 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3156 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3157 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3158 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3159 | req->Start(); |
| 3160 | base::RunLoop().Run(); |
| 3161 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3162 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3163 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3164 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3165 | } |
| 3166 | } |
| 3167 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3168 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3169 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3170 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3171 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3172 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3173 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3174 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3175 | TestURLRequestContext context(true); |
| 3176 | context.set_network_delegate(&network_delegate); |
| 3177 | context.Init(); |
| 3178 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3179 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3180 | { |
| 3181 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3182 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3183 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3184 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3185 | req->Start(); |
| 3186 | base::RunLoop().Run(); |
| 3187 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3188 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3189 | } |
| 3190 | |
| 3191 | // Verify that the cookie is set. |
| 3192 | { |
| 3193 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3194 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3195 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3196 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3197 | req->Start(); |
| 3198 | base::RunLoop().Run(); |
| 3199 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3200 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3201 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3202 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3203 | } |
| 3204 | } |
| 3205 | |
| 3206 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3207 | // cookies are enabled. |
| 3208 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3209 | EmbeddedTestServer http_server; |
| 3210 | http_server.AddDefaultHandlers( |
| 3211 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3212 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3213 | https_server.AddDefaultHandlers( |
| 3214 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3215 | ASSERT_TRUE(http_server.Start()); |
| 3216 | ASSERT_TRUE(https_server.Start()); |
| 3217 | |
| 3218 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3219 | TestURLRequestContext context(true); |
| 3220 | context.set_network_delegate(&network_delegate); |
| 3221 | context.Init(); |
| 3222 | |
| 3223 | // Try to set a Secure cookie, with experimental features enabled. |
| 3224 | { |
| 3225 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3226 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3227 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3228 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3229 | req->Start(); |
| 3230 | base::RunLoop().Run(); |
| 3231 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3232 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3233 | } |
| 3234 | |
| 3235 | // Verify that the cookie is not set. |
| 3236 | { |
| 3237 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3238 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3239 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3240 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3241 | req->Start(); |
| 3242 | base::RunLoop().Run(); |
| 3243 | |
| 3244 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3245 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3246 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 3251 | // cookies are enabled. |
| 3252 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 3253 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3254 | https_server.AddDefaultHandlers( |
| 3255 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3256 | ASSERT_TRUE(https_server.Start()); |
| 3257 | |
| 3258 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3259 | TestURLRequestContext context(true); |
| 3260 | context.set_network_delegate(&network_delegate); |
| 3261 | context.Init(); |
| 3262 | |
| 3263 | // Try to set a Secure cookie, with experimental features enabled. |
| 3264 | { |
| 3265 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3266 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3267 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3268 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3269 | req->Start(); |
| 3270 | base::RunLoop().Run(); |
| 3271 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3272 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3273 | } |
| 3274 | |
| 3275 | // Verify that the cookie is not set. |
| 3276 | { |
| 3277 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3278 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3279 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3280 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3281 | req->Start(); |
| 3282 | base::RunLoop().Run(); |
| 3283 | |
| 3284 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3285 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3286 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3287 | } |
| 3288 | } |
| 3289 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3290 | TEST_F(URLRequestTest, CookieAgeMetrics) { |
| 3291 | EmbeddedTestServer http_server; |
| 3292 | http_server.AddDefaultHandlers( |
| 3293 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3294 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3295 | https_server.AddDefaultHandlers( |
| 3296 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3297 | ASSERT_TRUE(http_server.Start()); |
| 3298 | ASSERT_TRUE(https_server.Start()); |
| 3299 | |
| 3300 | TestNetworkDelegate network_delegate; |
| 3301 | default_context_.set_network_delegate(&network_delegate); |
| 3302 | base::HistogramTester histograms; |
| 3303 | |
| 3304 | const std::string kHost = "example.test"; |
| 3305 | const std::string kCrossHost = "cross-origin.test"; |
| 3306 | |
| 3307 | // Set a test cookie. |
| 3308 | { |
| 3309 | TestDelegate d; |
| 3310 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3311 | http_server.GetURL(kHost, "/set-cookie?cookie=value"), DEFAULT_PRIORITY, |
| 3312 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3313 | req->Start(); |
| 3314 | base::RunLoop().Run(); |
| 3315 | ASSERT_EQ(1, network_delegate.set_cookie_count()); |
| 3316 | } |
| 3317 | |
| 3318 | // Make a secure request to `example.test`: we shouldn't record data. |
| 3319 | { |
| 3320 | TestDelegate d; |
| 3321 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3322 | https_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3323 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3324 | req->Start(); |
| 3325 | base::RunLoop().Run(); |
| 3326 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3327 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3328 | } |
| 3329 | |
| 3330 | // Make a non-secure same-site request. |
| 3331 | { |
| 3332 | TestDelegate d; |
| 3333 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3334 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3335 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3336 | req->set_site_for_cookies(http_server.GetURL(kHost, "/")); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3337 | req->set_initiator(url::Origin(http_server.GetURL(kHost, "/"))); |
| 3338 | req->Start(); |
| 3339 | base::RunLoop().Run(); |
| 3340 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3341 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3342 | } |
| 3343 | |
| 3344 | // Make a non-secure cross-site request. |
| 3345 | { |
| 3346 | TestDelegate d; |
| 3347 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3348 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3349 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3350 | req->set_site_for_cookies(http_server.GetURL(kCrossHost, "/")); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3351 | req->set_initiator(url::Origin(http_server.GetURL(kCrossHost, "/"))); |
| 3352 | req->Start(); |
| 3353 | base::RunLoop().Run(); |
| 3354 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 1); |
| 3355 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
| 3356 | } |
| 3357 | } |
| 3358 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3359 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3360 | // rather than a spawned test server because the connection used to talk to |
| 3361 | // the test server is affected by entering suspend mode on Android. |
| 3362 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3363 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3364 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3365 | |
| 3366 | URLRequestFailedJob::AddUrlHandler(); |
| 3367 | |
| 3368 | TestDelegate d; |
| 3369 | // Request that just hangs. |
| 3370 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3371 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3372 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3373 | r->Start(); |
| 3374 | |
| 3375 | power_monitor_source->Suspend(); |
| 3376 | // Wait for the suspend notification to cause the request to fail. |
| 3377 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3378 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3379 | EXPECT_TRUE(d.request_failed()); |
| 3380 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3381 | |
| 3382 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3383 | |
| 3384 | // Shouldn't be needed, but just in case. |
| 3385 | power_monitor_source->Resume(); |
| 3386 | } |
| 3387 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3388 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3389 | // value for the |fixed_date| argument given to the constructor. |
| 3390 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3391 | public: |
| 3392 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3393 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3394 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3395 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3396 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3397 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3398 | URLRequest* request, |
| 3399 | const CompletionCallback& callback, |
| 3400 | const HttpResponseHeaders* original_response_headers, |
| 3401 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3402 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3403 | |
| 3404 | private: |
| 3405 | std::string fixed_date_; |
| 3406 | |
| 3407 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3408 | }; |
| 3409 | |
| 3410 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3411 | URLRequest* request, |
| 3412 | const CompletionCallback& callback, |
| 3413 | const HttpResponseHeaders* original_response_headers, |
| 3414 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3415 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3416 | HttpResponseHeaders* new_response_headers = |
| 3417 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3418 | |
| 3419 | new_response_headers->RemoveHeader("Date"); |
| 3420 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3421 | |
| 3422 | *override_response_headers = new_response_headers; |
| 3423 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3424 | callback, |
| 3425 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3426 | override_response_headers, |
| 3427 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | // Test that cookie expiration times are adjusted for server/client clock |
| 3431 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3432 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3433 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 3434 | LocalHttpTestServer test_server; |
| 3435 | ASSERT_TRUE(test_server.Start()); |
| 3436 | |
| 3437 | // Set up an expired cookie. |
| 3438 | { |
| 3439 | TestNetworkDelegate network_delegate; |
| 3440 | default_context_.set_network_delegate(&network_delegate); |
| 3441 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3442 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3443 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3444 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3445 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3446 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3447 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3448 | } |
| 3449 | // Verify that the cookie is not set. |
| 3450 | { |
| 3451 | TestNetworkDelegate network_delegate; |
| 3452 | default_context_.set_network_delegate(&network_delegate); |
| 3453 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3454 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3455 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3456 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3457 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3458 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3459 | |
| 3460 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3461 | } |
| 3462 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3463 | { |
| 3464 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3465 | default_context_.set_network_delegate(&network_delegate); |
| 3466 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3467 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3468 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3469 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3470 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3471 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3472 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3473 | } |
| 3474 | // Verify that the cookie is set. |
| 3475 | { |
| 3476 | TestNetworkDelegate network_delegate; |
| 3477 | default_context_.set_network_delegate(&network_delegate); |
| 3478 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3479 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3480 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3482 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3483 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3484 | |
| 3485 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3490 | // Check that it is impossible to change the referrer in the extra headers of |
| 3491 | // an URLRequest. |
| 3492 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 3493 | LocalHttpTestServer test_server; |
| 3494 | ASSERT_TRUE(test_server.Start()); |
| 3495 | |
| 3496 | // If extra headers contain referer and the request contains a referer, |
| 3497 | // only the latter shall be respected. |
| 3498 | { |
| 3499 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3500 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3501 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3502 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3503 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3504 | |
| 3505 | HttpRequestHeaders headers; |
| 3506 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3507 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3509 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3510 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3511 | |
| 3512 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3513 | } |
| 3514 | |
| 3515 | // If extra headers contain a referer but the request does not, no referer |
| 3516 | // shall be sent in the header. |
| 3517 | { |
| 3518 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3519 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3520 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3521 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3522 | |
| 3523 | HttpRequestHeaders headers; |
| 3524 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3525 | req->SetExtraRequestHeaders(headers); |
| 3526 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3527 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3528 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3529 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3530 | |
| 3531 | EXPECT_EQ("None", d.data_received()); |
| 3532 | } |
| 3533 | } |
| 3534 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3535 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3536 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3537 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3538 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3539 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3540 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3541 | // |request_method| is the method to use for the initial request. |
| 3542 | // |redirect_method| is the method that is expected to be used for the second |
| 3543 | // request, after redirection. |
| 3544 | // If |include_data| is true, data is uploaded with the request. The |
| 3545 | // response body is expected to match it exactly, if and only if |
| 3546 | // |request_method| == |redirect_method|. |
| 3547 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3548 | const std::string& request_method, |
| 3549 | const std::string& redirect_method, |
| 3550 | bool include_data) { |
| 3551 | static const char kData[] = "hello world"; |
| 3552 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3553 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3554 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3555 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3556 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3557 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3558 | HttpRequestHeaders headers; |
| 3559 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3560 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3561 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3562 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3563 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3564 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3565 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3566 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3567 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3568 | if (include_data) { |
| 3569 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3570 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3571 | HttpRequestHeaders::kContentLength)); |
| 3572 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3573 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3574 | EXPECT_EQ(kData, d.data_received()); |
| 3575 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3576 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3577 | HttpRequestHeaders::kContentLength)); |
| 3578 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3579 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3580 | EXPECT_NE(kData, d.data_received()); |
| 3581 | } |
| 3582 | } |
| 3583 | if (HasFailure()) |
| 3584 | LOG(WARNING) << "Request method was: " << request_method; |
| 3585 | } |
| 3586 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3587 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3588 | // |request_method| is the method to use for the initial request. |
| 3589 | // |redirect_method| is the method that is expected to be used for the second |
| 3590 | // request, after redirection. |
| 3591 | // |origin_value| is the expected value for the Origin header after |
| 3592 | // redirection. If empty, expects that there will be no Origin header. |
| 3593 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3594 | const std::string& request_method, |
| 3595 | const std::string& redirect_method, |
| 3596 | const std::string& origin_value) { |
| 3597 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3598 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3599 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3600 | req->set_method(request_method); |
| 3601 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3602 | redirect_url.GetOrigin().spec(), false); |
| 3603 | req->Start(); |
| 3604 | |
| 3605 | base::RunLoop().Run(); |
| 3606 | |
| 3607 | EXPECT_EQ(redirect_method, req->method()); |
| 3608 | // Note that there is no check for request success here because, for |
| 3609 | // purposes of testing, the request very well may fail. For example, if the |
| 3610 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3611 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3612 | // request would fail. However, that's fine, as long as the request headers |
| 3613 | // are in order and pass the checks below. |
| 3614 | if (origin_value.empty()) { |
| 3615 | EXPECT_FALSE( |
| 3616 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3617 | } else { |
| 3618 | std::string origin_header; |
| 3619 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3620 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 3621 | EXPECT_EQ(origin_value, origin_header); |
| 3622 | } |
| 3623 | } |
| 3624 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3625 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3626 | const int kMsgSize = 20000; // multiple of 10 |
| 3627 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3628 | char* uploadBytes = new char[kMsgSize+1]; |
| 3629 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3630 | char marker = 'a'; |
| 3631 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3632 | memcpy(ptr, "----------", 10); |
| 3633 | ptr += 10; |
| 3634 | if (idx % 100 == 0) { |
| 3635 | ptr--; |
| 3636 | *ptr++ = marker; |
| 3637 | if (++marker > 'z') |
| 3638 | marker = 'a'; |
| 3639 | } |
| 3640 | } |
| 3641 | uploadBytes[kMsgSize] = '\0'; |
| 3642 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3643 | for (int i = 0; i < kIterations; ++i) { |
| 3644 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3645 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3646 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 3647 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3648 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3649 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3650 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3651 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3652 | r->Start(); |
| 3653 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3654 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3655 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3656 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3657 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 3658 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3659 | |
| 3660 | EXPECT_FALSE(d.received_data_before_response()); |
| 3661 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3662 | } |
| 3663 | delete[] uploadBytes; |
| 3664 | } |
| 3665 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3666 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3667 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3668 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3669 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3670 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3671 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3672 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3673 | r->Start(); |
| 3674 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3675 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3676 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3677 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3678 | if (d.request_status() != OK) { |
| 3679 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 3680 | return false; |
| 3681 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3682 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3683 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3684 | } |
| 3685 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3686 | LocalHttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3687 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3688 | private: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3689 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3690 | }; |
| 3691 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3692 | namespace { |
| 3693 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3694 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3695 | const test_server::HttpRequest& request) { |
| 3696 | if (request.headers.find("Host") == request.headers.end() || |
| 3697 | request.headers.at("Host") != "www.redirect.com" || |
| 3698 | request.method != test_server::METHOD_CONNECT) { |
| 3699 | return nullptr; |
| 3700 | } |
| 3701 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3702 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3703 | new test_server::BasicHttpResponse); |
| 3704 | http_response->set_code(HTTP_FOUND); |
| 3705 | http_response->AddCustomHeader("Location", |
| 3706 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3707 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | } // namespace |
| 3711 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3712 | class TestSSLConfigService : public SSLConfigService { |
| 3713 | public: |
| 3714 | TestSSLConfigService(bool ev_enabled, |
| 3715 | bool online_rev_checking, |
| 3716 | bool rev_checking_required_local_anchors, |
| 3717 | bool token_binding_enabled) |
| 3718 | : ev_enabled_(ev_enabled), |
| 3719 | online_rev_checking_(online_rev_checking), |
| 3720 | rev_checking_required_local_anchors_( |
| 3721 | rev_checking_required_local_anchors), |
| 3722 | token_binding_enabled_(token_binding_enabled), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3723 | min_version_(kDefaultSSLVersionMin), |
| 3724 | max_version_(kDefaultSSLVersionMax) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3725 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3726 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3727 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3728 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3729 | // SSLConfigService: |
| 3730 | void GetSSLConfig(SSLConfig* config) override { |
| 3731 | *config = SSLConfig(); |
| 3732 | config->rev_checking_enabled = online_rev_checking_; |
| 3733 | config->verify_ev_cert = ev_enabled_; |
| 3734 | config->rev_checking_required_local_anchors = |
| 3735 | rev_checking_required_local_anchors_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3736 | config->version_min = min_version_; |
| 3737 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3738 | if (token_binding_enabled_) { |
| 3739 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3740 | } |
| 3741 | } |
| 3742 | |
| 3743 | protected: |
| 3744 | ~TestSSLConfigService() override {} |
| 3745 | |
| 3746 | private: |
| 3747 | const bool ev_enabled_; |
| 3748 | const bool online_rev_checking_; |
| 3749 | const bool rev_checking_required_local_anchors_; |
| 3750 | const bool token_binding_enabled_; |
| 3751 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3752 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3753 | }; |
| 3754 | |
| 3755 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3756 | #if !defined(OS_IOS) |
| 3757 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3758 | public: |
fdoray | 9f0b478 | 2017-02-10 12:55:47 | [diff] [blame] | 3759 | TokenBindingURLRequestTest() = default; |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3760 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3761 | void SetUp() override { |
| 3762 | default_context_.set_ssl_config_service( |
| 3763 | new TestSSLConfigService(false, false, false, true)); |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3764 | channel_id_service_.reset( |
| 3765 | new ChannelIDService(new DefaultChannelIDStore(NULL))); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3766 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3767 | URLRequestTestHTTP::SetUp(); |
| 3768 | } |
| 3769 | |
| 3770 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3771 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3772 | }; |
| 3773 | |
| 3774 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3775 | SpawnedTestServer::SSLOptions ssl_options; |
| 3776 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3777 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3778 | ssl_options, |
| 3779 | base::FilePath(kTestFilePath)); |
| 3780 | ASSERT_TRUE(https_test_server.Start()); |
| 3781 | |
| 3782 | TestDelegate d; |
| 3783 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3784 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3785 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d, |
| 3786 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3787 | r->Start(); |
| 3788 | EXPECT_TRUE(r->is_pending()); |
| 3789 | |
| 3790 | base::RunLoop().Run(); |
| 3791 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3792 | EXPECT_EQ(OK, d.request_status()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3793 | |
| 3794 | HttpRequestHeaders headers; |
| 3795 | std::string token_binding_header, token_binding_message; |
| 3796 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3797 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3798 | &token_binding_header)); |
| 3799 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3800 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3801 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3802 | std::vector<TokenBinding> token_bindings; |
| 3803 | ASSERT_TRUE( |
| 3804 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3805 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3806 | |
| 3807 | EXPECT_GT(d.bytes_received(), 0); |
| 3808 | std::string ekm = d.data_received(); |
| 3809 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3810 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3811 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3812 | token_bindings[0].signature, |
| 3813 | TokenBindingType::PROVIDED, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3814 | } |
| 3815 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3816 | |
| 3817 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3818 | SpawnedTestServer::SSLOptions ssl_options; |
| 3819 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3820 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3821 | ssl_options, |
| 3822 | base::FilePath(kTestFilePath)); |
| 3823 | ASSERT_TRUE(https_test_server.Start()); |
| 3824 | |
| 3825 | TestDelegate d; |
| 3826 | { |
| 3827 | GURL redirect_url = |
| 3828 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3829 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3830 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3831 | r->Start(); |
| 3832 | EXPECT_TRUE(r->is_pending()); |
| 3833 | |
| 3834 | base::RunLoop().Run(); |
| 3835 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3836 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3837 | |
| 3838 | HttpRequestHeaders headers; |
| 3839 | std::string token_binding_header, token_binding_message; |
| 3840 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3841 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3842 | &token_binding_header)); |
| 3843 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3844 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3845 | &token_binding_message)); |
| 3846 | std::vector<TokenBinding> token_bindings; |
| 3847 | ASSERT_TRUE( |
| 3848 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3849 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3850 | |
| 3851 | EXPECT_GT(d.bytes_received(), 0); |
| 3852 | std::string ekm = d.data_received(); |
| 3853 | |
| 3854 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3855 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3856 | token_bindings[0].signature, |
| 3857 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3858 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3859 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[1].ec_point, |
| 3860 | token_bindings[1].signature, |
| 3861 | TokenBindingType::REFERRED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3866 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3867 | #if !defined(OS_ANDROID) |
| 3868 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 3869 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, base::FilePath()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3870 | ASSERT_TRUE(http_server.Start()); |
| 3871 | SpawnedTestServer::SSLOptions ssl_options; |
| 3872 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3873 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3874 | ssl_options, |
| 3875 | base::FilePath(kTestFilePath)); |
| 3876 | ASSERT_TRUE(https_test_server.Start()); |
| 3877 | |
| 3878 | TestDelegate d; |
| 3879 | { |
| 3880 | GURL redirect_url = http_server.GetURL( |
| 3881 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3882 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3883 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3884 | r->Start(); |
| 3885 | EXPECT_TRUE(r->is_pending()); |
| 3886 | |
| 3887 | base::RunLoop().Run(); |
| 3888 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3889 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3890 | |
| 3891 | HttpRequestHeaders headers; |
| 3892 | std::string token_binding_header, token_binding_message; |
| 3893 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3894 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3895 | &token_binding_header)); |
| 3896 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3897 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3898 | &token_binding_message)); |
| 3899 | std::vector<TokenBinding> token_bindings; |
| 3900 | ASSERT_TRUE( |
| 3901 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3902 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3903 | |
| 3904 | EXPECT_GT(d.bytes_received(), 0); |
| 3905 | std::string ekm = d.data_received(); |
| 3906 | |
| 3907 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3908 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3909 | token_bindings[0].signature, |
| 3910 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 3915 | // 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] | 3916 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3917 | // the redirect. |
| 3918 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3919 | SpawnedTestServer::SSLOptions ssl_options; |
| 3920 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3921 | ssl_options, |
| 3922 | base::FilePath(kTestFilePath)); |
| 3923 | ASSERT_TRUE(https_test_server.Start()); |
| 3924 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3925 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3926 | ssl_options, |
| 3927 | base::FilePath(kTestFilePath)); |
| 3928 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3929 | |
| 3930 | TestDelegate d; |
| 3931 | { |
| 3932 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3933 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3934 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3935 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3936 | r->Start(); |
| 3937 | EXPECT_TRUE(r->is_pending()); |
| 3938 | |
| 3939 | base::RunLoop().Run(); |
| 3940 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3941 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3942 | |
| 3943 | HttpRequestHeaders headers; |
| 3944 | std::string token_binding_header, token_binding_message; |
| 3945 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3946 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3947 | &token_binding_header)); |
| 3948 | } |
| 3949 | } |
| 3950 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3951 | #endif // !defined(OS_IOS) |
| 3952 | |
| 3953 | // 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] | 3954 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3955 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3956 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3957 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3958 | http_test_server()->RegisterRequestHandler( |
| 3959 | base::Bind(&HandleRedirectConnect)); |
| 3960 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3961 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3962 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3963 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3964 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3965 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3966 | TestDelegate d; |
| 3967 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3968 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3969 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3970 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3971 | r->Start(); |
| 3972 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3973 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3974 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3975 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3976 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 3977 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3978 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3979 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3980 | // We should not have followed the redirect. |
| 3981 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3982 | } |
| 3983 | } |
| 3984 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3985 | // This is the same as the previous test, but checks that the network delegate |
| 3986 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3987 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3988 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3989 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3990 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3991 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3992 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3993 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3994 | TestDelegate d; |
| 3995 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3996 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3997 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3998 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3999 | r->Start(); |
| 4000 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4001 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4002 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4003 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4004 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4005 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4006 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4007 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4008 | // We should not have followed the redirect. |
| 4009 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4010 | |
| 4011 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4012 | EXPECT_THAT(network_delegate.last_error(), |
| 4013 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4017 | // Tests that we can block and asynchronously return OK in various stages. |
| 4018 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4019 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4020 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4021 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4022 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4023 | }; |
| 4024 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 4025 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4026 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4027 | |
| 4028 | TestDelegate d; |
| 4029 | BlockingNetworkDelegate network_delegate( |
| 4030 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4031 | network_delegate.set_block_on( |
| 4032 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4033 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4034 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4035 | |
| 4036 | TestURLRequestContext context(true); |
| 4037 | context.set_network_delegate(&network_delegate); |
| 4038 | context.Init(); |
| 4039 | |
| 4040 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4041 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4042 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4043 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4044 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4045 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4046 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4047 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4048 | EXPECT_EQ(blocking_stages[i], |
| 4049 | network_delegate.stage_blocked_for_callback()); |
| 4050 | network_delegate.DoCallback(OK); |
| 4051 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4052 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4053 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4054 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4055 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4056 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4057 | } |
| 4058 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4059 | } |
| 4060 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4061 | // Tests that the network delegate can block and cancel a request. |
| 4062 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4063 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4064 | |
| 4065 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4066 | BlockingNetworkDelegate network_delegate( |
| 4067 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4068 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4069 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4070 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4071 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4072 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4073 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4074 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4075 | std::unique_ptr<URLRequest> r( |
| 4076 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4077 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4078 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4079 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4080 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4081 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4082 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4083 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4084 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4085 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4086 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4087 | } |
| 4088 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4089 | } |
| 4090 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4091 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4092 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4093 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4094 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4095 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4096 | BlockingNetworkDelegate::Stage stage, |
| 4097 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4098 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4099 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4100 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4101 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4102 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4103 | TestURLRequestContext context(true); |
| 4104 | context.set_network_delegate(&network_delegate); |
| 4105 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4106 | |
| 4107 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4108 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4109 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4110 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4111 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4112 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4113 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4114 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4115 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4116 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4117 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4118 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4119 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4120 | } else { |
| 4121 | NOTREACHED(); |
| 4122 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4123 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4124 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4125 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4126 | } |
| 4127 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4128 | } |
| 4129 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4130 | // The following 3 tests check that the network delegate can cancel a request |
| 4131 | // synchronously in various stages of the request. |
| 4132 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4133 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4134 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4135 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4136 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4140 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4141 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4142 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4143 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4147 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4148 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4149 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4150 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | // The following 3 tests check that the network delegate can cancel a request |
| 4154 | // asynchronously in various stages of the request. |
| 4155 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4156 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4157 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4158 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4159 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4163 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4164 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4165 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4166 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4170 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4171 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4172 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4173 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4174 | } |
| 4175 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4176 | // Tests that the network delegate can block and redirect a request to a new |
| 4177 | // URL. |
| 4178 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4179 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4180 | |
| 4181 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4182 | BlockingNetworkDelegate network_delegate( |
| 4183 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4184 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4185 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4186 | network_delegate.set_redirect_url(redirect_url); |
| 4187 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4188 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4189 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4190 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4191 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4192 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4193 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4194 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4195 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4196 | // Quit after hitting the redirect, so can check the headers. |
| 4197 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4198 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4199 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4200 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4201 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4202 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4203 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4204 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4205 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4206 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4207 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4208 | |
| 4209 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4210 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4211 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4212 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4213 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4214 | http_test_server()->host_port_pair()), |
| 4215 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4216 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4217 | // through an untunneled proxy. |
| 4218 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4219 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4220 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4221 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4222 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4223 | EXPECT_EQ(redirect_url, r->url()); |
| 4224 | EXPECT_EQ(original_url, r->original_url()); |
| 4225 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4226 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4227 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4228 | } |
| 4229 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4230 | } |
| 4231 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4232 | // Tests that the network delegate can block and redirect a request to a new |
| 4233 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4234 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4235 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4236 | |
| 4237 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4238 | BlockingNetworkDelegate network_delegate( |
| 4239 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4240 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4241 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4242 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4243 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4244 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4245 | |
| 4246 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4247 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4248 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4249 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4250 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4251 | // Quit after hitting the redirect, so can check the headers. |
| 4252 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4253 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4254 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4255 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4256 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4257 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4258 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4259 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4260 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4261 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4262 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4263 | |
| 4264 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4265 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4266 | base::RunLoop().Run(); |
| 4267 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4268 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4269 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4270 | http_test_server()->host_port_pair()), |
| 4271 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4272 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4273 | // through an untunneled proxy. |
| 4274 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4275 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4276 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4277 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4278 | EXPECT_EQ(redirect_url, r->url()); |
| 4279 | EXPECT_EQ(original_url, r->original_url()); |
| 4280 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4281 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4282 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4283 | } |
| 4284 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4285 | } |
| 4286 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4287 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4288 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4289 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4290 | |
| 4291 | const char kData[] = "hello world"; |
| 4292 | |
| 4293 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4294 | BlockingNetworkDelegate network_delegate( |
| 4295 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4296 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4297 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4298 | network_delegate.set_redirect_url(redirect_url); |
| 4299 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4300 | TestURLRequestContext context(true); |
| 4301 | context.set_network_delegate(&network_delegate); |
| 4302 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4303 | |
| 4304 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4305 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4306 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4307 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4308 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4309 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4310 | HttpRequestHeaders headers; |
| 4311 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 4312 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4313 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4314 | |
| 4315 | // Quit after hitting the redirect, so can check the headers. |
| 4316 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4317 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4318 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4319 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4320 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4321 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4322 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4323 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4324 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4325 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4326 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4327 | |
| 4328 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4329 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4330 | base::RunLoop().Run(); |
| 4331 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4332 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4333 | EXPECT_EQ(redirect_url, r->url()); |
| 4334 | EXPECT_EQ(original_url, r->original_url()); |
| 4335 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4336 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4337 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4338 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4339 | EXPECT_EQ(kData, d.data_received()); |
| 4340 | } |
| 4341 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4342 | } |
| 4343 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4344 | // Tests that the network delegate can block and redirect a request to a new |
| 4345 | // URL during OnHeadersReceived. |
| 4346 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4347 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4348 | |
| 4349 | TestDelegate d; |
| 4350 | BlockingNetworkDelegate network_delegate( |
| 4351 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4352 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4353 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4354 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4355 | |
| 4356 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4357 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4358 | |
| 4359 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4360 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4361 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4362 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4363 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4364 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4365 | base::RunLoop().Run(); |
| 4366 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4367 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4368 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4369 | http_test_server()->host_port_pair()), |
| 4370 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4371 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4372 | // through an untunneled proxy. |
| 4373 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4374 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4375 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4376 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4377 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4378 | EXPECT_EQ(redirect_url, r->url()); |
| 4379 | EXPECT_EQ(original_url, r->original_url()); |
| 4380 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4381 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4382 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4383 | } |
| 4384 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4385 | } |
| 4386 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4387 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4388 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4389 | // handle the challenge, and is passing the buck along to the |
| 4390 | // URLRequest::Delegate. |
| 4391 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4392 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4393 | |
| 4394 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4395 | BlockingNetworkDelegate network_delegate( |
| 4396 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4397 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4398 | TestURLRequestContext context(true); |
| 4399 | context.set_network_delegate(&network_delegate); |
| 4400 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4401 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4402 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4403 | |
| 4404 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4405 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4406 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4407 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4408 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4409 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4410 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4411 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4412 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4413 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4414 | EXPECT_TRUE(d.auth_required_called()); |
| 4415 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4416 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4417 | } |
| 4418 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4419 | } |
| 4420 | |
| 4421 | TEST_F(URLRequestTestHTTP, |
| 4422 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4423 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4424 | |
| 4425 | TestDelegate d; |
| 4426 | BlockingNetworkDelegate network_delegate( |
| 4427 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4428 | |
| 4429 | TestURLRequestContext context(true); |
| 4430 | context.set_network_delegate(&network_delegate); |
| 4431 | context.Init(); |
| 4432 | |
| 4433 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4434 | |
| 4435 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4436 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4437 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4438 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4439 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4440 | |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4441 | base::RunLoop().Run(); |
| 4442 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4443 | { |
| 4444 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4445 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4446 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4447 | } |
| 4448 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4449 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4450 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4451 | EXPECT_TRUE(d.auth_required_called()); |
| 4452 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4453 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4454 | } |
| 4455 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4456 | } |
| 4457 | |
| 4458 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4459 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4460 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4462 | |
| 4463 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4464 | BlockingNetworkDelegate network_delegate( |
| 4465 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4466 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4467 | network_delegate.set_auth_retval( |
| 4468 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4469 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4470 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4471 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4472 | TestURLRequestContext context(true); |
| 4473 | context.set_network_delegate(&network_delegate); |
| 4474 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4475 | |
| 4476 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4477 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4478 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4479 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4480 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4481 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4482 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4483 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4484 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4485 | EXPECT_FALSE(d.auth_required_called()); |
| 4486 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4487 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4488 | } |
| 4489 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4490 | } |
| 4491 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4492 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4493 | // headers (for the first or second request) when called at the proper times. |
| 4494 | TEST_F(URLRequestTestHTTP, |
| 4495 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4496 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4497 | |
| 4498 | TestDelegate d; |
| 4499 | BlockingNetworkDelegate network_delegate( |
| 4500 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4501 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4502 | network_delegate.set_auth_retval( |
| 4503 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4504 | |
| 4505 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4506 | |
| 4507 | TestURLRequestContext context(true); |
| 4508 | context.set_network_delegate(&network_delegate); |
| 4509 | context.Init(); |
| 4510 | |
| 4511 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4512 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4513 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4514 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4515 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4516 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4517 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4518 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4519 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4520 | EXPECT_FALSE(d.auth_required_called()); |
| 4521 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4522 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4523 | |
| 4524 | { |
| 4525 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4526 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4527 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4528 | } |
| 4529 | } |
| 4530 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4531 | } |
| 4532 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4533 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4534 | // by cancelling authentication. |
| 4535 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4536 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4537 | |
| 4538 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4539 | BlockingNetworkDelegate network_delegate( |
| 4540 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4541 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4542 | network_delegate.set_auth_retval( |
| 4543 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4544 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4545 | TestURLRequestContext context(true); |
| 4546 | context.set_network_delegate(&network_delegate); |
| 4547 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4548 | |
| 4549 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4550 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4551 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4552 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4553 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4554 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4555 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4556 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4557 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4558 | EXPECT_FALSE(d.auth_required_called()); |
| 4559 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4560 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4561 | } |
| 4562 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4563 | } |
| 4564 | |
| 4565 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4566 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4567 | // to handle the challenge, and is passing the buck along to the |
| 4568 | // URLRequest::Delegate. |
| 4569 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4570 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4571 | |
| 4572 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4573 | BlockingNetworkDelegate network_delegate( |
| 4574 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4575 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4576 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4577 | TestURLRequestContext context(true); |
| 4578 | context.set_network_delegate(&network_delegate); |
| 4579 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4580 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4581 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4582 | |
| 4583 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4584 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4585 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4586 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4587 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4588 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4589 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4590 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4591 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4592 | EXPECT_TRUE(d.auth_required_called()); |
| 4593 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4594 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4595 | } |
| 4596 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4597 | } |
| 4598 | |
| 4599 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4600 | // by setting credentials. |
| 4601 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4603 | |
| 4604 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4605 | BlockingNetworkDelegate network_delegate( |
| 4606 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4607 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4608 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4609 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4610 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4611 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4612 | network_delegate.set_auth_credentials(auth_credentials); |
| 4613 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4614 | TestURLRequestContext context(true); |
| 4615 | context.set_network_delegate(&network_delegate); |
| 4616 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4617 | |
| 4618 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4619 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4620 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4621 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4622 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4623 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4624 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4625 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4626 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4627 | EXPECT_FALSE(d.auth_required_called()); |
| 4628 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4629 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4630 | } |
| 4631 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4632 | } |
| 4633 | |
| 4634 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4635 | // by cancelling authentication. |
| 4636 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4637 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4638 | |
| 4639 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4640 | BlockingNetworkDelegate network_delegate( |
| 4641 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4642 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4643 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4644 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4645 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4646 | TestURLRequestContext context(true); |
| 4647 | context.set_network_delegate(&network_delegate); |
| 4648 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4649 | |
| 4650 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4651 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4652 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4653 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4654 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4655 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4656 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4657 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4658 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4659 | EXPECT_FALSE(d.auth_required_called()); |
| 4660 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4661 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4662 | } |
| 4663 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4664 | } |
| 4665 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4666 | // Tests that we can handle when a network request was canceled while we were |
| 4667 | // waiting for the network delegate. |
| 4668 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4669 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4670 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4671 | |
| 4672 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4673 | BlockingNetworkDelegate network_delegate( |
| 4674 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4675 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4676 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4677 | TestURLRequestContext context(true); |
| 4678 | context.set_network_delegate(&network_delegate); |
| 4679 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4680 | |
| 4681 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4682 | std::unique_ptr<URLRequest> r( |
| 4683 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4684 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4685 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4686 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4687 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4688 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4689 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4690 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4691 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4692 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4693 | // Ensure that network delegate is notified. |
| 4694 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4695 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4696 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4697 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4698 | } |
| 4699 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4700 | } |
| 4701 | |
| 4702 | // Tests that we can handle when a network request was canceled while we were |
| 4703 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4704 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4705 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4706 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4707 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4708 | |
| 4709 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4710 | BlockingNetworkDelegate network_delegate( |
| 4711 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4712 | network_delegate.set_block_on( |
| 4713 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4714 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4715 | TestURLRequestContext context(true); |
| 4716 | context.set_network_delegate(&network_delegate); |
| 4717 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4718 | |
| 4719 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4720 | std::unique_ptr<URLRequest> r( |
| 4721 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4722 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4723 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4724 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4725 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4726 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4727 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4728 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4729 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4730 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4731 | // Ensure that network delegate is notified. |
| 4732 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4733 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4734 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4735 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4736 | } |
| 4737 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4738 | } |
| 4739 | |
| 4740 | // Tests that we can handle when a network request was canceled while we were |
| 4741 | // waiting for the network delegate. |
| 4742 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4743 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4744 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4745 | |
| 4746 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4747 | BlockingNetworkDelegate network_delegate( |
| 4748 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4749 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4750 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4751 | TestURLRequestContext context(true); |
| 4752 | context.set_network_delegate(&network_delegate); |
| 4753 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4754 | |
| 4755 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4756 | std::unique_ptr<URLRequest> r( |
| 4757 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4758 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4760 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4761 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4762 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4763 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4764 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4765 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4766 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4767 | // Ensure that network delegate is notified. |
| 4768 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4769 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4770 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4771 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4772 | } |
| 4773 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4774 | } |
| 4775 | |
| 4776 | // Tests that we can handle when a network request was canceled while we were |
| 4777 | // waiting for the network delegate. |
| 4778 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4779 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4780 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4781 | |
| 4782 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4783 | BlockingNetworkDelegate network_delegate( |
| 4784 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4785 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4786 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4787 | TestURLRequestContext context(true); |
| 4788 | context.set_network_delegate(&network_delegate); |
| 4789 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4790 | |
| 4791 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4792 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4793 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 4794 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4795 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4796 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4797 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4798 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4799 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4800 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4801 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4802 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4803 | // Ensure that network delegate is notified. |
| 4804 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4805 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4806 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4807 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4808 | } |
| 4809 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4810 | } |
| 4811 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4812 | namespace { |
| 4813 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4814 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4815 | const test_server::HttpRequest& request) { |
| 4816 | if (request.headers.find("Host") == request.headers.end() || |
| 4817 | request.headers.at("Host") != "www.server-auth.com" || |
| 4818 | request.method != test_server::METHOD_CONNECT) { |
| 4819 | return nullptr; |
| 4820 | } |
| 4821 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4822 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4823 | new test_server::BasicHttpResponse); |
| 4824 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4825 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4826 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4827 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | } // namespace |
| 4831 | |
| 4832 | // 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] | 4833 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4834 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4835 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4836 | http_test_server()->RegisterRequestHandler( |
| 4837 | base::Bind(&HandleServerAuthConnect)); |
| 4838 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4839 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4840 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4841 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4842 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4843 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4844 | TestDelegate d; |
| 4845 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4846 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4847 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 4848 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4849 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4850 | r->Start(); |
| 4851 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4852 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4853 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4854 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4855 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4856 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4857 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4861 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4862 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4863 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4864 | TestDelegate d; |
| 4865 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4866 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4867 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4868 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4869 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4870 | r->Start(); |
| 4871 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4872 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4873 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4874 | |
| 4875 | EXPECT_EQ(1, d.response_started_count()); |
| 4876 | EXPECT_FALSE(d.received_data_before_response()); |
| 4877 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4878 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4879 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4880 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4881 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4882 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4883 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4884 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4885 | } |
| 4886 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4887 | // This test has the server send a large number of cookies to the client. |
| 4888 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4889 | // search is used to estimate that maximum number of cookies that are accepted |
| 4890 | // by the browser. Beyond the maximum number, the request will fail with |
| 4891 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4892 | #if defined(OS_WIN) |
| 4893 | // http://crbug.com/177916 |
| 4894 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4895 | #else |
| 4896 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4897 | #endif // defined(OS_WIN) |
| 4898 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4899 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4900 | |
| 4901 | int lower_bound = 0; |
| 4902 | int upper_bound = 1; |
| 4903 | |
| 4904 | // Double the number of cookies until the response header limits are |
| 4905 | // exceeded. |
| 4906 | while (DoManyCookiesRequest(upper_bound)) { |
| 4907 | lower_bound = upper_bound; |
| 4908 | upper_bound *= 2; |
| 4909 | ASSERT_LT(upper_bound, 1000000); |
| 4910 | } |
| 4911 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4912 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4913 | if (tolerance < 2) |
| 4914 | tolerance = 2; |
| 4915 | |
| 4916 | // Perform a binary search to find the highest possible number of cookies, |
| 4917 | // within the desired tolerance. |
| 4918 | while (upper_bound - lower_bound >= tolerance) { |
| 4919 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4920 | |
| 4921 | if (DoManyCookiesRequest(num_cookies)) |
| 4922 | lower_bound = num_cookies; |
| 4923 | else |
| 4924 | upper_bound = num_cookies; |
| 4925 | } |
| 4926 | // Success: the test did not crash. |
| 4927 | } |
| 4928 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4929 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4930 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4931 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4932 | TestDelegate d; |
| 4933 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4934 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4935 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4936 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4937 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4938 | r->Start(); |
| 4939 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4940 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4941 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4942 | |
| 4943 | EXPECT_EQ(1, d.response_started_count()); |
| 4944 | EXPECT_FALSE(d.received_data_before_response()); |
| 4945 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4946 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4947 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4948 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4949 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4950 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4951 | } |
| 4952 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4953 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4954 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4955 | |
| 4956 | TestDelegate d; |
| 4957 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4958 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4959 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4960 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4961 | |
| 4962 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4963 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4964 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4965 | r->Start(); |
| 4966 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4967 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4968 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4969 | |
| 4970 | EXPECT_EQ(1, d.response_started_count()); |
| 4971 | EXPECT_FALSE(d.received_data_before_response()); |
| 4972 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4973 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4974 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4975 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4976 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4977 | |
| 4978 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4979 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4980 | } |
| 4981 | } |
| 4982 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4983 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4984 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4985 | |
| 4986 | TestDelegate d; |
| 4987 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4988 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4989 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4990 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4991 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4992 | r->Start(); |
| 4993 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4994 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4995 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4996 | |
| 4997 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4998 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4999 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5000 | |
| 5001 | EXPECT_EQ(1, d.response_started_count()); |
| 5002 | EXPECT_FALSE(d.received_data_before_response()); |
| 5003 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5004 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5005 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5006 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5007 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5008 | } |
| 5009 | } |
| 5010 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5011 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5012 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5013 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5014 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5015 | base::FilePath(kTestFilePath)); |
| 5016 | |
| 5017 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5018 | |
| 5019 | // Parameter that specifies the Content-Length field in the response: |
| 5020 | // C - Compressed length. |
| 5021 | // U - Uncompressed length. |
| 5022 | // L - Large length (larger than both C & U). |
| 5023 | // M - Medium length (between C & U). |
| 5024 | // S - Small length (smaller than both C & U). |
| 5025 | const char test_parameters[] = "CULMS"; |
| 5026 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 5027 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5028 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5029 | // S has too little data, but we seem to accept it. |
| 5030 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5031 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5032 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5033 | base::FilePath file_path; |
| 5034 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 5035 | file_path = file_path.Append(kTestFilePath); |
| 5036 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5037 | std::string expected_content; |
| 5038 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5039 | |
| 5040 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5041 | TestDelegate d; |
| 5042 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5043 | std::string test_file = base::StringPrintf( |
| 5044 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5045 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5046 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5047 | TestURLRequestContext context(true); |
| 5048 | context.set_network_delegate(&network_delegate); |
| 5049 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5050 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5051 | std::unique_ptr<URLRequest> r( |
| 5052 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5053 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5054 | r->Start(); |
| 5055 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5056 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5057 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5058 | |
| 5059 | EXPECT_EQ(1, d.response_started_count()); |
| 5060 | EXPECT_FALSE(d.received_data_before_response()); |
| 5061 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5062 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5063 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5064 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5065 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5066 | if (test_parameters[i] == 'S') { |
| 5067 | // When content length is smaller than both compressed length and |
| 5068 | // uncompressed length, HttpStreamParser might not read the full |
| 5069 | // response body. |
| 5070 | continue; |
| 5071 | } |
| 5072 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5073 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5074 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5075 | << " Parameter = \"" << test_file << "\""; |
| 5076 | } |
| 5077 | } |
| 5078 | } |
| 5079 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5080 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5081 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5082 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5083 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5084 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5085 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5086 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5087 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5088 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5089 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5090 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5091 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5092 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5093 | |
| 5094 | EXPECT_EQ(1, d.response_started_count()); |
| 5095 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5096 | EXPECT_EQ(destination_url, req->url()); |
| 5097 | EXPECT_EQ(original_url, req->original_url()); |
| 5098 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5099 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5100 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5101 | |
| 5102 | LoadTimingInfo load_timing_info_before_redirect; |
| 5103 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5104 | &load_timing_info_before_redirect)); |
| 5105 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5106 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5107 | |
| 5108 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5109 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5110 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5111 | |
| 5112 | // Check that a new socket was used on redirect, since the server does not |
| 5113 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5114 | // before the ones recorded for the second request. |
| 5115 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5116 | load_timing_info.socket_log_id); |
| 5117 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5118 | load_timing_info.connect_timing.connect_start); |
| 5119 | } |
| 5120 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5121 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5122 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5123 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5124 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5125 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5126 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5127 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5128 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5129 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5130 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5131 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5132 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5133 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5134 | |
| 5135 | EXPECT_EQ(1, d.response_started_count()); |
| 5136 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5137 | EXPECT_EQ(destination_url, req->url()); |
| 5138 | EXPECT_EQ(original_url, req->original_url()); |
| 5139 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5140 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5141 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5142 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5143 | } |
| 5144 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5145 | // First and second pieces of information logged by delegates to URLRequests. |
| 5146 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5147 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5148 | |
| 5149 | // Logs delegate information to a URLRequest. The first string is logged |
| 5150 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5151 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5152 | // another asynchronous call is used to clear the delegate information |
| 5153 | // before calling a callback. The object then deletes itself. |
| 5154 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5155 | public: |
| 5156 | typedef base::Callback<void()> Callback; |
| 5157 | |
| 5158 | // Each time delegate information is added to the URLRequest, the resulting |
| 5159 | // load state is checked. The expected load state after each request is |
| 5160 | // passed in as an argument. |
| 5161 | static void Run(URLRequest* url_request, |
| 5162 | LoadState expected_first_load_state, |
| 5163 | LoadState expected_second_load_state, |
| 5164 | LoadState expected_third_load_state, |
| 5165 | const Callback& callback) { |
| 5166 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 5167 | url_request, |
| 5168 | expected_first_load_state, |
| 5169 | expected_second_load_state, |
| 5170 | expected_third_load_state, |
| 5171 | callback); |
| 5172 | logger->Start(); |
| 5173 | } |
| 5174 | |
| 5175 | // Checks that the log entries, starting with log_position, contain the |
| 5176 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5177 | // recorded. Returns the index of entry after the expected number of |
| 5178 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5179 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5180 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5181 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5182 | if (log_position + 3 >= entries.size()) { |
| 5183 | ADD_FAILURE() << "Not enough log entries"; |
| 5184 | return entries.size(); |
| 5185 | } |
| 5186 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5187 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5188 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5189 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5190 | &delegate_info)); |
| 5191 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5192 | |
| 5193 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5194 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5195 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5196 | |
| 5197 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5198 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5199 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5200 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5201 | &delegate_info)); |
| 5202 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5203 | |
| 5204 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5205 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5206 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5207 | |
| 5208 | return log_position + 1; |
| 5209 | } |
| 5210 | |
| 5211 | private: |
| 5212 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5213 | |
| 5214 | AsyncDelegateLogger(URLRequest* url_request, |
| 5215 | LoadState expected_first_load_state, |
| 5216 | LoadState expected_second_load_state, |
| 5217 | LoadState expected_third_load_state, |
| 5218 | const Callback& callback) |
| 5219 | : url_request_(url_request), |
| 5220 | expected_first_load_state_(expected_first_load_state), |
| 5221 | expected_second_load_state_(expected_second_load_state), |
| 5222 | expected_third_load_state_(expected_third_load_state), |
| 5223 | callback_(callback) { |
| 5224 | } |
| 5225 | |
| 5226 | ~AsyncDelegateLogger() {} |
| 5227 | |
| 5228 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5229 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5230 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5231 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5232 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5233 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5234 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5235 | } |
| 5236 | |
| 5237 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5238 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5239 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5240 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5241 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5242 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5243 | } else { |
| 5244 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5245 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5246 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5247 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5248 | } |
| 5249 | |
| 5250 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5251 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5252 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5253 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5254 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5255 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5256 | callback_.Run(); |
| 5257 | } |
| 5258 | |
| 5259 | URLRequest* url_request_; |
| 5260 | const int expected_first_load_state_; |
| 5261 | const int expected_second_load_state_; |
| 5262 | const int expected_third_load_state_; |
| 5263 | const Callback callback_; |
| 5264 | |
| 5265 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5266 | }; |
| 5267 | |
| 5268 | // NetworkDelegate that logs delegate information before a request is started, |
| 5269 | // before headers are sent, when headers are read, and when auth information |
| 5270 | // is requested. Uses AsyncDelegateLogger. |
| 5271 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5272 | public: |
| 5273 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5274 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5275 | |
| 5276 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5277 | int OnBeforeURLRequest(URLRequest* request, |
| 5278 | const CompletionCallback& callback, |
| 5279 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5280 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 5281 | return RunCallbackAsynchronously(request, callback); |
| 5282 | } |
| 5283 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5284 | int OnBeforeStartTransaction(URLRequest* request, |
| 5285 | const CompletionCallback& callback, |
| 5286 | HttpRequestHeaders* headers) override { |
| 5287 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5288 | return RunCallbackAsynchronously(request, callback); |
| 5289 | } |
| 5290 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5291 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5292 | URLRequest* request, |
| 5293 | const CompletionCallback& callback, |
| 5294 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5295 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5296 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5297 | TestNetworkDelegate::OnHeadersReceived(request, |
| 5298 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5299 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5300 | override_response_headers, |
| 5301 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5302 | return RunCallbackAsynchronously(request, callback); |
| 5303 | } |
| 5304 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5305 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5306 | URLRequest* request, |
| 5307 | const AuthChallengeInfo& auth_info, |
| 5308 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5309 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5310 | AsyncDelegateLogger::Run( |
| 5311 | request, |
| 5312 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5313 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5314 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5315 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5316 | callback, credentials)); |
| 5317 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5318 | } |
| 5319 | |
| 5320 | private: |
| 5321 | static int RunCallbackAsynchronously( |
| 5322 | URLRequest* request, |
| 5323 | const CompletionCallback& callback) { |
| 5324 | AsyncDelegateLogger::Run( |
| 5325 | request, |
| 5326 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5327 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5328 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5329 | base::Bind(callback, OK)); |
| 5330 | return ERR_IO_PENDING; |
| 5331 | } |
| 5332 | |
| 5333 | static void SetAuthAndResume(const AuthCallback& callback, |
| 5334 | AuthCredentials* credentials) { |
| 5335 | *credentials = AuthCredentials(kUser, kSecret); |
| 5336 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5337 | } |
| 5338 | |
| 5339 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5340 | }; |
| 5341 | |
| 5342 | // URLRequest::Delegate that logs delegate information when the headers |
| 5343 | // are received, when each read completes, and during redirects. Uses |
| 5344 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5345 | // |
| 5346 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5347 | // advancing to the next step in most cases, as well as cancellation. |
| 5348 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5349 | public: |
| 5350 | enum CancelStage { |
| 5351 | NO_CANCEL = 0, |
| 5352 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5353 | CANCEL_ON_RESPONSE_STARTED, |
| 5354 | CANCEL_ON_READ_COMPLETED |
| 5355 | }; |
| 5356 | |
| 5357 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5358 | : cancel_stage_(cancel_stage) { |
| 5359 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5360 | set_cancel_in_received_redirect(true); |
| 5361 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5362 | set_cancel_in_response_started(true); |
| 5363 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5364 | set_cancel_in_received_data(true); |
| 5365 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5366 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5367 | |
| 5368 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5369 | void OnReceivedRedirect(URLRequest* request, |
| 5370 | const RedirectInfo& redirect_info, |
| 5371 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5372 | *defer_redirect = true; |
| 5373 | AsyncDelegateLogger::Run( |
| 5374 | request, |
| 5375 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5376 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5377 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5378 | base::Bind( |
| 5379 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5380 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5381 | } |
| 5382 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5383 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5384 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5385 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5386 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5387 | base::Bind( |
| 5388 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5389 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5390 | } |
| 5391 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5392 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5393 | AsyncDelegateLogger::Run( |
| 5394 | request, |
| 5395 | LOAD_STATE_IDLE, |
| 5396 | LOAD_STATE_IDLE, |
| 5397 | LOAD_STATE_IDLE, |
| 5398 | base::Bind( |
| 5399 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5400 | base::Unretained(this), request, bytes_read)); |
| 5401 | } |
| 5402 | |
| 5403 | private: |
| 5404 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5405 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5406 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5407 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5408 | // FollowDeferredRedirect should not be called after cancellation. |
| 5409 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5410 | return; |
| 5411 | if (!defer_redirect) |
| 5412 | request->FollowDeferredRedirect(); |
| 5413 | } |
| 5414 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5415 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5416 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5417 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5418 | } |
| 5419 | |
| 5420 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5421 | // The parent class continues the request. |
| 5422 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5423 | } |
| 5424 | |
| 5425 | const CancelStage cancel_stage_; |
| 5426 | |
| 5427 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5428 | }; |
| 5429 | |
| 5430 | // Tests handling of delegate info before a request starts. |
| 5431 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5432 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5433 | |
| 5434 | TestDelegate request_delegate; |
| 5435 | TestURLRequestContext context(true); |
| 5436 | context.set_network_delegate(NULL); |
| 5437 | context.set_net_log(&net_log_); |
| 5438 | context.Init(); |
| 5439 | |
| 5440 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5441 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5442 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5443 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5444 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5445 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5446 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5447 | |
| 5448 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5449 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5450 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5451 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5452 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5453 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5454 | |
| 5455 | base::RunLoop().Run(); |
| 5456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5457 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5458 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5459 | } |
| 5460 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5461 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5462 | net_log_.GetEntries(&entries); |
| 5463 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5464 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5465 | |
| 5466 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5467 | |
| 5468 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5469 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5470 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5471 | } |
| 5472 | |
| 5473 | // Tests handling of delegate info from a network delegate. |
| 5474 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5475 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5476 | |
| 5477 | TestDelegate request_delegate; |
| 5478 | AsyncLoggingNetworkDelegate network_delegate; |
| 5479 | TestURLRequestContext context(true); |
| 5480 | context.set_network_delegate(&network_delegate); |
| 5481 | context.set_net_log(&net_log_); |
| 5482 | context.Init(); |
| 5483 | |
| 5484 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5485 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5486 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5487 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5488 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5489 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5490 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5491 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5492 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5493 | base::RunLoop().Run(); |
| 5494 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5495 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5496 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5497 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5498 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5499 | } |
| 5500 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5501 | |
| 5502 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5503 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5504 | net_log_.GetEntries(&entries); |
| 5505 | for (size_t i = 0; i < 3; ++i) { |
| 5506 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5507 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5508 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5509 | |
| 5510 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5511 | log_position + 1); |
| 5512 | |
| 5513 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5514 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5515 | entries[log_position].type); |
| 5516 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5517 | } |
| 5518 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5519 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5520 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | // Tests handling of delegate info from a network delegate in the case of an |
| 5524 | // HTTP redirect. |
| 5525 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5526 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5527 | |
| 5528 | TestDelegate request_delegate; |
| 5529 | AsyncLoggingNetworkDelegate network_delegate; |
| 5530 | TestURLRequestContext context(true); |
| 5531 | context.set_network_delegate(&network_delegate); |
| 5532 | context.set_net_log(&net_log_); |
| 5533 | context.Init(); |
| 5534 | |
| 5535 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5536 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5537 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5538 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5539 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5540 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5541 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5542 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5543 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5544 | base::RunLoop().Run(); |
| 5545 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5546 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5547 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5548 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5549 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5550 | } |
| 5551 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5552 | |
| 5553 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5554 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5555 | net_log_.GetEntries(&entries); |
| 5556 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5557 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5558 | for (size_t i = 0; i < 3; ++i) { |
| 5559 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5560 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5561 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5562 | |
| 5563 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5564 | log_position + 1); |
| 5565 | |
| 5566 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5567 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5568 | entries[log_position].type); |
| 5569 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5573 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5574 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5575 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5576 | |
| 5577 | // The NetworkDelegate logged information in the same three events as before. |
| 5578 | for (size_t i = 0; i < 3; ++i) { |
| 5579 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5580 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5581 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5582 | |
| 5583 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5584 | log_position + 1); |
| 5585 | |
| 5586 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5587 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5588 | entries[log_position].type); |
| 5589 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5590 | } |
| 5591 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5592 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5593 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5594 | } |
| 5595 | |
| 5596 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5597 | // AUTH. |
| 5598 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5599 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5600 | |
| 5601 | TestDelegate request_delegate; |
| 5602 | AsyncLoggingNetworkDelegate network_delegate; |
| 5603 | TestURLRequestContext context(true); |
| 5604 | context.set_network_delegate(&network_delegate); |
| 5605 | context.set_net_log(&net_log_); |
| 5606 | context.Init(); |
| 5607 | |
| 5608 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5609 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5610 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5611 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5612 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5613 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5614 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5615 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5616 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5617 | base::RunLoop().Run(); |
| 5618 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5619 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5620 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5621 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5622 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5623 | } |
| 5624 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5625 | |
| 5626 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5627 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5628 | net_log_.GetEntries(&entries); |
| 5629 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5630 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5631 | // in |
| 5632 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5633 | for (size_t i = 0; i < 6; ++i) { |
| 5634 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5635 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5636 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5637 | |
| 5638 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5639 | log_position + 1); |
| 5640 | |
| 5641 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5642 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5643 | entries[log_position].type); |
| 5644 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5645 | } |
| 5646 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5647 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5648 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5649 | } |
| 5650 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5651 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5652 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5653 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5654 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5655 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5656 | base::FilePath(kTestFilePath)); |
| 5657 | |
| 5658 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5659 | |
| 5660 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5661 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5662 | TestURLRequestContext context(true); |
| 5663 | context.set_network_delegate(NULL); |
| 5664 | context.set_net_log(&net_log_); |
| 5665 | context.Init(); |
| 5666 | |
| 5667 | { |
| 5668 | // A chunked response with delays between chunks is used to make sure that |
| 5669 | // attempts by the URLRequest delegate to log information while reading the |
| 5670 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5671 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5673 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5674 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5675 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5676 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5677 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5678 | base::RunLoop().Run(); |
| 5679 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5680 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5681 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5682 | } |
| 5683 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5684 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5685 | net_log_.GetEntries(&entries); |
| 5686 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5687 | size_t log_position = 0; |
| 5688 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5689 | // The delegate info should only have been logged on header complete. Other |
| 5690 | // times it should silently be ignored. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5691 | log_position = ExpectLogContainsSomewhereAfter( |
| 5692 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5693 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5694 | |
| 5695 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5696 | log_position + 1); |
| 5697 | |
| 5698 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5699 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5700 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5701 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5702 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5703 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5704 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5705 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5706 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5707 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5708 | |
| 5709 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5710 | // an HTTP redirect. |
| 5711 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5712 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5713 | |
| 5714 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5715 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5716 | TestURLRequestContext context(true); |
| 5717 | context.set_network_delegate(NULL); |
| 5718 | context.set_net_log(&net_log_); |
| 5719 | context.Init(); |
| 5720 | |
| 5721 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5722 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5723 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5724 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5725 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5726 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5727 | base::RunLoop().Run(); |
| 5728 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5729 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5730 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5731 | } |
| 5732 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5733 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5734 | net_log_.GetEntries(&entries); |
| 5735 | |
| 5736 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5737 | // OnResponseStarted. |
| 5738 | size_t log_position = 0; |
| 5739 | for (int i = 0; i < 2; ++i) { |
| 5740 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5741 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5742 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5743 | |
| 5744 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5745 | log_position + 1); |
| 5746 | |
| 5747 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5748 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5749 | entries[log_position].type); |
| 5750 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5751 | } |
| 5752 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5753 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5754 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5755 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5756 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5760 | // an HTTP redirect, with cancellation at various points. |
| 5761 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5762 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5763 | |
| 5764 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5765 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5766 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5767 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5768 | }; |
| 5769 | |
| 5770 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5771 | ++test_case) { |
| 5772 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5773 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 5774 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5775 | context.set_network_delegate(NULL); |
| 5776 | context.set_net_log(&net_log); |
| 5777 | context.Init(); |
| 5778 | |
| 5779 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5780 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5781 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5782 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5783 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5784 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5785 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5786 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5787 | } |
| 5788 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5789 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5790 | net_log.GetEntries(&entries); |
| 5791 | |
| 5792 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5793 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5794 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5795 | // still currently supported in that call. |
| 5796 | size_t log_position = 0; |
| 5797 | for (int i = 0; i < 2; ++i) { |
| 5798 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5799 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5800 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5801 | |
| 5802 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5803 | log_position + 1); |
| 5804 | |
| 5805 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5806 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5807 | entries[log_position].type); |
| 5808 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5809 | } |
| 5810 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5811 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5812 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5813 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5814 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5815 | } |
| 5816 | } |
| 5817 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5818 | namespace { |
| 5819 | |
| 5820 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5821 | const char kExtraValue[] = "fubar"; |
| 5822 | |
| 5823 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5824 | void OnReceivedRedirect(URLRequest* request, |
| 5825 | const RedirectInfo& redirect_info, |
| 5826 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5827 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5828 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5829 | } |
| 5830 | }; |
| 5831 | |
| 5832 | } // namespace |
| 5833 | |
| 5834 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5836 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5837 | GURL destination_url = |
| 5838 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5839 | GURL original_url = |
| 5840 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5841 | RedirectWithAdditionalHeadersDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5842 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5843 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5844 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5845 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5846 | |
| 5847 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5848 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5849 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5850 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5851 | EXPECT_FALSE(req->is_pending()); |
| 5852 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5853 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5854 | } |
| 5855 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5856 | namespace { |
| 5857 | |
| 5858 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5859 | |
| 5860 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5861 | void OnReceivedRedirect(URLRequest* request, |
| 5862 | const RedirectInfo& redirect_info, |
| 5863 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5864 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5865 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5866 | } |
| 5867 | }; |
| 5868 | |
| 5869 | } // namespace |
| 5870 | |
| 5871 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5872 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5873 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5874 | GURL destination_url = http_test_server()->GetURL( |
| 5875 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5876 | GURL original_url = |
| 5877 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5878 | RedirectWithHeaderRemovalDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5879 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5880 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5881 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5882 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5883 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5884 | |
| 5885 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5886 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5887 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5888 | EXPECT_FALSE(req->is_pending()); |
| 5889 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5890 | EXPECT_EQ("None", d.data_received()); |
| 5891 | } |
| 5892 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5893 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5894 | TestDelegate d; |
| 5895 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5896 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5897 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 5898 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5899 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5900 | r->Start(); |
| 5901 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5902 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5903 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5904 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5905 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5906 | |
| 5907 | // We expect to receive OnResponseStarted even though the request has been |
| 5908 | // cancelled. |
| 5909 | EXPECT_EQ(1, d.response_started_count()); |
| 5910 | EXPECT_EQ(0, d.bytes_received()); |
| 5911 | EXPECT_FALSE(d.received_data_before_response()); |
| 5912 | } |
| 5913 | } |
| 5914 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5915 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5916 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5917 | |
| 5918 | TestDelegate d; |
| 5919 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5920 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5921 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5922 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5923 | |
| 5924 | d.set_cancel_in_response_started(true); |
| 5925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5926 | r->Start(); |
| 5927 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5928 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5929 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5930 | |
| 5931 | EXPECT_EQ(1, d.response_started_count()); |
| 5932 | EXPECT_EQ(0, d.bytes_received()); |
| 5933 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5934 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5935 | } |
| 5936 | } |
| 5937 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5938 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5939 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5940 | |
| 5941 | TestDelegate d; |
| 5942 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5943 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5944 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5945 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5946 | |
| 5947 | d.set_cancel_in_received_data(true); |
| 5948 | |
| 5949 | r->Start(); |
| 5950 | EXPECT_TRUE(r->is_pending()); |
| 5951 | |
| 5952 | base::RunLoop().Run(); |
| 5953 | |
| 5954 | EXPECT_EQ(1, d.response_started_count()); |
| 5955 | EXPECT_NE(0, d.received_bytes_count()); |
| 5956 | EXPECT_FALSE(d.received_data_before_response()); |
| 5957 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 5962 | ASSERT_TRUE(http_test_server()->Start()); |
| 5963 | |
| 5964 | TestDelegate d; |
| 5965 | { |
| 5966 | // This returns an empty response (With headers). |
| 5967 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5968 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5969 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5970 | |
| 5971 | d.set_cancel_in_received_data(true); |
| 5972 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5973 | r->Start(); |
| 5974 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5975 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5976 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5977 | |
| 5978 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5979 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5980 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5981 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5982 | } |
| 5983 | } |
| 5984 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5985 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5986 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5987 | |
| 5988 | TestDelegate d; |
| 5989 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5990 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5991 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5992 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5993 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5994 | r->Start(); |
| 5995 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5996 | |
| 5997 | // The request will be implicitly canceled when it is destroyed. The |
| 5998 | // test delegate must not post a quit message when this happens because |
| 5999 | // this test doesn't actually have a message loop. The quit message would |
| 6000 | // get put on this thread's message queue and the next test would exit |
| 6001 | // early, causing problems. |
| 6002 | d.set_quit_on_complete(false); |
| 6003 | } |
| 6004 | // expect things to just cleanup properly. |
| 6005 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6006 | // 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] | 6007 | // message loop |
| 6008 | EXPECT_FALSE(d.received_data_before_response()); |
| 6009 | EXPECT_EQ(0, d.bytes_received()); |
| 6010 | } |
| 6011 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6012 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6013 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6014 | |
| 6015 | // populate cache |
| 6016 | { |
| 6017 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6018 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6019 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6020 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6021 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6022 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6023 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6024 | } |
| 6025 | |
| 6026 | // cancel read from cache (see bug 990242) |
| 6027 | { |
| 6028 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6029 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6030 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6032 | r->Start(); |
| 6033 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6034 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6035 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6036 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6037 | EXPECT_EQ(1, d.response_started_count()); |
| 6038 | EXPECT_EQ(0, d.bytes_received()); |
| 6039 | EXPECT_FALSE(d.received_data_before_response()); |
| 6040 | } |
| 6041 | } |
| 6042 | |
| 6043 | TEST_F(URLRequestTestHTTP, PostTest) { |
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 | HTTPUploadDataOperationTest("POST"); |
| 6046 | } |
| 6047 | |
| 6048 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6049 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6050 | HTTPUploadDataOperationTest("PUT"); |
| 6051 | } |
| 6052 | |
| 6053 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6054 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6055 | |
| 6056 | TestDelegate d; |
| 6057 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6058 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6059 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6060 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6061 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6063 | r->Start(); |
| 6064 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6065 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6066 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6067 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6068 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6069 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6070 | |
| 6071 | EXPECT_FALSE(d.received_data_before_response()); |
| 6072 | EXPECT_TRUE(d.data_received().empty()); |
| 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6077 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6078 | |
| 6079 | TestDelegate d; |
| 6080 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6081 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6082 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6083 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6084 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6085 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6086 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6087 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6088 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6089 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6090 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6091 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6092 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6093 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6094 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6095 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame^] | 6096 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6097 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6098 | std::numeric_limits<uint64_t>::max(), base::Time())); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6099 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 6100 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6101 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6102 | r->Start(); |
| 6103 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6104 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6105 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6106 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6107 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6108 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6109 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6110 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6111 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6112 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6113 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6114 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6115 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6116 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6117 | |
| 6118 | EXPECT_FALSE(d.received_data_before_response()); |
| 6119 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6120 | EXPECT_EQ(size, d.bytes_received()); |
| 6121 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6125 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6126 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6127 | |
| 6128 | TestDelegate d; |
| 6129 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6130 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6131 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6132 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6133 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6134 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6135 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6136 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame^] | 6137 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6138 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6139 | base::FilePath(FILE_PATH_LITERAL( |
| 6140 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6141 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6142 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 6143 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6144 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6145 | r->Start(); |
| 6146 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6147 | |
| 6148 | base::RunLoop().Run(); |
| 6149 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6150 | EXPECT_TRUE(d.request_failed()); |
| 6151 | EXPECT_FALSE(d.received_data_before_response()); |
| 6152 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6153 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6154 | } |
| 6155 | } |
| 6156 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6157 | namespace { |
| 6158 | |
| 6159 | // Adds a standard set of data to an upload for chunked upload integration |
| 6160 | // tests. |
| 6161 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6162 | writer->AppendData("a", 1, false); |
| 6163 | writer->AppendData("bcd", 3, false); |
| 6164 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6165 | writer->AppendData("\r\n\r\n", 4, false); |
| 6166 | writer->AppendData("0", 1, false); |
| 6167 | writer->AppendData("2323", 4, true); |
| 6168 | } |
| 6169 | |
| 6170 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6171 | // the server. |
| 6172 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6173 | // This should match the chunks sent by AddChunksToUpload(). |
| 6174 | const std::string expected_data = |
| 6175 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6176 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6177 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6178 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6179 | |
| 6180 | EXPECT_FALSE(d->received_data_before_response()); |
| 6181 | |
| 6182 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6183 | EXPECT_EQ(expected_data, d->data_received()); |
| 6184 | } |
| 6185 | |
| 6186 | } // namespace |
| 6187 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6188 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6189 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6190 | |
| 6191 | TestDelegate d; |
| 6192 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6193 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6194 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6195 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6196 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6197 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6198 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6199 | upload_data_stream->CreateWriter(); |
| 6200 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6201 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6202 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6203 | r->Start(); |
| 6204 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6205 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6206 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6207 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6208 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6212 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6213 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6214 | |
| 6215 | TestDelegate d; |
| 6216 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6217 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6218 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6219 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6220 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6221 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6222 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6223 | upload_data_stream->CreateWriter(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6224 | r->set_upload(base::WrapUnique(upload_data_stream.release())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6225 | r->set_method("POST"); |
| 6226 | r->Start(); |
| 6227 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6228 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6229 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6230 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6231 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6232 | } |
| 6233 | } |
| 6234 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6235 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6236 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6237 | |
| 6238 | TestDelegate d; |
| 6239 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6240 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6241 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6242 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6243 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6244 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6245 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6246 | upload_data_stream->CreateWriter(); |
| 6247 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6248 | r->set_method("POST"); |
| 6249 | r->Start(); |
| 6250 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6251 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6252 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6253 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6254 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6255 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6256 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6257 | } |
| 6258 | } |
| 6259 | |
| 6260 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6261 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6262 | |
| 6263 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6264 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6265 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6266 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6267 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6268 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6269 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6270 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6271 | |
| 6272 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6273 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6274 | |
| 6275 | std::string header; |
| 6276 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6277 | EXPECT_EQ("private", header); |
| 6278 | |
| 6279 | header.clear(); |
| 6280 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6281 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6282 | |
| 6283 | // The response has two "X-Multiple-Entries" headers. |
| 6284 | // This verfies our output has them concatenated together. |
| 6285 | header.clear(); |
| 6286 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6287 | EXPECT_EQ("a, b", header); |
| 6288 | } |
| 6289 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6290 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6291 | // security state. (see http://crbug.com/550977). |
| 6292 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6293 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6294 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6295 | https_test_server.SetSSLConfig( |
| 6296 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6297 | https_test_server.ServeFilesFromSourceDirectory( |
| 6298 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6299 | ASSERT_TRUE(https_test_server.Start()); |
| 6300 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6301 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6302 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6303 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6304 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6305 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6306 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6307 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6308 | |
| 6309 | TransportSecurityState* security_state = |
| 6310 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6311 | TransportSecurityState::STSState sts_state; |
| 6312 | TransportSecurityState::PKPState pkp_state; |
| 6313 | EXPECT_TRUE( |
| 6314 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6315 | EXPECT_FALSE( |
| 6316 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6317 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6318 | sts_state.upgrade_mode); |
| 6319 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6320 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6321 | #if defined(OS_ANDROID) |
| 6322 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6323 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6324 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6325 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6326 | } |
| 6327 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6328 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6329 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6330 | https_test_server.ServeFilesFromSourceDirectory( |
| 6331 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6332 | ASSERT_TRUE(https_test_server.Start()); |
| 6333 | // Make sure this test fails if the test server is changed to not |
| 6334 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6335 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6336 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6337 | |
| 6338 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6339 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6340 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6341 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6342 | request->Start(); |
| 6343 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6344 | TransportSecurityState* security_state = |
| 6345 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6346 | TransportSecurityState::STSState sts_state; |
| 6347 | EXPECT_FALSE( |
| 6348 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6349 | } |
| 6350 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6351 | namespace { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6352 | const char kExpectCTStaticHostname[] = "preloaded-expect-ct.badssl.com"; |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 6353 | const char kExpectStapleStaticHostname[] = "preloaded-expect-staple.badssl.com"; |
| 6354 | const char kExpectStapleReportURI[] = "https://report.badssl.com/expect-staple"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6355 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 6356 | } // namespace |
| 6357 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6358 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 6359 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6360 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6361 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6362 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6363 | https_test_server.SetSSLConfig( |
| 6364 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6365 | https_test_server.ServeFilesFromSourceDirectory( |
| 6366 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6367 | ASSERT_TRUE(https_test_server.Start()); |
| 6368 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6369 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6370 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6371 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6372 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6373 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6374 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6375 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6376 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6377 | TransportSecurityState* security_state = |
| 6378 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6379 | TransportSecurityState::STSState sts_state; |
| 6380 | TransportSecurityState::PKPState pkp_state; |
| 6381 | EXPECT_FALSE( |
| 6382 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6383 | EXPECT_TRUE( |
| 6384 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6385 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6386 | sts_state.upgrade_mode); |
| 6387 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6388 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6389 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6390 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6391 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6392 | } |
| 6393 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6394 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6395 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6396 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6397 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6398 | https_test_server.SetSSLConfig( |
| 6399 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6400 | https_test_server.ServeFilesFromSourceDirectory( |
| 6401 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6402 | ASSERT_TRUE(https_test_server.Start()); |
| 6403 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6404 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6405 | |
| 6406 | // Set up a pin for |test_server_hostname|. |
| 6407 | TransportSecurityState security_state; |
| 6408 | const base::Time current_time(base::Time::Now()); |
| 6409 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6410 | HashValueVector hashes; |
| 6411 | HashValue hash1; |
| 6412 | HashValue hash2; |
| 6413 | // The values here don't matter, as long as they are different from |
| 6414 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6415 | ASSERT_TRUE( |
| 6416 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6417 | ASSERT_TRUE( |
| 6418 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6419 | hashes.push_back(hash1); |
| 6420 | hashes.push_back(hash2); |
| 6421 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6422 | false, /* include subdomains */ |
| 6423 | hashes, report_uri); |
| 6424 | |
| 6425 | MockCertificateReportSender mock_report_sender; |
| 6426 | security_state.SetReportSender(&mock_report_sender); |
| 6427 | |
| 6428 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6429 | // set pin. |
| 6430 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6431 | ASSERT_TRUE(cert); |
| 6432 | |
| 6433 | MockCertVerifier cert_verifier; |
| 6434 | CertVerifyResult verify_result; |
| 6435 | verify_result.verified_cert = cert; |
| 6436 | verify_result.is_issued_by_known_root = true; |
| 6437 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6438 | ASSERT_TRUE( |
| 6439 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6440 | verify_result.public_key_hashes.push_back(hash3); |
| 6441 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6442 | |
| 6443 | TestNetworkDelegate network_delegate; |
| 6444 | TestURLRequestContext context(true); |
| 6445 | context.set_transport_security_state(&security_state); |
| 6446 | context.set_network_delegate(&network_delegate); |
| 6447 | context.set_cert_verifier(&cert_verifier); |
| 6448 | context.Init(); |
| 6449 | |
| 6450 | // Now send a request to trigger the violation. |
| 6451 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6452 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6453 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 6454 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6455 | violating_request->Start(); |
| 6456 | base::RunLoop().Run(); |
| 6457 | |
| 6458 | // Check that a report was sent. |
| 6459 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6460 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6461 | EXPECT_EQ("application/json; charset=utf-8", |
| 6462 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6463 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6464 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6465 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6466 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6467 | base::DictionaryValue* report_dict; |
| 6468 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6469 | std::string report_hostname; |
| 6470 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6471 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6472 | } |
| 6473 | |
| 6474 | // Tests that reports get sent on requests with |
| 6475 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6476 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6477 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6478 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6479 | https_test_server.SetSSLConfig( |
| 6480 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6481 | https_test_server.ServeFilesFromSourceDirectory( |
| 6482 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6483 | ASSERT_TRUE(https_test_server.Start()); |
| 6484 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6485 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6486 | |
| 6487 | TransportSecurityState security_state; |
| 6488 | MockCertificateReportSender mock_report_sender; |
| 6489 | security_state.SetReportSender(&mock_report_sender); |
| 6490 | |
| 6491 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6492 | // header. |
| 6493 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6494 | ASSERT_TRUE(cert); |
| 6495 | |
| 6496 | MockCertVerifier cert_verifier; |
| 6497 | CertVerifyResult verify_result; |
| 6498 | verify_result.verified_cert = cert; |
| 6499 | verify_result.is_issued_by_known_root = true; |
| 6500 | HashValue hash; |
| 6501 | // This value doesn't matter, as long as it is different from the pins |
| 6502 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6503 | ASSERT_TRUE( |
| 6504 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6505 | verify_result.public_key_hashes.push_back(hash); |
| 6506 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6507 | |
| 6508 | TestNetworkDelegate network_delegate; |
| 6509 | TestURLRequestContext context(true); |
| 6510 | context.set_transport_security_state(&security_state); |
| 6511 | context.set_network_delegate(&network_delegate); |
| 6512 | context.set_cert_verifier(&cert_verifier); |
| 6513 | context.Init(); |
| 6514 | |
| 6515 | // Now send a request to trigger the violation. |
| 6516 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6517 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6518 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6519 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6520 | violating_request->Start(); |
| 6521 | base::RunLoop().Run(); |
| 6522 | |
| 6523 | // Check that a report was sent. |
| 6524 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6525 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6526 | EXPECT_EQ("application/json; charset=utf-8", |
| 6527 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6528 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6529 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6530 | ASSERT_TRUE(value); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 6531 | ASSERT_TRUE(value->IsType(base::Value::Type::DICTIONARY)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6532 | base::DictionaryValue* report_dict; |
| 6533 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6534 | std::string report_hostname; |
| 6535 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6536 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6537 | } |
| 6538 | |
| 6539 | // Tests that reports do not get sent on requests with |
| 6540 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6541 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6542 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6543 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6544 | https_test_server.SetSSLConfig( |
| 6545 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6546 | https_test_server.ServeFilesFromSourceDirectory( |
| 6547 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6548 | ASSERT_TRUE(https_test_server.Start()); |
| 6549 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6550 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6551 | |
| 6552 | TransportSecurityState security_state; |
| 6553 | MockCertificateReportSender mock_report_sender; |
| 6554 | security_state.SetReportSender(&mock_report_sender); |
| 6555 | |
| 6556 | TestNetworkDelegate network_delegate; |
| 6557 | MockCertVerifier mock_cert_verifier; |
| 6558 | TestURLRequestContext context(true); |
| 6559 | context.set_transport_security_state(&security_state); |
| 6560 | context.set_network_delegate(&network_delegate); |
| 6561 | context.set_cert_verifier(&mock_cert_verifier); |
| 6562 | mock_cert_verifier.set_default_result(OK); |
| 6563 | context.Init(); |
| 6564 | |
| 6565 | // Now send a request that does not trigger the violation. |
| 6566 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6567 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6568 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6569 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6570 | request->Start(); |
| 6571 | base::RunLoop().Run(); |
| 6572 | |
| 6573 | // Check that a report was not sent. |
| 6574 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6575 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6576 | } |
| 6577 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6578 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6579 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6580 | https_test_server.ServeFilesFromSourceDirectory( |
| 6581 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6582 | ASSERT_TRUE(https_test_server.Start()); |
| 6583 | // Make sure this test fails if the test server is changed to not |
| 6584 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6585 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6586 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6587 | |
| 6588 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6589 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6590 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6591 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6592 | request->Start(); |
| 6593 | base::RunLoop().Run(); |
| 6594 | |
| 6595 | TransportSecurityState* security_state = |
| 6596 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6597 | TransportSecurityState::PKPState pkp_state; |
| 6598 | EXPECT_FALSE( |
| 6599 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6600 | } |
| 6601 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6602 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6603 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6604 | https_test_server.SetSSLConfig( |
| 6605 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6606 | https_test_server.ServeFilesFromSourceDirectory( |
| 6607 | base::FilePath(kTestFilePath)); |
| 6608 | ASSERT_TRUE(https_test_server.Start()); |
| 6609 | |
| 6610 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6611 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6612 | ASSERT_TRUE(cert); |
| 6613 | |
| 6614 | MockCertVerifier cert_verifier; |
| 6615 | CertVerifyResult verify_result; |
| 6616 | verify_result.verified_cert = cert; |
| 6617 | verify_result.is_issued_by_known_root = false; |
| 6618 | HashValue hash; |
| 6619 | ASSERT_TRUE( |
| 6620 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6621 | verify_result.public_key_hashes.push_back(hash); |
| 6622 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6623 | cert_verifier.set_default_result(OK); |
| 6624 | |
| 6625 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6626 | |
| 6627 | // Set up HPKP |
| 6628 | base::Time current_time = base::Time::Now(); |
| 6629 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6630 | HashValue pin; |
| 6631 | ASSERT_TRUE( |
| 6632 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6633 | HashValueVector hashes; |
| 6634 | hashes.push_back(pin); |
| 6635 | GURL report_uri(kHPKPReportUri); |
| 6636 | TransportSecurityState security_state; |
| 6637 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6638 | false, /* include subdomains */ |
| 6639 | hashes, report_uri); |
| 6640 | |
| 6641 | TestNetworkDelegate network_delegate; |
| 6642 | TestURLRequestContext context(true); |
| 6643 | context.set_transport_security_state(&security_state); |
| 6644 | context.set_network_delegate(&network_delegate); |
| 6645 | context.set_cert_verifier(&cert_verifier); |
| 6646 | context.Init(); |
| 6647 | |
| 6648 | TestDelegate d; |
| 6649 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6650 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6651 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6652 | request->Start(); |
| 6653 | base::RunLoop().Run(); |
| 6654 | |
| 6655 | TransportSecurityState::PKPState pkp_state; |
| 6656 | EXPECT_TRUE( |
| 6657 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6658 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6659 | } |
| 6660 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6661 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6662 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6663 | https_test_server.SetSSLConfig( |
| 6664 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6665 | https_test_server.ServeFilesFromSourceDirectory( |
| 6666 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6667 | ASSERT_TRUE(https_test_server.Start()); |
| 6668 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6669 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6670 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6671 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6672 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6673 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6674 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6675 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6676 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6677 | |
| 6678 | // We should have set parameters from the first header, not the second. |
| 6679 | TransportSecurityState* security_state = |
| 6680 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6681 | TransportSecurityState::STSState sts_state; |
| 6682 | EXPECT_TRUE( |
| 6683 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6684 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6685 | sts_state.upgrade_mode); |
| 6686 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6687 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6688 | } |
| 6689 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6690 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6691 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6692 | https_test_server.SetSSLConfig( |
| 6693 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6694 | https_test_server.ServeFilesFromSourceDirectory( |
| 6695 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6696 | ASSERT_TRUE(https_test_server.Start()); |
| 6697 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6698 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6699 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6700 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6701 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6702 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6703 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6704 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6705 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6706 | |
| 6707 | // We should have set parameters from the first header, not the second. |
| 6708 | TransportSecurityState* security_state = |
| 6709 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6710 | TransportSecurityState::STSState sts_state; |
| 6711 | TransportSecurityState::PKPState pkp_state; |
| 6712 | EXPECT_TRUE( |
| 6713 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6714 | EXPECT_TRUE( |
| 6715 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6716 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6717 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6718 | #if defined(OS_ANDROID) |
| 6719 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6720 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6721 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6722 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6723 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6724 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6725 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6726 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6727 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6728 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6729 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6730 | } |
| 6731 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6732 | // Tests that when multiple HPKP headers are present, asserting different |
| 6733 | // policies, that only the first such policy is processed. |
| 6734 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6735 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6736 | https_test_server.SetSSLConfig( |
| 6737 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6738 | https_test_server.ServeFilesFromSourceDirectory( |
| 6739 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6740 | ASSERT_TRUE(https_test_server.Start()); |
| 6741 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6742 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6743 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6744 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6745 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6746 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6747 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6748 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6749 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6750 | |
| 6751 | TransportSecurityState* security_state = |
| 6752 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6753 | TransportSecurityState::STSState sts_state; |
| 6754 | TransportSecurityState::PKPState pkp_state; |
| 6755 | EXPECT_TRUE( |
| 6756 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6757 | EXPECT_TRUE( |
| 6758 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6759 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6760 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6761 | #if defined(OS_ANDROID) |
| 6762 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6763 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6764 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6765 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6766 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6767 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6768 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6769 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6770 | } |
| 6771 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6772 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6773 | // called. |
| 6774 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6775 | public: |
| 6776 | MockExpectCTReporter() : num_failures_(0) {} |
| 6777 | ~MockExpectCTReporter() override {} |
| 6778 | |
| 6779 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6780 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6781 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6782 | const X509Certificate* validated_certificate_chain, |
| 6783 | const X509Certificate* served_certificate_chain, |
| 6784 | const SignedCertificateTimestampAndStatusList& |
| 6785 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6786 | num_failures_++; |
| 6787 | } |
| 6788 | |
| 6789 | uint32_t num_failures() { return num_failures_; } |
| 6790 | |
| 6791 | private: |
| 6792 | uint32_t num_failures_; |
| 6793 | }; |
| 6794 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6795 | // A CTPolicyEnforcer that returns a default CertPolicyCompliance value |
| 6796 | // for every certificate. |
| 6797 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6798 | public: |
| 6799 | MockCTPolicyEnforcer() |
| 6800 | : default_result_( |
| 6801 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS) {} |
| 6802 | ~MockCTPolicyEnforcer() override {} |
| 6803 | |
| 6804 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 6805 | X509Certificate* cert, |
| 6806 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6807 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6808 | return default_result_; |
| 6809 | } |
| 6810 | |
| 6811 | void set_default_result(ct::CertPolicyCompliance default_result) { |
| 6812 | default_result_ = default_result; |
| 6813 | } |
| 6814 | |
| 6815 | private: |
| 6816 | ct::CertPolicyCompliance default_result_; |
| 6817 | }; |
| 6818 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6819 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 6820 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 6821 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 6822 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 6823 | #endif |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6824 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6825 | https_test_server.SetSSLConfig( |
| 6826 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6827 | https_test_server.ServeFilesFromSourceDirectory( |
| 6828 | base::FilePath(kTestFilePath)); |
| 6829 | ASSERT_TRUE(https_test_server.Start()); |
| 6830 | |
| 6831 | MockExpectCTReporter reporter; |
| 6832 | TransportSecurityState transport_security_state; |
| 6833 | transport_security_state.enable_static_expect_ct_ = true; |
| 6834 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6835 | |
| 6836 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6837 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6838 | ASSERT_TRUE(cert); |
| 6839 | MockCertVerifier cert_verifier; |
| 6840 | CertVerifyResult verify_result; |
| 6841 | verify_result.verified_cert = cert; |
| 6842 | verify_result.is_issued_by_known_root = true; |
| 6843 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6844 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 6845 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 6846 | // CT violation. |
| 6847 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6848 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6849 | ct_policy_enforcer.set_default_result( |
| 6850 | ct::CertPolicyCompliance::CERT_POLICY_NOT_ENOUGH_SCTS); |
| 6851 | |
| 6852 | TestNetworkDelegate network_delegate; |
| 6853 | // Use a MockHostResolver (which by default maps all hosts to |
| 6854 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6855 | // CT preload list. |
| 6856 | MockHostResolver host_resolver; |
| 6857 | TestURLRequestContext context(true); |
| 6858 | context.set_host_resolver(&host_resolver); |
| 6859 | context.set_transport_security_state(&transport_security_state); |
| 6860 | context.set_network_delegate(&network_delegate); |
| 6861 | context.set_cert_verifier(&cert_verifier); |
| 6862 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 6863 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6864 | context.Init(); |
| 6865 | |
| 6866 | // Now send a request to trigger the violation. |
| 6867 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6868 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6869 | GURL::Replacements replace_host; |
| 6870 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6871 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6872 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 6873 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6874 | violating_request->Start(); |
| 6875 | base::RunLoop().Run(); |
| 6876 | |
| 6877 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6878 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6879 | |
| 6880 | // Tests that Expect CT HTTP headers are processed correctly. |
| 6881 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6882 | base::test::ScopedFeatureList feature_list; |
| 6883 | feature_list.InitAndEnableFeature( |
| 6884 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6885 | |
| 6886 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6887 | https_test_server.SetSSLConfig( |
| 6888 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6889 | https_test_server.ServeFilesFromSourceDirectory( |
| 6890 | base::FilePath(kTestFilePath)); |
| 6891 | ASSERT_TRUE(https_test_server.Start()); |
| 6892 | |
| 6893 | MockExpectCTReporter reporter; |
| 6894 | TransportSecurityState transport_security_state; |
| 6895 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6896 | |
| 6897 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6898 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6899 | ASSERT_TRUE(cert); |
| 6900 | MockCertVerifier cert_verifier; |
| 6901 | CertVerifyResult verify_result; |
| 6902 | verify_result.verified_cert = cert; |
| 6903 | verify_result.is_issued_by_known_root = true; |
| 6904 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6905 | |
| 6906 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6907 | // compliance. |
| 6908 | DoNothingCTVerifier ct_verifier; |
| 6909 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6910 | ct_policy_enforcer.set_default_result( |
| 6911 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6912 | |
| 6913 | TestNetworkDelegate network_delegate; |
| 6914 | // Use a MockHostResolver (which by default maps all hosts to |
| 6915 | // 127.0.0.1). |
| 6916 | MockHostResolver host_resolver; |
| 6917 | TestURLRequestContext context(true); |
| 6918 | context.set_host_resolver(&host_resolver); |
| 6919 | context.set_transport_security_state(&transport_security_state); |
| 6920 | context.set_network_delegate(&network_delegate); |
| 6921 | context.set_cert_verifier(&cert_verifier); |
| 6922 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6923 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6924 | context.Init(); |
| 6925 | |
| 6926 | // Now send a request to trigger the header processing. |
| 6927 | TestDelegate d; |
| 6928 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6929 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6930 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6931 | request->Start(); |
| 6932 | base::RunLoop().Run(); |
| 6933 | |
| 6934 | TransportSecurityState::ExpectCTState state; |
| 6935 | ASSERT_TRUE( |
| 6936 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6937 | EXPECT_TRUE(state.enforce); |
| 6938 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6939 | } |
| 6940 | |
| 6941 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 6942 | // processed. |
| 6943 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 6944 | base::test::ScopedFeatureList feature_list; |
| 6945 | feature_list.InitAndEnableFeature( |
| 6946 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6947 | |
| 6948 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6949 | https_test_server.SetSSLConfig( |
| 6950 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6951 | https_test_server.ServeFilesFromSourceDirectory( |
| 6952 | base::FilePath(kTestFilePath)); |
| 6953 | ASSERT_TRUE(https_test_server.Start()); |
| 6954 | |
| 6955 | MockExpectCTReporter reporter; |
| 6956 | TransportSecurityState transport_security_state; |
| 6957 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6958 | |
| 6959 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6960 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6961 | ASSERT_TRUE(cert); |
| 6962 | MockCertVerifier cert_verifier; |
| 6963 | CertVerifyResult verify_result; |
| 6964 | verify_result.verified_cert = cert; |
| 6965 | verify_result.is_issued_by_known_root = true; |
| 6966 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6967 | |
| 6968 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6969 | // compliance. |
| 6970 | DoNothingCTVerifier ct_verifier; |
| 6971 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6972 | ct_policy_enforcer.set_default_result( |
| 6973 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS); |
| 6974 | |
| 6975 | TestNetworkDelegate network_delegate; |
| 6976 | // Use a MockHostResolver (which by default maps all hosts to |
| 6977 | // 127.0.0.1). |
| 6978 | MockHostResolver host_resolver; |
| 6979 | TestURLRequestContext context(true); |
| 6980 | context.set_host_resolver(&host_resolver); |
| 6981 | context.set_transport_security_state(&transport_security_state); |
| 6982 | context.set_network_delegate(&network_delegate); |
| 6983 | context.set_cert_verifier(&cert_verifier); |
| 6984 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6985 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6986 | context.Init(); |
| 6987 | |
| 6988 | // Now send a request to trigger the header processing. |
| 6989 | TestDelegate d; |
| 6990 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 6991 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6992 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6993 | request->Start(); |
| 6994 | base::RunLoop().Run(); |
| 6995 | |
| 6996 | TransportSecurityState::ExpectCTState state; |
| 6997 | ASSERT_TRUE( |
| 6998 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6999 | EXPECT_TRUE(state.enforce); |
| 7000 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7001 | } |
| 7002 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7003 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7004 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7005 | #if BUILDFLAG(ENABLE_REPORTING) |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7006 | namespace { |
| 7007 | |
| 7008 | class TestReportingService : public ReportingService { |
| 7009 | public: |
| 7010 | struct Header { |
| 7011 | GURL url; |
| 7012 | std::string header_value; |
| 7013 | }; |
| 7014 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7015 | const std::vector<Header>& headers() { return headers_; } |
| 7016 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7017 | // ReportingService implementation: |
| 7018 | |
| 7019 | ~TestReportingService() override {} |
| 7020 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7021 | void QueueReport(const GURL& url, |
| 7022 | const std::string& group, |
| 7023 | const std::string& type, |
| 7024 | std::unique_ptr<const base::Value> body) override { |
| 7025 | NOTIMPLEMENTED(); |
| 7026 | } |
| 7027 | |
| 7028 | void ProcessHeader(const GURL& url, |
| 7029 | const std::string& header_value) override { |
| 7030 | headers_.push_back({url, header_value}); |
| 7031 | } |
| 7032 | |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 7033 | void RemoveBrowsingData( |
| 7034 | int data_type_mask, |
| 7035 | base::Callback<bool(const GURL&)> origin_filter) override { |
| 7036 | NOTIMPLEMENTED(); |
| 7037 | } |
| 7038 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7039 | private: |
| 7040 | std::vector<Header> headers_; |
| 7041 | }; |
| 7042 | |
| 7043 | std::unique_ptr<test_server::HttpResponse> SendReportToHeader( |
| 7044 | const test_server::HttpRequest& request) { |
| 7045 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7046 | new test_server::BasicHttpResponse); |
| 7047 | http_response->set_code(HTTP_OK); |
| 7048 | http_response->AddCustomHeader("Report-To", "foo"); |
| 7049 | http_response->AddCustomHeader("Report-To", "bar"); |
| 7050 | return std::move(http_response); |
| 7051 | } |
| 7052 | |
| 7053 | } // namespace |
| 7054 | |
| 7055 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderNoService) { |
| 7056 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7057 | ASSERT_TRUE(http_test_server()->Start()); |
| 7058 | GURL request_url = http_test_server()->GetURL("/"); |
| 7059 | |
| 7060 | TestNetworkDelegate network_delegate; |
| 7061 | TestURLRequestContext context(true); |
| 7062 | context.set_network_delegate(&network_delegate); |
| 7063 | context.Init(); |
| 7064 | |
| 7065 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7066 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7067 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7068 | request->Start(); |
| 7069 | base::RunLoop().Run(); |
| 7070 | } |
| 7071 | |
| 7072 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderHTTP) { |
| 7073 | http_test_server()->RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7074 | ASSERT_TRUE(http_test_server()->Start()); |
| 7075 | GURL request_url = http_test_server()->GetURL("/"); |
| 7076 | |
| 7077 | TestNetworkDelegate network_delegate; |
| 7078 | TestReportingService reporting_service; |
| 7079 | TestURLRequestContext context(true); |
| 7080 | context.set_network_delegate(&network_delegate); |
| 7081 | context.set_reporting_service(&reporting_service); |
| 7082 | context.Init(); |
| 7083 | |
| 7084 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7085 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7086 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7087 | request->Start(); |
| 7088 | base::RunLoop().Run(); |
| 7089 | |
| 7090 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7091 | } |
| 7092 | |
| 7093 | TEST_F(URLRequestTestHTTP, ProcessReportToHeaderHTTPS) { |
| 7094 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7095 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7096 | ASSERT_TRUE(https_test_server.Start()); |
| 7097 | GURL request_url = https_test_server.GetURL("/"); |
| 7098 | |
| 7099 | TestNetworkDelegate network_delegate; |
| 7100 | TestReportingService reporting_service; |
| 7101 | TestURLRequestContext context(true); |
| 7102 | context.set_network_delegate(&network_delegate); |
| 7103 | context.set_reporting_service(&reporting_service); |
| 7104 | context.Init(); |
| 7105 | |
| 7106 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7107 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7108 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7109 | request->Start(); |
| 7110 | base::RunLoop().Run(); |
| 7111 | |
| 7112 | ASSERT_EQ(1u, reporting_service.headers().size()); |
| 7113 | EXPECT_EQ(request_url, reporting_service.headers()[0].url); |
| 7114 | EXPECT_EQ("foo, bar", reporting_service.headers()[0].header_value); |
| 7115 | } |
| 7116 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7117 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderInvalidHttps) { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7118 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7119 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7120 | https_test_server.RegisterRequestHandler(base::Bind(&SendReportToHeader)); |
| 7121 | ASSERT_TRUE(https_test_server.Start()); |
| 7122 | GURL request_url = https_test_server.GetURL("/"); |
| 7123 | |
| 7124 | TestNetworkDelegate network_delegate; |
| 7125 | TestReportingService reporting_service; |
| 7126 | TestURLRequestContext context(true); |
| 7127 | context.set_network_delegate(&network_delegate); |
| 7128 | context.set_reporting_service(&reporting_service); |
| 7129 | context.Init(); |
| 7130 | |
| 7131 | TestDelegate d; |
| 7132 | d.set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7133 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7134 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7135 | request->Start(); |
| 7136 | base::RunLoop().Run(); |
| 7137 | |
| 7138 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7139 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7140 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7141 | } |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7142 | #endif // BUILDFLAG(ENABLE_REPORTING) |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7143 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7144 | namespace { |
| 7145 | |
| 7146 | class TestNetworkErrorLoggingDelegate : public NetworkErrorLoggingDelegate { |
| 7147 | public: |
| 7148 | struct Header { |
| 7149 | Header() {} |
| 7150 | ~Header() {} |
| 7151 | |
| 7152 | url::Origin origin; |
| 7153 | std::string value; |
| 7154 | }; |
| 7155 | |
| 7156 | struct NetworkError { |
| 7157 | NetworkError() {} |
| 7158 | ~NetworkError() {} |
| 7159 | |
| 7160 | url::Origin origin; |
| 7161 | Error error; |
| 7162 | NetworkErrorLoggingDelegate::ErrorDetails details; |
| 7163 | }; |
| 7164 | |
| 7165 | const std::vector<Header>& headers() { return headers_; } |
| 7166 | const std::vector<NetworkError>& errors() { return errors_; } |
| 7167 | |
| 7168 | // NetworkErrorLoggingDelegate implementation: |
| 7169 | |
| 7170 | ~TestNetworkErrorLoggingDelegate() override {} |
| 7171 | |
| 7172 | void SetReportingService(ReportingService* reporting_service) override { |
| 7173 | NOTREACHED(); |
| 7174 | } |
| 7175 | |
| 7176 | void OnHeader(const url::Origin& origin, const std::string& value) override { |
| 7177 | Header header; |
| 7178 | header.origin = origin; |
| 7179 | header.value = value; |
| 7180 | headers_.push_back(header); |
| 7181 | } |
| 7182 | |
| 7183 | void OnNetworkError(const url::Origin& origin, |
| 7184 | Error error, |
| 7185 | ErrorDetailsCallback details_callback) override { |
| 7186 | NetworkError network_error; |
| 7187 | network_error.origin = origin; |
| 7188 | network_error.error = error; |
| 7189 | std::move(details_callback).Run(&network_error.details); |
| 7190 | errors_.push_back(network_error); |
| 7191 | } |
| 7192 | |
| 7193 | private: |
| 7194 | std::vector<Header> headers_; |
| 7195 | std::vector<NetworkError> errors_; |
| 7196 | }; |
| 7197 | |
| 7198 | std::unique_ptr<test_server::HttpResponse> SendNelHeader( |
| 7199 | const test_server::HttpRequest& request) { |
| 7200 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7201 | new test_server::BasicHttpResponse); |
| 7202 | http_response->set_code(HTTP_OK); |
| 7203 | http_response->AddCustomHeader(NetworkErrorLoggingDelegate::kHeaderName, |
| 7204 | "foo"); |
| 7205 | return std::move(http_response); |
| 7206 | } |
| 7207 | |
| 7208 | } // namespace |
| 7209 | |
| 7210 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderNoDelegate) { |
| 7211 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7212 | ASSERT_TRUE(http_test_server()->Start()); |
| 7213 | GURL request_url = http_test_server()->GetURL("/"); |
| 7214 | |
| 7215 | TestNetworkDelegate network_delegate; |
| 7216 | TestURLRequestContext context(true); |
| 7217 | context.set_network_delegate(&network_delegate); |
| 7218 | context.Init(); |
| 7219 | |
| 7220 | TestDelegate d; |
| 7221 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7222 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7223 | request->Start(); |
| 7224 | base::RunLoop().Run(); |
| 7225 | } |
| 7226 | |
| 7227 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderHttp) { |
| 7228 | http_test_server()->RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7229 | ASSERT_TRUE(http_test_server()->Start()); |
| 7230 | GURL request_url = http_test_server()->GetURL("/"); |
| 7231 | |
| 7232 | TestNetworkDelegate network_delegate; |
| 7233 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7234 | TestURLRequestContext context(true); |
| 7235 | context.set_network_delegate(&network_delegate); |
| 7236 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7237 | context.Init(); |
| 7238 | |
| 7239 | TestDelegate d; |
| 7240 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7241 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7242 | request->Start(); |
| 7243 | base::RunLoop().Run(); |
| 7244 | |
| 7245 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7246 | } |
| 7247 | |
| 7248 | TEST_F(URLRequestTestHTTP, ProcessNelHeaderHttps) { |
| 7249 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7250 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7251 | ASSERT_TRUE(https_test_server.Start()); |
| 7252 | GURL request_url = https_test_server.GetURL("/"); |
| 7253 | |
| 7254 | TestNetworkDelegate network_delegate; |
| 7255 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7256 | TestURLRequestContext context(true); |
| 7257 | context.set_network_delegate(&network_delegate); |
| 7258 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7259 | context.Init(); |
| 7260 | |
| 7261 | TestDelegate d; |
| 7262 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7263 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7264 | request->Start(); |
| 7265 | base::RunLoop().Run(); |
| 7266 | |
| 7267 | ASSERT_EQ(1u, nel_delegate.headers().size()); |
| 7268 | EXPECT_EQ(url::Origin(request_url), nel_delegate.headers()[0].origin); |
| 7269 | EXPECT_EQ("foo", nel_delegate.headers()[0].value); |
| 7270 | } |
| 7271 | |
| 7272 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderInvalidHttps) { |
| 7273 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7274 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7275 | https_test_server.RegisterRequestHandler(base::Bind(&SendNelHeader)); |
| 7276 | ASSERT_TRUE(https_test_server.Start()); |
| 7277 | GURL request_url = https_test_server.GetURL("/"); |
| 7278 | |
| 7279 | TestNetworkDelegate network_delegate; |
| 7280 | TestNetworkErrorLoggingDelegate nel_delegate; |
| 7281 | TestURLRequestContext context(true); |
| 7282 | context.set_network_delegate(&network_delegate); |
| 7283 | context.set_network_error_logging_delegate(&nel_delegate); |
| 7284 | context.Init(); |
| 7285 | |
| 7286 | TestDelegate d; |
| 7287 | d.set_allow_certificate_errors(true); |
| 7288 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7289 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7290 | request->Start(); |
| 7291 | base::RunLoop().Run(); |
| 7292 | |
| 7293 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7294 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7295 | EXPECT_TRUE(nel_delegate.headers().empty()); |
| 7296 | } |
| 7297 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7298 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7299 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7300 | |
| 7301 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7302 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7303 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7304 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7305 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7306 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7307 | |
| 7308 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7309 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7310 | EXPECT_EQ("text/html", mime_type); |
| 7311 | |
| 7312 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7313 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7314 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7315 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7316 | } |
| 7317 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7318 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7319 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7320 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7321 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7322 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7323 | |
| 7324 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7325 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7326 | } |
| 7327 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7328 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7329 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7330 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7331 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7332 | FileProtocolHandler file_protocol_handler( |
| 7333 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7334 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7335 | |
| 7336 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7337 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7338 | } |
| 7339 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7340 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7341 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7342 | |
| 7343 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7344 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7345 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7346 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7347 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7348 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7349 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7350 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7351 | |
| 7352 | // The redirect should have been rejected before reporting it to the caller. |
| 7353 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7354 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7355 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7356 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7357 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7358 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7359 | |
| 7360 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7361 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7362 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7363 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7364 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 7365 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7366 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7367 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7368 | |
| 7369 | // The redirect should have been rejected before reporting it to the |
| 7370 | // caller. See https://crbug.com/723796 |
| 7371 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7372 | } |
| 7373 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7374 | // Test that redirects to invalid URLs are rejected. See |
| 7375 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7376 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7377 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7378 | |
| 7379 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7380 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7381 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7382 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7383 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7384 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7385 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7386 | EXPECT_EQ(1, d.response_started_count()); |
| 7387 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7388 | |
| 7389 | // The redirect should have been rejected before reporting it to the caller. |
| 7390 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7391 | } |
| 7392 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7393 | // Make sure redirects are cached, despite not reading their bodies. |
| 7394 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7395 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7396 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7397 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7398 | |
| 7399 | { |
| 7400 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7401 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7402 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7403 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7404 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7405 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7406 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7407 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7408 | } |
| 7409 | |
| 7410 | { |
| 7411 | TestDelegate d; |
| 7412 | d.set_quit_on_redirect(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7413 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7414 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7415 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7416 | base::RunLoop().Run(); |
| 7417 | |
| 7418 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7419 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7420 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7421 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7422 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7423 | base::RunLoop().Run(); |
| 7424 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7425 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7426 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7427 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7432 | // when the headers are read, since the body won't have been read. |
| 7433 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7434 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7435 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7436 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7437 | |
| 7438 | { |
| 7439 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7440 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7441 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7442 | redirect_to_url); |
| 7443 | |
| 7444 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7445 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7446 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7447 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7448 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7449 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7450 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7451 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7452 | } |
| 7453 | |
| 7454 | { |
| 7455 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7456 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7457 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7458 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7459 | base::RunLoop().Run(); |
| 7460 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7461 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7462 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7463 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7464 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7465 | } |
| 7466 | } |
| 7467 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7468 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7469 | // safe. |
| 7470 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7471 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7472 | |
| 7473 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7474 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7475 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7476 | |
| 7477 | TestDelegate d; |
| 7478 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7479 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7480 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7482 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7483 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7484 | base::RunLoop().Run(); |
| 7485 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7486 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7487 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7488 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7489 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7490 | } |
| 7491 | } |
| 7492 | |
| 7493 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7494 | // some other URL to the whitelist. |
| 7495 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7496 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7497 | |
| 7498 | GURL unsafe_url("data:text/html,something"); |
| 7499 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7500 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7501 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7502 | different_unsafe_url); |
| 7503 | |
| 7504 | TestDelegate d; |
| 7505 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7506 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7507 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7508 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7509 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7510 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7511 | base::RunLoop().Run(); |
| 7512 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7513 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7514 | |
| 7515 | // The redirect should have been rejected before reporting it to the caller. |
| 7516 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7517 | } |
| 7518 | } |
| 7519 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7520 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7521 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7522 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7523 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7524 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7525 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7526 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7527 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7528 | |
| 7529 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7530 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7531 | |
| 7532 | TestDelegate d; |
| 7533 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7534 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7535 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7536 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7537 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7538 | base::RunLoop().Run(); |
| 7539 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7540 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7541 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7542 | EXPECT_EQ(original_url, r->original_url()); |
| 7543 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7544 | } |
| 7545 | } |
| 7546 | |
| 7547 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7548 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7549 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7550 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7551 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7552 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7553 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7554 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7555 | GURL expected_redirect_url( |
| 7556 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7557 | |
| 7558 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7559 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7560 | |
| 7561 | TestDelegate d; |
| 7562 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7563 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7564 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7565 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7566 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7567 | base::RunLoop().Run(); |
| 7568 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7569 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7570 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7571 | EXPECT_EQ(original_url, r->original_url()); |
| 7572 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7573 | } |
| 7574 | } |
| 7575 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7576 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7577 | // URL should not be changed. In particular, the reference fragment should not |
| 7578 | // be modified. |
| 7579 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7580 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7581 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7582 | GURL original_url( |
| 7583 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7584 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7585 | |
| 7586 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7587 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7588 | |
| 7589 | TestDelegate d; |
| 7590 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7591 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7592 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7593 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7594 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7595 | base::RunLoop().Run(); |
| 7596 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7597 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7598 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7599 | EXPECT_EQ(original_url, r->original_url()); |
| 7600 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7601 | } |
| 7602 | } |
| 7603 | |
| 7604 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7605 | // the reference fragment of the target URL must not be modified. |
| 7606 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7607 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7608 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7609 | GURL original_url( |
| 7610 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7611 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7612 | |
| 7613 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7614 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7615 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7616 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7617 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7618 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7619 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7620 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7621 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7622 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7623 | base::RunLoop().Run(); |
| 7624 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7625 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7626 | EXPECT_EQ(original_url, r->original_url()); |
| 7627 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7628 | } |
| 7629 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7630 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7631 | ASSERT_TRUE(http_test_server()->Start()); |
| 7632 | |
| 7633 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7634 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7635 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7636 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7637 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7638 | req->SetReferrer(referrer); |
| 7639 | req->Start(); |
| 7640 | base::RunLoop().Run(); |
| 7641 | |
| 7642 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7643 | } |
| 7644 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7645 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7646 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7647 | |
| 7648 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7649 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7650 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7651 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7652 | req->SetReferrer("http://user:[email protected]/"); |
| 7653 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7654 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7655 | |
| 7656 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7657 | } |
| 7658 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7659 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7660 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7661 | |
| 7662 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7663 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7664 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7665 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7666 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7667 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7668 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7669 | |
| 7670 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7671 | } |
| 7672 | |
| 7673 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7674 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7675 | |
| 7676 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7677 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7678 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7679 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7680 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7681 | req->SetReferrer(""); |
| 7682 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7683 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7684 | |
| 7685 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7686 | } |
| 7687 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7688 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7689 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7690 | |
| 7691 | TestDelegate d; |
| 7692 | { |
| 7693 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7694 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7695 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7696 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7697 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7698 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7699 | |
| 7700 | EXPECT_EQ(1, d.response_started_count()); |
| 7701 | EXPECT_EQ(0, d.bytes_received()); |
| 7702 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7703 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7704 | } |
| 7705 | } |
| 7706 | |
| 7707 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7708 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7709 | |
| 7710 | TestDelegate d; |
| 7711 | { |
| 7712 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7713 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7714 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7715 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7716 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7717 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7718 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7719 | |
| 7720 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7722 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7723 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7724 | |
| 7725 | EXPECT_EQ(1, d.response_started_count()); |
| 7726 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7727 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7728 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7729 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7730 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7731 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7732 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7733 | |
| 7734 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7735 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7736 | EXPECT_EQ(contents, d.data_received()); |
| 7737 | } |
| 7738 | } |
| 7739 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7740 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7741 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7742 | |
| 7743 | TestDelegate d; |
| 7744 | { |
| 7745 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7746 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7747 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7748 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7749 | |
| 7750 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7751 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7752 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7753 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7754 | |
| 7755 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7756 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7757 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7758 | d.ClearFullRequestHeaders(); |
| 7759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7760 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7761 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7762 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7763 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7764 | EXPECT_EQ(1, d.response_started_count()); |
| 7765 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7766 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7767 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7768 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7769 | |
| 7770 | base::FilePath path; |
| 7771 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7772 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7773 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7774 | |
| 7775 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7776 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7777 | EXPECT_EQ(contents, d.data_received()); |
| 7778 | } |
| 7779 | } |
| 7780 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7781 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7782 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7783 | |
| 7784 | TestDelegate d; |
| 7785 | { |
| 7786 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7787 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7788 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7789 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7790 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7791 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7792 | |
| 7793 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7794 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7795 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7796 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7797 | |
| 7798 | EXPECT_EQ(1, d.response_started_count()); |
| 7799 | EXPECT_EQ(0, d.bytes_received()); |
| 7800 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7801 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7802 | } |
| 7803 | } |
| 7804 | |
| 7805 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7806 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7807 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7808 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7809 | { |
| 7810 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7811 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7812 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7813 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7814 | HttpRequestHeaders headers; |
| 7815 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7816 | req->SetExtraRequestHeaders(headers); |
| 7817 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7818 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7819 | |
| 7820 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7821 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7822 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7823 | } |
| 7824 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7825 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7826 | { |
| 7827 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7828 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7829 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7830 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7831 | HttpRequestHeaders headers; |
| 7832 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7833 | req->SetExtraRequestHeaders(headers); |
| 7834 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7835 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7836 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7837 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7838 | |
| 7839 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7840 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7841 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7842 | } |
| 7843 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7844 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7845 | { |
| 7846 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7847 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7848 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7849 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7850 | HttpRequestHeaders headers; |
| 7851 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7852 | req->SetExtraRequestHeaders(headers); |
| 7853 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7854 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7855 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7856 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7857 | |
| 7858 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7859 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7860 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7861 | } |
| 7862 | } |
| 7863 | |
| 7864 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7865 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7866 | |
| 7867 | // populate the cache |
| 7868 | { |
| 7869 | TestDelegate d; |
| 7870 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7871 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7872 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7873 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7874 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7875 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7876 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7877 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7878 | |
| 7879 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7880 | } |
| 7881 | |
| 7882 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7883 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7884 | // response should be fetched from the cache. |
| 7885 | { |
| 7886 | TestDelegate d; |
| 7887 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7888 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7889 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7890 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7891 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7892 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7893 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7894 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7895 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7896 | |
| 7897 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7898 | |
| 7899 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7900 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7905 | // http://crbug.com/6450 |
| 7906 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7907 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7908 | |
| 7909 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7910 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7911 | |
| 7912 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7913 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7914 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7915 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7916 | TestURLRequestContext context(true); |
| 7917 | context.set_network_delegate(&network_delegate); |
| 7918 | context.Init(); |
| 7919 | |
| 7920 | TestDelegate d; |
| 7921 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7922 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7923 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7924 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 7925 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7926 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7927 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7928 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7929 | |
| 7930 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7931 | |
| 7932 | // Make sure we sent the cookie in the restarted transaction. |
| 7933 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7934 | != std::string::npos); |
| 7935 | } |
| 7936 | |
| 7937 | // Same test as above, except this time the restart is initiated earlier |
| 7938 | // (without user intervention since identity is embedded in the URL). |
| 7939 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7940 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7941 | TestURLRequestContext context(true); |
| 7942 | context.set_network_delegate(&network_delegate); |
| 7943 | context.Init(); |
| 7944 | |
| 7945 | TestDelegate d; |
| 7946 | |
| 7947 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 7948 | replacements.SetUsernameStr("user2"); |
| 7949 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7950 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 7951 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7952 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7953 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7954 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7955 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7956 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7957 | |
| 7958 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 7959 | |
| 7960 | // Make sure we sent the cookie in the restarted transaction. |
| 7961 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7962 | != std::string::npos); |
| 7963 | } |
| 7964 | } |
| 7965 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7966 | // Tests that load timing works as expected with auth and the cache. |
| 7967 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7968 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7969 | |
| 7970 | // populate the cache |
| 7971 | { |
| 7972 | TestDelegate d; |
| 7973 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7974 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7975 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7976 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7977 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7978 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7979 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7980 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7981 | |
| 7982 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7983 | |
| 7984 | LoadTimingInfo load_timing_info_before_auth; |
| 7985 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 7986 | &load_timing_info_before_auth)); |
| 7987 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 7988 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 7989 | |
| 7990 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7991 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7992 | // The test server does not support keep alive sockets, so the second |
| 7993 | // request with auth should use a new socket. |
| 7994 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 7995 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 7996 | load_timing_info.socket_log_id); |
| 7997 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 7998 | load_timing_info.connect_timing.connect_start); |
| 7999 | } |
| 8000 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8001 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8002 | // 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] | 8003 | // response should be fetched from the cache. |
| 8004 | { |
| 8005 | TestDelegate d; |
| 8006 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8007 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8008 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8009 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8010 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8011 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8012 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8013 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8014 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8015 | |
| 8016 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8017 | |
| 8018 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8019 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8020 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8021 | // Since there was a request that went over the wire, the load timing |
| 8022 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8023 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8024 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8025 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8026 | } |
| 8027 | } |
| 8028 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8029 | // In this test, we do a POST which the server will 302 redirect. |
| 8030 | // The subsequent transaction should use GET, and should not send the |
| 8031 | // Content-Type header. |
| 8032 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8033 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8034 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8035 | |
| 8036 | const char kData[] = "hello world"; |
| 8037 | |
| 8038 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8039 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8040 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8041 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8042 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8043 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8044 | |
| 8045 | // Set headers (some of which are specific to the POST). |
| 8046 | HttpRequestHeaders headers; |
| 8047 | headers.AddHeadersFromString( |
| 8048 | "Content-Type: multipart/form-data; " |
| 8049 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 8050 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 8051 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 8052 | "Accept-Language: en-US,en\r\n" |
| 8053 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 8054 | "Content-Length: 11\r\n" |
| 8055 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8056 | req->SetExtraRequestHeaders(headers); |
| 8057 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8058 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8059 | |
| 8060 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8061 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8062 | EXPECT_EQ("text/html", mime_type); |
| 8063 | |
| 8064 | const std::string& data = d.data_received(); |
| 8065 | |
| 8066 | // Check that the post-specific headers were stripped: |
| 8067 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8068 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8069 | |
| 8070 | // These extra request headers should not have been stripped. |
| 8071 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8072 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8073 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
Yutaka Hirano | 322fef87 | 2017-08-09 08:11:43 | [diff] [blame] | 8074 | EXPECT_TRUE(ContainsString(data, "Origin: http://localhost:1337")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8075 | } |
| 8076 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8077 | // The following tests check that we handle mutating the request for HTTP |
| 8078 | // redirects as expected. |
| 8079 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8080 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8081 | |
| 8082 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8083 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8084 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8085 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8086 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8087 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8088 | |
| 8089 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8090 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8091 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8092 | |
| 8093 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8094 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 322fef87 | 2017-08-09 08:11:43 | [diff] [blame] | 8095 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", url.GetOrigin().spec()); |
| 8096 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8097 | } |
| 8098 | |
| 8099 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8100 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8101 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8102 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8103 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8104 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8105 | |
| 8106 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8107 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8108 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8109 | |
| 8110 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8111 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 322fef87 | 2017-08-09 08:11:43 | [diff] [blame] | 8112 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", url.GetOrigin().spec()); |
| 8113 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8114 | } |
| 8115 | |
| 8116 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8117 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8118 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8119 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8120 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8121 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8122 | |
| 8123 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8124 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8125 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8126 | |
| 8127 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8128 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 322fef87 | 2017-08-09 08:11:43 | [diff] [blame] | 8129 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", url.GetOrigin().spec()); |
| 8130 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8131 | } |
| 8132 | |
| 8133 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8134 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8135 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8136 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8137 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8138 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8139 | |
| 8140 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8141 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8142 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8143 | |
| 8144 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8145 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8146 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8147 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8148 | } |
| 8149 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8150 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8151 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8152 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8153 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8154 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8155 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8156 | |
| 8157 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8158 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8159 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8160 | |
| 8161 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8162 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8163 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8164 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8165 | } |
| 8166 | |
| 8167 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8168 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8169 | // (Like Google Drive). |
| 8170 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8171 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8172 | |
| 8173 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8174 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8175 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8176 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
| 8177 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8178 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8179 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8180 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8181 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8182 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8183 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8184 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8185 | } |
| 8186 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8187 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8188 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8189 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8190 | GURL original_url( |
| 8191 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8192 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8193 | |
| 8194 | TestDelegate d; |
| 8195 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8196 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8197 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8198 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8199 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8200 | base::RunLoop().Run(); |
| 8201 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8202 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8203 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8204 | EXPECT_EQ(original_url, r->original_url()); |
| 8205 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8206 | } |
| 8207 | } |
| 8208 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8209 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8210 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8211 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8212 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8213 | GURL first_party_url("http://example.com"); |
| 8214 | |
| 8215 | TestDelegate d; |
| 8216 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8217 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8218 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8219 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8220 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8221 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8222 | base::RunLoop().Run(); |
| 8223 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8224 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8225 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8226 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8232 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8233 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8234 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8235 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8236 | |
| 8237 | TestDelegate d; |
| 8238 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8239 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8240 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8241 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8242 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8243 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8244 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8245 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8246 | base::RunLoop().Run(); |
| 8247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8248 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8249 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8250 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8251 | } |
| 8252 | } |
| 8253 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8254 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8255 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8256 | |
| 8257 | const char kData[] = "hello world"; |
| 8258 | |
| 8259 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8260 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8261 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8262 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8263 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8264 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8265 | HttpRequestHeaders headers; |
| 8266 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8267 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8268 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8269 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8270 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8271 | req.get(), &default_network_delegate_, |
| 8272 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8273 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8274 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8275 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8276 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8277 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8278 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8279 | } |
| 8280 | |
| 8281 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8282 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8283 | |
| 8284 | const char kData[] = "hello world"; |
| 8285 | |
| 8286 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8287 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8288 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8289 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8290 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8291 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8292 | HttpRequestHeaders headers; |
| 8293 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 8294 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8295 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8296 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8297 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8298 | req.get(), &default_network_delegate_, |
| 8299 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8300 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8301 | "Very Good Reason")); |
| 8302 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8303 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8304 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8305 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8306 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8307 | EXPECT_EQ(kData, d.data_received()); |
| 8308 | } |
| 8309 | |
| 8310 | // Check that default A-L header is sent. |
| 8311 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8312 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8313 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8314 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8315 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8316 | TestURLRequestContext context(true); |
| 8317 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8318 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8319 | context.Init(); |
| 8320 | |
| 8321 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8322 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8323 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8324 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8325 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8326 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8327 | EXPECT_EQ("en", d.data_received()); |
| 8328 | } |
| 8329 | |
| 8330 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8331 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8332 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8333 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8334 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8335 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8336 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8337 | TestURLRequestContext context(true); |
| 8338 | context.set_network_delegate(&network_delegate); |
| 8339 | context.Init(); |
| 8340 | // We override the language after initialization because empty entries |
| 8341 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8342 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8343 | |
| 8344 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8345 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8346 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8347 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8348 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8349 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8350 | EXPECT_EQ("None", d.data_received()); |
| 8351 | } |
| 8352 | |
| 8353 | // Check that if request overrides the A-L header, the default is not appended. |
| 8354 | // See http://crbug.com/20894 |
| 8355 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8356 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8357 | |
| 8358 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8359 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8360 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8361 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8362 | HttpRequestHeaders headers; |
| 8363 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8364 | req->SetExtraRequestHeaders(headers); |
| 8365 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8366 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8367 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8368 | } |
| 8369 | |
| 8370 | // Check that default A-E header is sent. |
| 8371 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8372 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8373 | |
| 8374 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8375 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8376 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8377 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8378 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8379 | req->SetExtraRequestHeaders(headers); |
| 8380 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8381 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8382 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8383 | } |
| 8384 | |
| 8385 | // Check that if request overrides the A-E header, the default is not appended. |
| 8386 | // See http://crbug.com/47381 |
| 8387 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8388 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8389 | |
| 8390 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8391 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8392 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8393 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8394 | HttpRequestHeaders headers; |
| 8395 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8396 | req->SetExtraRequestHeaders(headers); |
| 8397 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8398 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8399 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8400 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8401 | } |
| 8402 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8403 | // Check that setting the A-C header sends the proper header. |
| 8404 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8405 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8406 | |
| 8407 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8408 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8409 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8410 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8411 | HttpRequestHeaders headers; |
| 8412 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8413 | req->SetExtraRequestHeaders(headers); |
| 8414 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8415 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8416 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8417 | } |
| 8418 | |
| 8419 | // Check that default User-Agent header is sent. |
| 8420 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8421 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8422 | |
| 8423 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8424 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8425 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8426 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8427 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8428 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8429 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8430 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8431 | } |
| 8432 | |
| 8433 | // Check that if request overrides the User-Agent header, |
| 8434 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8435 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8436 | #if defined(OS_IOS) |
| 8437 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8438 | #else |
| 8439 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8440 | #endif |
| 8441 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8442 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8443 | |
| 8444 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8445 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8446 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8447 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8448 | HttpRequestHeaders headers; |
| 8449 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8450 | req->SetExtraRequestHeaders(headers); |
| 8451 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8452 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8453 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8454 | } |
| 8455 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8456 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8457 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8458 | // Accept-Charset headers. |
| 8459 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8460 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8461 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8462 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8463 | TestURLRequestContext context(true); |
| 8464 | context.set_network_delegate(&network_delegate); |
| 8465 | context.Init(); |
| 8466 | // We override the HttpUserAgentSettings after initialization because empty |
| 8467 | // entries get overridden by Init(). |
| 8468 | context.set_http_user_agent_settings(NULL); |
| 8469 | |
| 8470 | struct { |
| 8471 | const char* request; |
| 8472 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8473 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8474 | {"/echoheader?Accept-Charset", "None"}, |
| 8475 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8476 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 8477 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8478 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8479 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8480 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8482 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8483 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8484 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8485 | << " Request = \"" << tests[i].request << "\""; |
| 8486 | } |
| 8487 | } |
| 8488 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8489 | // Make sure that URLRequest passes on its priority updates to |
| 8490 | // newly-created jobs after the first one. |
| 8491 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8492 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8493 | |
| 8494 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8495 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8496 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8498 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8499 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8500 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8501 | req.get(), &default_network_delegate_, |
| 8502 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8503 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8504 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8505 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8506 | req->SetPriority(LOW); |
| 8507 | req->Start(); |
| 8508 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8509 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8510 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8511 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8512 | req.get(), &default_network_delegate_, &job_priority)); |
| 8513 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8514 | |
| 8515 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8516 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8517 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8518 | } |
| 8519 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8520 | // Check that creating a network request while entering/exiting suspend mode |
| 8521 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8522 | // does not return an HttpTransaction. |
| 8523 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 8524 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8525 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8526 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8527 | network_layer->OnSuspend(); |
| 8528 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8529 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 8530 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 8531 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8532 | |
| 8533 | TestURLRequestContext context(true); |
| 8534 | context.set_http_transaction_factory(&http_cache); |
| 8535 | context.Init(); |
| 8536 | |
| 8537 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8538 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8539 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8540 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8541 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8542 | base::RunLoop().Run(); |
| 8543 | |
| 8544 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8545 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8546 | } |
| 8547 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8548 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8549 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8550 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 8551 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 8552 | public: |
| 8553 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 8554 | : network_session_(network_session) {} |
| 8555 | |
| 8556 | ~FailingHttpTransactionFactory() override {} |
| 8557 | |
| 8558 | // HttpTransactionFactory methods: |
| 8559 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8560 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8561 | return ERR_FAILED; |
| 8562 | } |
| 8563 | |
| 8564 | HttpCache* GetCache() override { return nullptr; } |
| 8565 | |
| 8566 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 8567 | |
| 8568 | private: |
| 8569 | HttpNetworkSession* network_session_; |
| 8570 | |
| 8571 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 8572 | }; |
| 8573 | |
| 8574 | } // namespace |
| 8575 | |
| 8576 | // Check that when a request that fails to create an HttpTransaction can be |
| 8577 | // cancelled while the failure notification is pending, and doesn't send two |
| 8578 | // failure notifications. |
| 8579 | // |
| 8580 | // This currently only happens when in suspend mode and there's no cache, but |
| 8581 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 8582 | // behaviors. |
| 8583 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 8584 | FailingHttpTransactionFactory http_transaction_factory( |
| 8585 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8586 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8587 | context.set_http_transaction_factory(&http_transaction_factory); |
| 8588 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8589 | context.Init(); |
| 8590 | |
| 8591 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8592 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8593 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8594 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8595 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 8596 | // try to create an HttpNetworkTransaction synchronously on start). |
| 8597 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8598 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8599 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8600 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8601 | // Run pending error task, if there is one. |
| 8602 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8603 | |
| 8604 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8605 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8606 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8607 | |
| 8608 | // NetworkDelegate should see the cancellation, but not the error. |
| 8609 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 8610 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8611 | } |
| 8612 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8613 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8614 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8615 | |
| 8616 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8617 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8618 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8619 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8620 | |
| 8621 | req->Start(); |
| 8622 | base::RunLoop().Run(); |
| 8623 | |
| 8624 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8625 | } |
| 8626 | |
| 8627 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8628 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8629 | |
| 8630 | // Populate the cache. |
| 8631 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8632 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8633 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8634 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8635 | req->Start(); |
| 8636 | base::RunLoop().Run(); |
| 8637 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8638 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8639 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8640 | EXPECT_FALSE(req->response_info().was_cached); |
| 8641 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8642 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8643 | DEFAULT_PRIORITY, &d, |
| 8644 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8645 | req->Start(); |
| 8646 | base::RunLoop().Run(); |
| 8647 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8648 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8649 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8650 | EXPECT_TRUE(req->response_info().was_cached); |
| 8651 | } |
| 8652 | |
| 8653 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8654 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8655 | |
| 8656 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8657 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8658 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8659 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 8660 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8661 | |
| 8662 | req->Start(); |
| 8663 | base::RunLoop().Run(); |
| 8664 | |
| 8665 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8666 | } |
| 8667 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8668 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8669 | // correctly in the absence of contention. |
| 8670 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 8671 | ASSERT_TRUE(http_test_server()->Start()); |
| 8672 | |
| 8673 | TestDelegate d; |
| 8674 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8675 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8676 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8677 | req->Start(); |
| 8678 | base::RunLoop().Run(); |
| 8679 | |
| 8680 | EXPECT_TRUE(req->status().is_success()); |
| 8681 | } |
| 8682 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8683 | // A class to hold state for responding to USER_NOTIFY callbacks from |
| 8684 | // BlockingNetworkDelegate. It also accepts a RunLoop that will be |
| 8685 | // signaled via QuitWhenIdle() when any request is blocked. |
| 8686 | // |
| 8687 | class NotificationCallbackHandler { |
| 8688 | public: |
| 8689 | // Default constructed object doesn't block anything. |
| 8690 | NotificationCallbackHandler() : run_loop_(nullptr) {} |
| 8691 | |
| 8692 | void AddURLRequestToBlockList(const URLRequest* request) { |
| 8693 | requests_to_block_.insert(request); |
| 8694 | } |
| 8695 | |
| 8696 | Error ShouldBlockRequest(const CompletionCallback& callback, |
| 8697 | const URLRequest* request) { |
| 8698 | if (requests_to_block_.find(request) == requests_to_block_.end()) { |
| 8699 | return OK; |
| 8700 | } |
| 8701 | |
| 8702 | DCHECK(blocked_callbacks_.find(request) == blocked_callbacks_.end()); |
| 8703 | blocked_callbacks_[request] = callback; |
| 8704 | if (run_loop_ && blocked_callbacks_.size() == requests_to_block_.size()) |
| 8705 | run_loop_->QuitWhenIdle(); |
| 8706 | return ERR_IO_PENDING; |
| 8707 | } |
| 8708 | |
| 8709 | // Erases object's memory of blocked callbacks as a side effect. |
| 8710 | void GetBlockedCallbacks( |
| 8711 | std::map<const URLRequest*, CompletionCallback>* blocked_callbacks) { |
| 8712 | blocked_callbacks_.swap(*blocked_callbacks); |
| 8713 | } |
| 8714 | |
| 8715 | // Set a RunLoop that, if non-null, will be signaled if any request |
| 8716 | // is blocked. It is the callers responsibility to make sure the |
| 8717 | // passed object lives past the destruction of this class or |
| 8718 | // next call to SetRunLoop(). |
| 8719 | void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 8720 | |
| 8721 | private: |
| 8722 | std::set<const URLRequest*> requests_to_block_; |
| 8723 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks_; |
| 8724 | |
| 8725 | base::RunLoop* run_loop_; |
| 8726 | |
| 8727 | DISALLOW_COPY_AND_ASSIGN(NotificationCallbackHandler); |
| 8728 | }; |
| 8729 | |
| 8730 | TEST_F(URLRequestTestHTTP, MultiThrottledPriority) { |
| 8731 | ASSERT_TRUE(http_test_server()->Start()); |
| 8732 | |
| 8733 | base::RunLoop run_until_request_blocked; |
| 8734 | |
| 8735 | NotificationCallbackHandler notification_handler; |
| 8736 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8737 | BlockingNetworkDelegate network_delegate( |
| 8738 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8739 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8740 | network_delegate.set_notification_callback( |
| 8741 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8742 | // Both objects are owned by this function, and |
| 8743 | // |*network_delegate| will be destroyed first, so |
| 8744 | // it's safe to pass it an unretained pointer. |
| 8745 | base::Unretained(¬ification_handler))); |
| 8746 | |
| 8747 | TestURLRequestContext context(true); |
| 8748 | context.set_network_delegate(&network_delegate); |
| 8749 | context.Init(); |
| 8750 | |
| 8751 | // Use different test URLs to make sure all three requests turn into |
| 8752 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8753 | // the requests may be waited on separately. |
| 8754 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8755 | std::unique_ptr<URLRequest> req1( |
| 8756 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8757 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8758 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8759 | |
| 8760 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8761 | std::unique_ptr<URLRequest> req2( |
| 8762 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8763 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8764 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8765 | |
| 8766 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8767 | std::unique_ptr<URLRequest> req3( |
| 8768 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8769 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8770 | req1->Start(); |
| 8771 | req2->Start(); |
| 8772 | req3->Start(); |
| 8773 | run_until_request_blocked.Run(); |
| 8774 | notification_handler.SetRunLoop(nullptr); |
| 8775 | |
| 8776 | // The first two requests should be blocked based on the notification |
| 8777 | // callback, and their status should have blocked the third request |
| 8778 | // through throttling. |
| 8779 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8780 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8781 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8782 | |
| 8783 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8784 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8785 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8786 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8787 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8788 | |
| 8789 | // Unblocking one of the requests blocked on the notification callback |
| 8790 | // should let it complete, which should then let the third request |
| 8791 | // complete. Unblock the second request, then wait for the third |
| 8792 | // request to complete. |
| 8793 | // TODO(rdsmith): Find something to wait on other than the third |
| 8794 | // requests completion; if there's a bug in throttling, that will |
| 8795 | // result in this test hanging rather than failing quickly. |
| 8796 | d1.set_quit_on_complete(false); |
| 8797 | d2.set_quit_on_complete(false); |
| 8798 | d3.set_quit_on_complete(true); |
| 8799 | blocked_callbacks[req2.get()].Run(OK); |
| 8800 | base::RunLoop().Run(); |
| 8801 | |
| 8802 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8803 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8804 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8805 | // req3 is only unblocked after req2 completes, so req2's |
| 8806 | // success is guaranteed at this point in the function. |
| 8807 | EXPECT_EQ(URLRequestStatus::SUCCESS, req2->status().status()); |
| 8808 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8809 | } |
| 8810 | |
| 8811 | // Confirm that failing a request unblocks following requests. |
| 8812 | TEST_F(URLRequestTestHTTP, ThrottledFailure) { |
| 8813 | ASSERT_TRUE(http_test_server()->Start()); |
| 8814 | |
| 8815 | base::RunLoop run_until_request_blocked; |
| 8816 | |
| 8817 | NotificationCallbackHandler notification_handler; |
| 8818 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8819 | BlockingNetworkDelegate network_delegate( |
| 8820 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8821 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8822 | network_delegate.set_notification_callback( |
| 8823 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8824 | // Both objects are owned by this function, and |
| 8825 | // |*network_delegate| will be destroyed first, so |
| 8826 | // it's safe to pass it an unretained pointer. |
| 8827 | base::Unretained(¬ification_handler))); |
| 8828 | |
| 8829 | TestURLRequestContext context(true); |
| 8830 | context.set_network_delegate(&network_delegate); |
| 8831 | context.Init(); |
| 8832 | |
| 8833 | // Use different test URLs to make sure all three requests turn into |
| 8834 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8835 | // the requests may be waited on separately. |
| 8836 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8837 | std::unique_ptr<URLRequest> req1( |
| 8838 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8839 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8840 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8841 | |
| 8842 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8843 | std::unique_ptr<URLRequest> req2( |
| 8844 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8845 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8846 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8847 | |
| 8848 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8849 | std::unique_ptr<URLRequest> req3( |
| 8850 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8851 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8852 | req1->Start(); |
| 8853 | req2->Start(); |
| 8854 | req3->Start(); |
| 8855 | run_until_request_blocked.Run(); |
| 8856 | notification_handler.SetRunLoop(nullptr); |
| 8857 | |
| 8858 | // The first two requests should be blocked based on the notification |
| 8859 | // callback, and their status should have blocked the third request |
| 8860 | // through throttling. |
| 8861 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8862 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8863 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8864 | |
| 8865 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8866 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8867 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8868 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8869 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8870 | |
| 8871 | // Confirm canceling one of the outstanding requests allows the |
| 8872 | // blocked request to complete. |
| 8873 | |
| 8874 | // TODO(rdsmith): Find something to wait on other than the third |
| 8875 | // requests completion; if there's a bug in throttling, that will |
| 8876 | // result in this test hanging rather than failing quickly. |
| 8877 | d1.set_quit_on_complete(false); |
| 8878 | d2.set_quit_on_complete(false); |
| 8879 | d3.set_quit_on_complete(true); |
| 8880 | req2->Cancel(); |
| 8881 | base::RunLoop().Run(); |
| 8882 | |
| 8883 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8884 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8885 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8886 | EXPECT_EQ(URLRequestStatus::CANCELED, req2->status().status()); |
| 8887 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8888 | } |
| 8889 | |
| 8890 | TEST_F(URLRequestTestHTTP, ThrottledRepriUnblock) { |
| 8891 | ASSERT_TRUE(http_test_server()->Start()); |
| 8892 | |
| 8893 | base::RunLoop run_until_request_blocked; |
| 8894 | |
| 8895 | NotificationCallbackHandler notification_handler; |
| 8896 | notification_handler.SetRunLoop(&run_until_request_blocked); |
| 8897 | BlockingNetworkDelegate network_delegate( |
| 8898 | BlockingNetworkDelegate::USER_NOTIFY); |
| 8899 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 8900 | network_delegate.set_notification_callback( |
| 8901 | base::Bind(&NotificationCallbackHandler::ShouldBlockRequest, |
| 8902 | // Both objects are owned by this function, and |
| 8903 | // |*network_delegate| will be destroyed first, so |
| 8904 | // it's safe to pass it an unretained pointer. |
| 8905 | base::Unretained(¬ification_handler))); |
| 8906 | |
| 8907 | TestURLRequestContext context(true); |
| 8908 | context.set_network_delegate(&network_delegate); |
| 8909 | context.Init(); |
| 8910 | |
| 8911 | // Use different test URLs to make sure all three requests turn into |
| 8912 | // HttpNetworkTransacations. Use different URLRequest::Delegates so that |
| 8913 | // the requests may be waited on separately. |
| 8914 | TestDelegate d1; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8915 | std::unique_ptr<URLRequest> req1( |
| 8916 | context.CreateRequest(http_test_server()->GetURL("/echoall/l"), THROTTLED, |
| 8917 | &d1, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8918 | notification_handler.AddURLRequestToBlockList(req1.get()); |
| 8919 | |
| 8920 | TestDelegate d2; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8921 | std::unique_ptr<URLRequest> req2( |
| 8922 | context.CreateRequest(http_test_server()->GetURL("/echoall/2"), THROTTLED, |
| 8923 | &d2, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8924 | notification_handler.AddURLRequestToBlockList(req2.get()); |
| 8925 | |
| 8926 | TestDelegate d3; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8927 | std::unique_ptr<URLRequest> req3( |
| 8928 | context.CreateRequest(http_test_server()->GetURL("/echoall/3"), THROTTLED, |
| 8929 | &d3, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8930 | req1->Start(); |
| 8931 | req2->Start(); |
| 8932 | req3->Start(); |
| 8933 | run_until_request_blocked.Run(); |
| 8934 | notification_handler.SetRunLoop(nullptr); |
| 8935 | |
| 8936 | // The first two requests should be blocked based on the notification |
| 8937 | // callback, and their status should have blocked the third request |
| 8938 | // through throttling. |
| 8939 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8940 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8941 | EXPECT_TRUE(req3->status().is_io_pending()); |
| 8942 | |
| 8943 | std::map<const URLRequest*, CompletionCallback> blocked_callbacks; |
| 8944 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8945 | ASSERT_EQ(2u, blocked_callbacks.size()); |
| 8946 | ASSERT_TRUE(blocked_callbacks.find(req1.get()) != blocked_callbacks.end()); |
| 8947 | ASSERT_TRUE(blocked_callbacks.find(req2.get()) != blocked_callbacks.end()); |
| 8948 | |
| 8949 | // Confirm raising the priority of the third request allows it to complete. |
| 8950 | |
| 8951 | // TODO(rdsmith): Find something to wait on other than the third |
| 8952 | // requests completion; if there's a bug in throttling, that will |
| 8953 | // result in this test hanging rather than failing quickly. |
| 8954 | d1.set_quit_on_complete(false); |
| 8955 | d2.set_quit_on_complete(false); |
| 8956 | d3.set_quit_on_complete(true); |
| 8957 | req3->SetPriority(IDLE); |
| 8958 | base::RunLoop().Run(); |
| 8959 | |
| 8960 | notification_handler.GetBlockedCallbacks(&blocked_callbacks); |
| 8961 | EXPECT_EQ(0u, blocked_callbacks.size()); |
| 8962 | EXPECT_TRUE(req1->status().is_io_pending()); |
| 8963 | EXPECT_TRUE(req2->status().is_io_pending()); |
| 8964 | EXPECT_EQ(URLRequestStatus::SUCCESS, req3->status().status()); |
| 8965 | } |
| 8966 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8967 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 8968 | ASSERT_TRUE(http_test_server()->Start()); |
| 8969 | |
| 8970 | TestDelegate d; |
| 8971 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8972 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 8973 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8974 | req->Start(); |
| 8975 | base::RunLoop().Run(); |
| 8976 | |
| 8977 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 8978 | } |
| 8979 | |
| 8980 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 8981 | ASSERT_TRUE(http_test_server()->Start()); |
| 8982 | |
| 8983 | TestDelegate d; |
| 8984 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8985 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 8986 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8987 | req->Start(); |
| 8988 | base::RunLoop().Run(); |
| 8989 | |
| 8990 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 8991 | } |
| 8992 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8993 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 8994 | // the delegate isn't called back synchronously. |
| 8995 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 8996 | ASSERT_TRUE(http_test_server()->Start()); |
| 8997 | default_network_delegate_.set_before_start_transaction_fails(); |
| 8998 | |
| 8999 | TestDelegate d; |
| 9000 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9001 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9002 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9003 | req->Start(); |
| 9004 | DCHECK(!d.response_completed()); |
| 9005 | base::RunLoop().Run(); |
| 9006 | DCHECK(d.response_completed()); |
| 9007 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9008 | } |
| 9009 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9010 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9011 | public: |
| 9012 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9013 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9014 | // tests into the factory tests. |
| 9015 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 9016 | } |
| 9017 | |
| 9018 | void SetUpFactory() override { |
| 9019 | interceptor_ = new MockURLRequestInterceptor(); |
| 9020 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9021 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9022 | } |
| 9023 | |
| 9024 | MockURLRequestInterceptor* interceptor() const { |
| 9025 | return interceptor_; |
| 9026 | } |
| 9027 | |
| 9028 | private: |
| 9029 | MockURLRequestInterceptor* interceptor_; |
| 9030 | }; |
| 9031 | |
| 9032 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9033 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9034 | interceptor()->set_intercept_redirect(true); |
| 9035 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9036 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9037 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9038 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9039 | |
| 9040 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9041 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9042 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9043 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9044 | req->Start(); |
| 9045 | base::RunLoop().Run(); |
| 9046 | |
| 9047 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9048 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9049 | int status = d.request_status(); |
| 9050 | EXPECT_EQ(OK, status); |
| 9051 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9052 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9053 | |
| 9054 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9055 | EXPECT_EQ(1, d.response_started_count()); |
| 9056 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9057 | |
| 9058 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9059 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9060 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9061 | } |
| 9062 | |
| 9063 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9064 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9065 | // Intercept that error and respond with an OK response. |
| 9066 | interceptor()->set_intercept_final_response(true); |
| 9067 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9068 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9069 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9070 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9071 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9072 | |
| 9073 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9074 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9075 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9076 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9077 | req->set_method("GET"); |
| 9078 | req->Start(); |
| 9079 | base::RunLoop().Run(); |
| 9080 | |
| 9081 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9082 | |
| 9083 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9084 | int status = d.request_status(); |
| 9085 | EXPECT_EQ(OK, status); |
| 9086 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9087 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9088 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9089 | EXPECT_EQ(1, d.response_started_count()); |
| 9090 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9091 | |
| 9092 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9093 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9094 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9095 | } |
| 9096 | |
| 9097 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9098 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9099 | // Intercept that error and respond with an OK response. |
| 9100 | interceptor()->set_intercept_final_response(true); |
| 9101 | |
| 9102 | // Intercept with a real URLRequestHttpJob. |
| 9103 | interceptor()->set_use_url_request_http_job(true); |
| 9104 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9105 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9106 | |
| 9107 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9108 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9109 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9110 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9111 | req->set_method("GET"); |
| 9112 | req->Start(); |
| 9113 | base::RunLoop().Run(); |
| 9114 | |
| 9115 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9116 | |
| 9117 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9118 | int status = d.request_status(); |
| 9119 | EXPECT_EQ(OK, status); |
| 9120 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9121 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9122 | EXPECT_EQ("hello", d.data_received()); |
| 9123 | EXPECT_EQ(1, d.response_started_count()); |
| 9124 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9125 | |
| 9126 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9127 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9128 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9129 | } |
| 9130 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9131 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9132 | public: |
| 9133 | URLRequestTestReferrerPolicy() {} |
| 9134 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9135 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9136 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9137 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9138 | origin_server_->AddDefaultHandlers( |
| 9139 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9140 | } else { |
| 9141 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9142 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9143 | ASSERT_TRUE(origin_server_->Start()); |
| 9144 | } |
| 9145 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9146 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9147 | net::EmbeddedTestServer::Type dest_type) { |
| 9148 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9149 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9150 | origin_server_->AddDefaultHandlers( |
| 9151 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9152 | } else { |
| 9153 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9154 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9155 | ASSERT_TRUE(origin_server_->Start()); |
| 9156 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9157 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9158 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9159 | destination_server_->AddDefaultHandlers( |
| 9160 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9161 | } else { |
| 9162 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9163 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9164 | ASSERT_TRUE(destination_server_->Start()); |
| 9165 | } |
| 9166 | |
| 9167 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9168 | const GURL& referrer, |
| 9169 | const GURL& expected) { |
| 9170 | // Create and execute the request: we'll only have a |destination_server_| |
| 9171 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9172 | // |origin_server_| for both endpoints. |
| 9173 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9174 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9175 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9176 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9177 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9178 | |
| 9179 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9180 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9181 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9182 | req->set_referrer_policy(policy); |
| 9183 | req->SetReferrer(referrer.spec()); |
| 9184 | req->Start(); |
| 9185 | base::RunLoop().Run(); |
| 9186 | |
| 9187 | EXPECT_EQ(1, d.response_started_count()); |
| 9188 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9189 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9190 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9191 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9192 | |
| 9193 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9194 | if (expected.is_empty()) |
| 9195 | EXPECT_EQ("None", d.data_received()); |
| 9196 | else |
| 9197 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9198 | } |
| 9199 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9200 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9201 | |
| 9202 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9203 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9204 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9205 | }; |
| 9206 | |
| 9207 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9208 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9209 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9210 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9211 | VerifyReferrerAfterRedirect( |
| 9212 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9213 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9214 | |
| 9215 | VerifyReferrerAfterRedirect( |
| 9216 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9217 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9218 | |
| 9219 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9220 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9221 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9222 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9223 | referrer); |
| 9224 | |
| 9225 | // The original referrer set on the request is expected to obey the referrer |
| 9226 | // policy and already be stripped to the origin; thus this test case just |
| 9227 | // checks that this policy doesn't cause the referrer to change when following |
| 9228 | // a redirect. |
| 9229 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9230 | referrer.GetOrigin()); |
| 9231 | |
| 9232 | VerifyReferrerAfterRedirect( |
| 9233 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9234 | referrer); |
| 9235 | |
| 9236 | // The original referrer set on the request is expected to obey the referrer |
| 9237 | // policy and already be stripped to the origin; thus this test case just |
| 9238 | // checks that this policy doesn't cause the referrer to change when following |
| 9239 | // a redirect. |
| 9240 | VerifyReferrerAfterRedirect( |
| 9241 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9242 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9243 | |
| 9244 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9245 | } |
| 9246 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9247 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9248 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9249 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9250 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9251 | |
| 9252 | VerifyReferrerAfterRedirect( |
| 9253 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9254 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9255 | |
| 9256 | VerifyReferrerAfterRedirect( |
| 9257 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9258 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9259 | |
| 9260 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9261 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9262 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9263 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9264 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9265 | referrer); |
| 9266 | |
| 9267 | // The original referrer set on the request is expected to obey the referrer |
| 9268 | // policy and already be stripped to the origin; thus this test case just |
| 9269 | // checks that this policy doesn't cause the referrer to change when following |
| 9270 | // a redirect. |
| 9271 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9272 | referrer.GetOrigin()); |
| 9273 | |
| 9274 | VerifyReferrerAfterRedirect( |
| 9275 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9276 | |
| 9277 | // The original referrer set on the request is expected to obey the referrer |
| 9278 | // policy and already be stripped to the origin; thus this test case just |
| 9279 | // checks that this policy doesn't cause the referrer to change when following |
| 9280 | // a redirect. |
| 9281 | VerifyReferrerAfterRedirect( |
| 9282 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9283 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9284 | |
| 9285 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9286 | } |
| 9287 | |
| 9288 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9289 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9290 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9291 | |
| 9292 | VerifyReferrerAfterRedirect( |
| 9293 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9294 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9295 | |
| 9296 | VerifyReferrerAfterRedirect( |
| 9297 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9298 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9299 | |
| 9300 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9301 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9302 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9303 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9304 | referrer); |
| 9305 | |
| 9306 | // The original referrer set on the request is expected to obey the referrer |
| 9307 | // policy and already be stripped to the origin; thus this test case just |
| 9308 | // checks that this policy doesn't cause the referrer to change when following |
| 9309 | // a redirect. |
| 9310 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9311 | referrer.GetOrigin()); |
| 9312 | |
| 9313 | VerifyReferrerAfterRedirect( |
| 9314 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9315 | referrer); |
| 9316 | |
| 9317 | // The original referrer set on the request is expected to obey the referrer |
| 9318 | // policy and already be stripped to the origin; thus this test case just |
| 9319 | // checks that this policy doesn't cause the referrer to change when following |
| 9320 | // a redirect. |
| 9321 | VerifyReferrerAfterRedirect( |
| 9322 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9323 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9324 | |
| 9325 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9326 | } |
| 9327 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9328 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9329 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9330 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9331 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9332 | |
| 9333 | VerifyReferrerAfterRedirect( |
| 9334 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9335 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9336 | |
| 9337 | VerifyReferrerAfterRedirect( |
| 9338 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9339 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9340 | |
| 9341 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9342 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9343 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9344 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9345 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9346 | referrer); |
| 9347 | |
| 9348 | // The original referrer set on the request is expected to obey the referrer |
| 9349 | // policy and already be stripped to the origin; thus this test case just |
| 9350 | // checks that this policy doesn't cause the referrer to change when following |
| 9351 | // a redirect. |
| 9352 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9353 | referrer.GetOrigin()); |
| 9354 | |
| 9355 | VerifyReferrerAfterRedirect( |
| 9356 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9357 | |
| 9358 | // The original referrer set on the request is expected to obey the referrer |
| 9359 | // policy and already be stripped to the origin; thus this test case just |
| 9360 | // checks that this policy doesn't cause the referrer to change when following |
| 9361 | // a redirect. |
| 9362 | VerifyReferrerAfterRedirect( |
| 9363 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9364 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9365 | |
| 9366 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9367 | } |
| 9368 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9369 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9370 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9371 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9372 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9373 | |
| 9374 | VerifyReferrerAfterRedirect( |
| 9375 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9376 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9377 | |
| 9378 | VerifyReferrerAfterRedirect( |
| 9379 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9380 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9381 | |
| 9382 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9383 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9384 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9385 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9386 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9387 | referrer); |
| 9388 | |
| 9389 | // The original referrer set on the request is expected to obey the referrer |
| 9390 | // policy and already be stripped to the origin; thus this test case just |
| 9391 | // checks that this policy doesn't cause the referrer to change when following |
| 9392 | // a redirect. |
| 9393 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9394 | referrer.GetOrigin()); |
| 9395 | |
| 9396 | VerifyReferrerAfterRedirect( |
| 9397 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9398 | |
| 9399 | // The original referrer set on the request is expected to obey the referrer |
| 9400 | // policy and already be stripped to the origin; thus this test case just |
| 9401 | // checks that this policy doesn't cause the referrer to change when following |
| 9402 | // a redirect. |
| 9403 | VerifyReferrerAfterRedirect( |
| 9404 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9405 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9406 | |
| 9407 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9408 | } |
| 9409 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9410 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9411 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9412 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9413 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9414 | |
| 9415 | VerifyReferrerAfterRedirect( |
| 9416 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9417 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9418 | |
| 9419 | VerifyReferrerAfterRedirect( |
| 9420 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9421 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9422 | |
| 9423 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9424 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9425 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9426 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9427 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9428 | referrer); |
| 9429 | |
| 9430 | // The original referrer set on the request is expected to obey the referrer |
| 9431 | // policy and already be stripped to the origin; thus this test case just |
| 9432 | // checks that this policy doesn't cause the referrer to change when following |
| 9433 | // a redirect. |
| 9434 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9435 | referrer.GetOrigin()); |
| 9436 | |
| 9437 | VerifyReferrerAfterRedirect( |
| 9438 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9439 | |
| 9440 | // The original referrer set on the request is expected to obey the referrer |
| 9441 | // policy and already be stripped to the origin, though it should be |
| 9442 | // subsequently cleared during the downgrading redirect. |
| 9443 | VerifyReferrerAfterRedirect( |
| 9444 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9445 | referrer.GetOrigin(), GURL()); |
| 9446 | |
| 9447 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9448 | } |
| 9449 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9450 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9451 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9452 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9453 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9454 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9455 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9456 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9457 | |
| 9458 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9459 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9460 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9461 | }; |
| 9462 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9463 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9464 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9465 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9466 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9467 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9468 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9469 | TestDelegate d; |
| 9470 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9471 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9472 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9473 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9474 | r->Start(); |
| 9475 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9476 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9477 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9478 | |
| 9479 | EXPECT_EQ(1, d.response_started_count()); |
| 9480 | EXPECT_FALSE(d.received_data_before_response()); |
| 9481 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9482 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9483 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9484 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9485 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9486 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9487 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9488 | } |
| 9489 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9490 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9491 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9492 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9493 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9494 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9495 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9496 | |
| 9497 | bool err_allowed = true; |
| 9498 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9499 | TestDelegate d; |
| 9500 | { |
| 9501 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9502 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9503 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9504 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9505 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9506 | r->Start(); |
| 9507 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9508 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9509 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9510 | |
| 9511 | EXPECT_EQ(1, d.response_started_count()); |
| 9512 | EXPECT_FALSE(d.received_data_before_response()); |
| 9513 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9514 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9515 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9516 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9517 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9518 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9519 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9520 | } |
| 9521 | } |
| 9522 | } |
| 9523 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9524 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9525 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9526 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9527 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9528 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9529 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9530 | |
| 9531 | // Iterate from false to true, just so that we do the opposite of the |
| 9532 | // previous test in order to increase test coverage. |
| 9533 | bool err_allowed = false; |
| 9534 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9535 | TestDelegate d; |
| 9536 | { |
| 9537 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9538 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9539 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9540 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9541 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9542 | r->Start(); |
| 9543 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9544 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9545 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9546 | |
| 9547 | EXPECT_EQ(1, d.response_started_count()); |
| 9548 | EXPECT_FALSE(d.received_data_before_response()); |
| 9549 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9550 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9551 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9552 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9553 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9554 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9555 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9556 | } |
| 9557 | } |
| 9558 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9559 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9560 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9561 | // security state. (see http://crbug.com/550977). |
| 9562 | #if !defined(OS_IOS) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9563 | // This tests that a load of www.google.com with a certificate error sets |
| 9564 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 9565 | // the interstitial to be fatal. |
| 9566 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 9567 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 9568 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 9569 | #endif |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9570 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9571 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9572 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9573 | ASSERT_TRUE(test_server.Start()); |
| 9574 | |
| 9575 | // We require that the URL be www.google.com in order to pick up the |
| 9576 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 9577 | // 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] | 9578 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9579 | |
| 9580 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9581 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9582 | TestURLRequestContext context(true); |
| 9583 | context.set_network_delegate(&network_delegate); |
| 9584 | context.set_host_resolver(&host_resolver); |
| 9585 | TransportSecurityState transport_security_state; |
| 9586 | context.set_transport_security_state(&transport_security_state); |
| 9587 | context.Init(); |
| 9588 | |
| 9589 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9590 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9591 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9592 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9593 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9594 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9595 | r->Start(); |
| 9596 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9597 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9598 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9599 | |
| 9600 | EXPECT_EQ(1, d.response_started_count()); |
| 9601 | EXPECT_FALSE(d.received_data_before_response()); |
| 9602 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9603 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9604 | } |
| 9605 | |
| 9606 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9607 | // TransportSecurityState. |
| 9608 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 9609 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 9610 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 9611 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9612 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9613 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9614 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9615 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9616 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9617 | ASSERT_TRUE(test_server.Start()); |
| 9618 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9619 | // We require that the URL be www.google.com in order to pick up the static |
| 9620 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 9621 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 9622 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9623 | |
| 9624 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9625 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9626 | TestURLRequestContext context(true); |
| 9627 | context.set_network_delegate(&network_delegate); |
| 9628 | context.set_host_resolver(&host_resolver); |
| 9629 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9630 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9631 | TransportSecurityState::STSState static_sts_state; |
| 9632 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9633 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9634 | "www.google.com", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9635 | context.set_transport_security_state(&transport_security_state); |
| 9636 | context.Init(); |
| 9637 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9638 | TransportSecurityState::STSState dynamic_sts_state; |
| 9639 | TransportSecurityState::PKPState dynamic_pkp_state; |
| 9640 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState("www.google.com", |
| 9641 | &dynamic_sts_state)); |
| 9642 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState("www.google.com", |
| 9643 | &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9644 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9645 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9646 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9647 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9648 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9649 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9650 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9651 | r->Start(); |
| 9652 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9653 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9654 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9655 | |
| 9656 | EXPECT_EQ(1, d.response_started_count()); |
| 9657 | EXPECT_FALSE(d.received_data_before_response()); |
| 9658 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9659 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9660 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9661 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9662 | TransportSecurityState::STSState new_static_sts_state; |
| 9663 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9664 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9665 | "www.google.com", &new_static_sts_state, &new_static_pkp_state)); |
| 9666 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9667 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9668 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9669 | "www.google.com", &new_dynamic_sts_state)); |
| 9670 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9671 | "www.google.com", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9672 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9673 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9674 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9675 | static_sts_state.include_subdomains); |
| 9676 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9677 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9678 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9679 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9680 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9681 | } |
| 9682 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9683 | // Make sure HSTS preserves a POST request's method and body. |
| 9684 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9685 | static const char kData[] = "hello world"; |
| 9686 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9687 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9688 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9689 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9690 | ASSERT_TRUE(test_server.Start()); |
| 9691 | |
| 9692 | |
| 9693 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9694 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9695 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9696 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9697 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9698 | |
| 9699 | // Force https for www.somewhere.com. |
| 9700 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9701 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9702 | bool include_subdomains = false; |
| 9703 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9704 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9705 | |
| 9706 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9707 | |
| 9708 | TestURLRequestContext context(true); |
| 9709 | context.set_host_resolver(&host_resolver); |
| 9710 | context.set_transport_security_state(&transport_security_state); |
| 9711 | context.set_network_delegate(&network_delegate); |
| 9712 | context.Init(); |
| 9713 | |
| 9714 | TestDelegate d; |
| 9715 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9716 | // cause a certificate error. Ignore the error. |
| 9717 | d.set_allow_certificate_errors(true); |
| 9718 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9719 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9720 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9721 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9722 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9723 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9724 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9725 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9726 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9727 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9728 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9729 | EXPECT_EQ("https", req->url().scheme()); |
| 9730 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9731 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9732 | |
| 9733 | LoadTimingInfo load_timing_info; |
| 9734 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9735 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9736 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9737 | } |
| 9738 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9739 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9740 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9741 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9742 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9743 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9744 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9745 | ASSERT_TRUE(test_server.Start()); |
| 9746 | |
| 9747 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9748 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9749 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9750 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9751 | MockHostResolver host_resolver; |
| 9752 | |
| 9753 | TransportSecurityState transport_security_state; |
| 9754 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9755 | bool include_subdomains = false; |
| 9756 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9757 | |
| 9758 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9759 | |
| 9760 | MockCertVerifier cert_verifier; |
| 9761 | cert_verifier.set_default_result(OK); |
| 9762 | |
| 9763 | TestURLRequestContext context(true); |
| 9764 | context.set_host_resolver(&host_resolver); |
| 9765 | context.set_transport_security_state(&transport_security_state); |
| 9766 | context.set_network_delegate(&network_delegate); |
| 9767 | context.set_cert_verifier(&cert_verifier); |
| 9768 | context.Init(); |
| 9769 | |
| 9770 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9771 | test_server.host_port_pair().port())); |
| 9772 | url::Replacements<char> replacements; |
| 9773 | const char kNewScheme[] = "https"; |
| 9774 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9775 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9776 | |
| 9777 | TestDelegate d; |
| 9778 | // Quit on redirect to allow response header inspection upon redirect. |
| 9779 | d.set_quit_on_redirect(true); |
| 9780 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9781 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9782 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9783 | // Set Origin header to simulate a cross-origin request. |
| 9784 | HttpRequestHeaders request_headers; |
| 9785 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9786 | req->SetExtraRequestHeaders(request_headers); |
| 9787 | |
| 9788 | req->Start(); |
| 9789 | base::RunLoop().Run(); |
| 9790 | |
| 9791 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9792 | |
| 9793 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9794 | std::string redirect_location; |
| 9795 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 9796 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9797 | |
| 9798 | std::string received_cors_header; |
| 9799 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 9800 | &received_cors_header)); |
| 9801 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9802 | } |
| 9803 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9804 | namespace { |
| 9805 | |
| 9806 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 9807 | public: |
| 9808 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 9809 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9810 | void OnCertificateRequested(URLRequest* request, |
| 9811 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9812 | on_certificate_requested_count_++; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 9813 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9814 | } |
| 9815 | int on_certificate_requested_count() { |
| 9816 | return on_certificate_requested_count_; |
| 9817 | } |
| 9818 | private: |
| 9819 | int on_certificate_requested_count_; |
| 9820 | }; |
| 9821 | |
| 9822 | } // namespace |
| 9823 | |
| 9824 | // TODO(davidben): Test the rest of the code. Specifically, |
| 9825 | // - Filtering which certificates to select. |
| 9826 | // - Sending a certificate back. |
| 9827 | // - Getting a certificate request in an SSL renegotiation sending the |
| 9828 | // HTTP request. |
| 9829 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9830 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9831 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 9832 | ssl_config.client_cert_type = |
| 9833 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9834 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 9835 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9836 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9837 | ASSERT_TRUE(test_server.Start()); |
| 9838 | |
| 9839 | SSLClientAuthTestDelegate d; |
| 9840 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9841 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9842 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9843 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9844 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9845 | r->Start(); |
| 9846 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9847 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9848 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9849 | |
| 9850 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9851 | EXPECT_FALSE(d.received_data_before_response()); |
| 9852 | EXPECT_EQ(0, d.bytes_received()); |
| 9853 | |
| 9854 | // Send no certificate. |
| 9855 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 9856 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 9857 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9858 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9859 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9860 | |
| 9861 | EXPECT_EQ(1, d.response_started_count()); |
| 9862 | EXPECT_FALSE(d.received_data_before_response()); |
| 9863 | EXPECT_NE(0, d.bytes_received()); |
| 9864 | } |
| 9865 | } |
| 9866 | |
| 9867 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 9868 | // Test that we attempt a session resume when making two connections to the |
| 9869 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9870 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9871 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9872 | SpawnedTestServer test_server( |
| 9873 | SpawnedTestServer::TYPE_HTTPS, |
| 9874 | ssl_options, |
| 9875 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9876 | ASSERT_TRUE(test_server.Start()); |
| 9877 | |
| 9878 | SSLClientSocket::ClearSessionCache(); |
| 9879 | |
| 9880 | { |
| 9881 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9882 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9883 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9884 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9885 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9886 | r->Start(); |
| 9887 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9888 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9889 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9890 | |
| 9891 | EXPECT_EQ(1, d.response_started_count()); |
| 9892 | } |
| 9893 | |
| 9894 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 9895 | CloseAllConnections(); |
| 9896 | |
| 9897 | { |
| 9898 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9899 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9900 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9901 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9902 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9903 | r->Start(); |
| 9904 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9905 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9906 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9907 | |
| 9908 | // The response will look like; |
| 9909 | // insert abc |
| 9910 | // lookup abc |
| 9911 | // insert xyz |
| 9912 | // |
| 9913 | // With a newline at the end which makes the split think that there are |
| 9914 | // four lines. |
| 9915 | |
| 9916 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9917 | std::vector<std::string> lines = base::SplitString( |
| 9918 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9919 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 9920 | |
| 9921 | std::string session_id; |
| 9922 | |
| 9923 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9924 | std::vector<std::string> parts = base::SplitString( |
| 9925 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9926 | ASSERT_EQ(2u, parts.size()); |
| 9927 | if (i == 0) { |
| 9928 | EXPECT_EQ("insert", parts[0]); |
| 9929 | session_id = parts[1]; |
| 9930 | } else { |
| 9931 | EXPECT_EQ("lookup", parts[0]); |
| 9932 | EXPECT_EQ(session_id, parts[1]); |
| 9933 | } |
| 9934 | } |
| 9935 | } |
| 9936 | } |
| 9937 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9938 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 9939 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 9940 | // that exactly two different sessions were inserted, with no lookups etc. |
| 9941 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9942 | std::vector<std::string> lines = base::SplitString( |
| 9943 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9944 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 9945 | |
| 9946 | std::string session_id; |
| 9947 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9948 | std::vector<std::string> parts = base::SplitString( |
| 9949 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9950 | ASSERT_EQ(2u, parts.size()); |
| 9951 | EXPECT_EQ("insert", parts[0]); |
| 9952 | if (i == 0) { |
| 9953 | session_id = parts[1]; |
| 9954 | } else { |
| 9955 | EXPECT_NE(session_id, parts[1]); |
| 9956 | } |
| 9957 | } |
| 9958 | } |
| 9959 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9960 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 9961 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 9962 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9963 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9964 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9965 | SpawnedTestServer test_server( |
| 9966 | SpawnedTestServer::TYPE_HTTPS, |
| 9967 | ssl_options, |
| 9968 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9969 | ASSERT_TRUE(test_server.Start()); |
| 9970 | |
| 9971 | SSLClientSocket::ClearSessionCache(); |
| 9972 | |
| 9973 | { |
| 9974 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9975 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9976 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9977 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9978 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9979 | r->Start(); |
| 9980 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9981 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9982 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9983 | |
| 9984 | EXPECT_EQ(1, d.response_started_count()); |
| 9985 | } |
| 9986 | |
| 9987 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9988 | HttpNetworkSession::Context session_context; |
| 9989 | session_context.host_resolver = default_context_.host_resolver(); |
| 9990 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 9991 | session_context.transport_security_state = |
| 9992 | default_context_.transport_security_state(); |
| 9993 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9994 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9995 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
| 9996 | session_context.proxy_service = default_context_.proxy_service(); |
| 9997 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 9998 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9999 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10000 | session_context.http_server_properties = |
| 10001 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10002 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10003 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10004 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10005 | std::unique_ptr<HttpCache> cache( |
| 10006 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10007 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10008 | |
| 10009 | default_context_.set_http_transaction_factory(cache.get()); |
| 10010 | |
| 10011 | { |
| 10012 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10013 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10014 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10015 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10016 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10017 | r->Start(); |
| 10018 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10019 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10020 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10021 | |
| 10022 | // The response will look like; |
| 10023 | // insert abc |
| 10024 | // insert xyz |
| 10025 | // |
| 10026 | // With a newline at the end which makes the split think that there are |
| 10027 | // three lines. |
| 10028 | |
| 10029 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10030 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10031 | } |
| 10032 | } |
| 10033 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10034 | class HTTPSFallbackTest : public testing::Test { |
| 10035 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10036 | HTTPSFallbackTest() : context_(true) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10037 | ssl_config_service_ = new TestSSLConfigService( |
| 10038 | true /* check for EV */, false /* online revocation checking */, |
| 10039 | false /* require rev. checking for local anchors */, |
| 10040 | false /* token binding enabled */); |
| 10041 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10042 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10043 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10044 | |
| 10045 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10046 | TestSSLConfigService* ssl_config_service() { |
| 10047 | return ssl_config_service_.get(); |
| 10048 | } |
| 10049 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10050 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10051 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10052 | context_.Init(); |
| 10053 | delegate_.set_allow_certificate_errors(true); |
| 10054 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10055 | SpawnedTestServer test_server( |
| 10056 | SpawnedTestServer::TYPE_HTTPS, |
| 10057 | ssl_options, |
| 10058 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10059 | ASSERT_TRUE(test_server.Start()); |
| 10060 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10061 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10062 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10063 | request_->Start(); |
| 10064 | |
| 10065 | base::RunLoop().Run(); |
| 10066 | } |
| 10067 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10068 | void ExpectConnection(int version) { |
| 10069 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10070 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10071 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10072 | request_->ssl_info().connection_status)); |
| 10073 | } |
| 10074 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10075 | void ExpectFailure(int error) { |
| 10076 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10077 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10078 | } |
| 10079 | |
| 10080 | private: |
| 10081 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10082 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10083 | std::unique_ptr<URLRequest> request_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10084 | scoped_refptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10085 | }; |
| 10086 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10087 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10088 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10089 | SpawnedTestServer::SSLOptions ssl_options( |
| 10090 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10091 | ssl_options.tls_intolerant = |
| 10092 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10093 | |
| 10094 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10095 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10096 | } |
| 10097 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10098 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10099 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10100 | SpawnedTestServer::SSLOptions ssl_options( |
| 10101 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10102 | ssl_options.tls_intolerant = |
| 10103 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10104 | |
| 10105 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10106 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10107 | } |
| 10108 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10109 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10110 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10111 | SpawnedTestServer::SSLOptions ssl_options( |
| 10112 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10113 | ssl_options.tls_intolerant = |
| 10114 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10115 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10116 | |
| 10117 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10118 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10119 | } |
| 10120 | |
| 10121 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10122 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10123 | SpawnedTestServer::SSLOptions ssl_options( |
| 10124 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10125 | ssl_options.tls_intolerant = |
| 10126 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10127 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10128 | |
| 10129 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10130 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10131 | } |
| 10132 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10133 | class HTTPSSessionTest : public testing::Test { |
| 10134 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10135 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10136 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10137 | |
| 10138 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10139 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10140 | default_context_.Init(); |
| 10141 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10142 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10143 | |
| 10144 | protected: |
| 10145 | MockCertVerifier cert_verifier_; |
| 10146 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10147 | TestURLRequestContext default_context_; |
| 10148 | }; |
| 10149 | |
| 10150 | // Tests that session resumption is not attempted if an invalid certificate |
| 10151 | // is presented. |
| 10152 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10153 | SpawnedTestServer::SSLOptions ssl_options; |
| 10154 | ssl_options.record_resume = true; |
| 10155 | SpawnedTestServer test_server( |
| 10156 | SpawnedTestServer::TYPE_HTTPS, |
| 10157 | ssl_options, |
| 10158 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10159 | ASSERT_TRUE(test_server.Start()); |
| 10160 | |
| 10161 | SSLClientSocket::ClearSessionCache(); |
| 10162 | |
| 10163 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10164 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10165 | { |
| 10166 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10167 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10168 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10169 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10170 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10171 | r->Start(); |
| 10172 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10173 | |
| 10174 | base::RunLoop().Run(); |
| 10175 | |
| 10176 | EXPECT_EQ(1, d.response_started_count()); |
| 10177 | } |
| 10178 | |
| 10179 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10180 | CloseAllConnections(); |
| 10181 | |
| 10182 | // Now change the certificate to be acceptable (so that the response is |
| 10183 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10184 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10185 | { |
| 10186 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10187 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10188 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10189 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10190 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10191 | r->Start(); |
| 10192 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10193 | |
| 10194 | base::RunLoop().Run(); |
| 10195 | |
| 10196 | // The response will look like; |
| 10197 | // insert abc |
| 10198 | // insert xyz |
| 10199 | // |
| 10200 | // With a newline at the end which makes the split think that there are |
| 10201 | // three lines. |
| 10202 | // |
| 10203 | // If a session was presented (eg: a bug), then the response would look |
| 10204 | // like; |
| 10205 | // insert abc |
| 10206 | // lookup abc |
| 10207 | // insert xyz |
| 10208 | |
| 10209 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10210 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10211 | } |
| 10212 | } |
| 10213 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10214 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10215 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10216 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10217 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10218 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10219 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10220 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10221 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10222 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10223 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10224 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10225 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10226 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10227 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10228 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10229 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10230 | // This is the policy OID contained in the certificates that testserver |
| 10231 | // generates. |
| 10232 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10233 | |
| 10234 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10235 | public: |
| 10236 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10237 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10238 | ev_test_policy_( |
| 10239 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10240 | kOCSPTestCertFingerprint, |
| 10241 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10242 | } |
| 10243 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10244 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10245 | context_.SetCTPolicyEnforcer( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame^] | 10246 | std::make_unique<AllowAnyCertCTPolicyEnforcer>()); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10247 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10248 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10249 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10250 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10251 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10252 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10253 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10254 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10255 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10256 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10257 | EnsureNSSHttpIOInit(); |
| 10258 | #endif |
| 10259 | } |
| 10260 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10261 | void DoConnectionWithDelegate( |
| 10262 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10263 | TestDelegate* delegate, |
| 10264 | SSLInfo* out_ssl_info) { |
| 10265 | // Always overwrite |out_ssl_info|. |
| 10266 | out_ssl_info->Reset(); |
| 10267 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10268 | SpawnedTestServer test_server( |
| 10269 | SpawnedTestServer::TYPE_HTTPS, |
| 10270 | ssl_options, |
| 10271 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10272 | ASSERT_TRUE(test_server.Start()); |
| 10273 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10274 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10275 | std::unique_ptr<URLRequest> r( |
| 10276 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10277 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10278 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10279 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10280 | base::RunLoop().Run(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10281 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10282 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10283 | *out_ssl_info = r->ssl_info(); |
| 10284 | } |
| 10285 | |
| 10286 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10287 | CertStatus* out_cert_status) { |
| 10288 | // Always overwrite |out_cert_status|. |
| 10289 | *out_cert_status = 0; |
| 10290 | |
| 10291 | TestDelegate d; |
| 10292 | SSLInfo ssl_info; |
| 10293 | ASSERT_NO_FATAL_FAILURE( |
| 10294 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10295 | |
| 10296 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10297 | } |
| 10298 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10299 | ~HTTPSOCSPTest() override { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10300 | #if defined(USE_NSS_CERTS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10301 | ShutdownNSSHttpIO(); |
| 10302 | #endif |
| 10303 | } |
| 10304 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10305 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10306 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 10307 | public: |
| 10308 | AllowAnyCertCTPolicyEnforcer() = default; |
| 10309 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 10310 | |
| 10311 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 10312 | X509Certificate* cert, |
| 10313 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10314 | const NetLogWithSource& net_log) override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10315 | return ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS; |
| 10316 | } |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10317 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10318 | // SetupContext configures the URLRequestContext that will be used for making |
| 10319 | // connetions to testserver. This can be overridden in test subclasses for |
| 10320 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10321 | virtual void SetupContext() { |
| 10322 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 10323 | true /* check for EV */, true /* online revocation checking */, |
| 10324 | false /* require rev. checking for local |
| 10325 | anchors */, |
| 10326 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10327 | } |
| 10328 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10329 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10330 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10331 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10332 | }; |
| 10333 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10334 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10335 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10336 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10337 | // have that ability on other platforms. |
| 10338 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10339 | #else |
| 10340 | return 0; |
| 10341 | #endif |
| 10342 | } |
| 10343 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10344 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10345 | // operating system supports revocation checking and can distinguish between |
| 10346 | // situations where a given certificate lacks any revocation information (eg: |
| 10347 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10348 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10349 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10350 | // skipped. |
| 10351 | static bool SystemSupportsHardFailRevocationChecking() { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10352 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10353 | return true; |
| 10354 | #else |
| 10355 | return false; |
| 10356 | #endif |
| 10357 | } |
| 10358 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10359 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10360 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10361 | // several tests are effected because our testing EV certificate won't be |
| 10362 | // recognised as EV. |
| 10363 | static bool SystemUsesChromiumEVMetadata() { |
davidben | 965721b | 2017-05-03 19:40:31 | [diff] [blame] | 10364 | #if defined(OS_ANDROID) |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 10365 | // On Android, we use the system to tell us whether a certificate is EV or not |
| 10366 | // and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10367 | return false; |
| 10368 | #else |
| 10369 | return true; |
| 10370 | #endif |
| 10371 | } |
| 10372 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10373 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10374 | // check failure as a result of checking a test EV cert, which will not |
| 10375 | // actually trigger an online revocation check on some platforms. |
| 10376 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10377 | if (SystemUsesChromiumEVMetadata()) { |
| 10378 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10379 | } else { |
| 10380 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10381 | // be enabled, and thus there will not be a revocation check to fail. |
| 10382 | return 0u; |
| 10383 | } |
| 10384 | } |
| 10385 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10386 | static bool SystemSupportsOCSP() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 10387 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10388 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 10389 | return false; |
| 10390 | #else |
| 10391 | return true; |
| 10392 | #endif |
| 10393 | } |
| 10394 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10395 | static bool SystemSupportsOCSPStapling() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 10396 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10397 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10398 | #else |
| 10399 | return false; |
| 10400 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10401 | } |
| 10402 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10403 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10404 | if (!SystemSupportsOCSP()) { |
| 10405 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10406 | return; |
| 10407 | } |
| 10408 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10409 | SpawnedTestServer::SSLOptions ssl_options( |
| 10410 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10411 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10412 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10413 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10414 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10415 | |
| 10416 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10417 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10418 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10419 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10420 | |
| 10421 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10422 | } |
| 10423 | |
| 10424 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10425 | if (!SystemSupportsOCSP()) { |
| 10426 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10427 | return; |
| 10428 | } |
| 10429 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10430 | SpawnedTestServer::SSLOptions ssl_options( |
| 10431 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10432 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10433 | |
| 10434 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10435 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10436 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10437 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10438 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10439 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10440 | } |
| 10441 | |
| 10442 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10443 | if (!SystemSupportsOCSP()) { |
| 10444 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10445 | return; |
| 10446 | } |
| 10447 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10448 | SpawnedTestServer::SSLOptions ssl_options( |
| 10449 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10450 | ssl_options.ocsp_status = |
| 10451 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10452 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10453 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10454 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10455 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10456 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10457 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10458 | |
| 10459 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10460 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10461 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10462 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10463 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10464 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10465 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10466 | LOG(WARNING) |
| 10467 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10468 | return; |
| 10469 | } |
| 10470 | |
| 10471 | SpawnedTestServer::SSLOptions ssl_options( |
| 10472 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10473 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10474 | ssl_options.staple_ocsp_response = true; |
| 10475 | ssl_options.ocsp_server_unavailable = true; |
| 10476 | |
| 10477 | CertStatus cert_status; |
| 10478 | DoConnection(ssl_options, &cert_status); |
| 10479 | |
| 10480 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10481 | |
| 10482 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10483 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10484 | |
| 10485 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10486 | } |
| 10487 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10488 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 10489 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10490 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 10491 | #else |
| 10492 | #define MAYBE_RevokedStapled RevokedStapled |
| 10493 | #endif |
| 10494 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10495 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10496 | LOG(WARNING) |
| 10497 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10498 | return; |
| 10499 | } |
| 10500 | |
| 10501 | SpawnedTestServer::SSLOptions ssl_options( |
| 10502 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10503 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10504 | ssl_options.staple_ocsp_response = true; |
| 10505 | ssl_options.ocsp_server_unavailable = true; |
| 10506 | |
| 10507 | CertStatus cert_status; |
| 10508 | DoConnection(ssl_options, &cert_status); |
| 10509 | |
| 10510 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10511 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10512 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10513 | } |
| 10514 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10515 | TEST_F(HTTPSOCSPTest, ExpectStapleReportSentOnMissing) { |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 10516 | #if !BUILDFLAG(INCLUDE_TRANSPORT_SECURITY_STATE_PRELOAD_LIST) |
| 10517 | SetTransportSecurityStateSourceForTesting(&test0::kHSTSSource); |
| 10518 | #endif |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10519 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10520 | https_test_server.SetSSLConfig( |
| 10521 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10522 | https_test_server.ServeFilesFromSourceDirectory( |
| 10523 | base::FilePath(kTestFilePath)); |
| 10524 | ASSERT_TRUE(https_test_server.Start()); |
| 10525 | |
| 10526 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10527 | // but not provide any OCSP information. |
| 10528 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10529 | ASSERT_TRUE(cert); |
| 10530 | MockCertVerifier cert_verifier; |
| 10531 | CertVerifyResult verify_result; |
| 10532 | verify_result.verified_cert = cert; |
| 10533 | verify_result.is_issued_by_known_root = true; |
| 10534 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10535 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10536 | |
| 10537 | // Catch the Expect-Staple report. |
| 10538 | TransportSecurityState transport_security_state; |
| 10539 | MockCertificateReportSender mock_report_sender; |
| 10540 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10541 | |
| 10542 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10543 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10544 | MockHostResolver host_resolver; |
| 10545 | TestNetworkDelegate network_delegate; |
| 10546 | TestURLRequestContext context(true); |
| 10547 | context.set_host_resolver(&host_resolver); |
| 10548 | context.set_transport_security_state(&transport_security_state); |
| 10549 | context.set_network_delegate(&network_delegate); |
| 10550 | context.set_cert_verifier(&cert_verifier); |
| 10551 | context.Init(); |
| 10552 | |
| 10553 | // Now send a request to trigger the violation. |
| 10554 | TestDelegate d; |
| 10555 | GURL url = https_test_server.GetURL("/"); |
| 10556 | GURL::Replacements replace_host; |
| 10557 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10558 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10559 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10560 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10561 | violating_request->Start(); |
| 10562 | base::RunLoop().Run(); |
| 10563 | |
| 10564 | // Confirm a report was sent. |
| 10565 | EXPECT_FALSE(mock_report_sender.latest_report().empty()); |
| 10566 | EXPECT_EQ(GURL(kExpectStapleReportURI), |
| 10567 | mock_report_sender.latest_report_uri()); |
| 10568 | } |
| 10569 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10570 | // Tests that Expect-Staple reports are not sent for connections on which there |
| 10571 | // is a certificate error. |
| 10572 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnMissingWithCertError) { |
| 10573 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10574 | https_test_server.SetSSLConfig( |
| 10575 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10576 | https_test_server.ServeFilesFromSourceDirectory( |
| 10577 | base::FilePath(kTestFilePath)); |
| 10578 | ASSERT_TRUE(https_test_server.Start()); |
| 10579 | |
| 10580 | // Set up a MockCertVerifier to report an error for the certificate |
| 10581 | // and indicate that there was no stapled OCSP response. |
| 10582 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10583 | ASSERT_TRUE(cert); |
| 10584 | MockCertVerifier cert_verifier; |
| 10585 | CertVerifyResult verify_result; |
| 10586 | verify_result.cert_status = CERT_STATUS_DATE_INVALID; |
| 10587 | verify_result.verified_cert = cert; |
| 10588 | verify_result.is_issued_by_known_root = true; |
| 10589 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10590 | cert_verifier.AddResultForCert(cert.get(), verify_result, |
| 10591 | ERR_CERT_DATE_INVALID); |
| 10592 | |
| 10593 | // Set up a mock report sender so that the test can check that an |
| 10594 | // Expect-Staple report is not sent. |
| 10595 | TransportSecurityState transport_security_state; |
| 10596 | MockCertificateReportSender mock_report_sender; |
| 10597 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10598 | |
| 10599 | TestNetworkDelegate network_delegate; |
| 10600 | TestURLRequestContext context(true); |
| 10601 | |
| 10602 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10603 | MockHostResolver host_resolver; |
| 10604 | context.set_host_resolver(&host_resolver); |
| 10605 | |
| 10606 | context.set_transport_security_state(&transport_security_state); |
| 10607 | context.set_network_delegate(&network_delegate); |
| 10608 | context.set_cert_verifier(&cert_verifier); |
| 10609 | context.Init(); |
| 10610 | |
| 10611 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10612 | // |verify_result| used with the |cert_verifier| will indicate a certificate |
| 10613 | // error, an Expect-Staple report should not be sent. |
| 10614 | TestDelegate d; |
| 10615 | GURL url = https_test_server.GetURL("/"); |
| 10616 | GURL::Replacements replace_host; |
| 10617 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10618 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10619 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10620 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10621 | violating_request->Start(); |
| 10622 | base::RunLoop().Run(); |
| 10623 | |
| 10624 | // Confirm a report was not sent. |
| 10625 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10626 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10627 | } |
| 10628 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10629 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnValid) { |
| 10630 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10631 | https_test_server.SetSSLConfig( |
| 10632 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10633 | https_test_server.ServeFilesFromSourceDirectory( |
| 10634 | base::FilePath(kTestFilePath)); |
| 10635 | ASSERT_TRUE(https_test_server.Start()); |
| 10636 | |
| 10637 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10638 | // and provide GOOD revocation status. |
| 10639 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10640 | ASSERT_TRUE(cert); |
| 10641 | MockCertVerifier cert_verifier; |
| 10642 | CertVerifyResult verify_result; |
| 10643 | verify_result.verified_cert = cert; |
| 10644 | verify_result.is_issued_by_known_root = true; |
| 10645 | verify_result.ocsp_result.response_status = OCSPVerifyResult::PROVIDED; |
| 10646 | verify_result.ocsp_result.revocation_status = OCSPRevocationStatus::GOOD; |
| 10647 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10648 | |
| 10649 | // Catch the Expect-Staple report. |
| 10650 | TransportSecurityState transport_security_state; |
| 10651 | MockCertificateReportSender mock_report_sender; |
| 10652 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10653 | |
| 10654 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10655 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10656 | MockHostResolver host_resolver; |
| 10657 | TestNetworkDelegate network_delegate; |
| 10658 | TestURLRequestContext context(true); |
| 10659 | context.set_host_resolver(&host_resolver); |
| 10660 | context.set_transport_security_state(&transport_security_state); |
| 10661 | context.set_network_delegate(&network_delegate); |
| 10662 | context.set_cert_verifier(&cert_verifier); |
| 10663 | context.Init(); |
| 10664 | |
| 10665 | // This request should not not trigger an Expect-Staple violation. |
| 10666 | TestDelegate d; |
| 10667 | GURL url = https_test_server.GetURL("/"); |
| 10668 | GURL::Replacements replace_host; |
| 10669 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10670 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10671 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10672 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10673 | ok_request->Start(); |
| 10674 | base::RunLoop().Run(); |
| 10675 | |
| 10676 | // Check that no report was sent. |
| 10677 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10678 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10679 | } |
| 10680 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10681 | // Tests that an Expect-Staple report is not sent when OCSP details are not |
| 10682 | // checked on the connection. |
| 10683 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnNotChecked) { |
| 10684 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10685 | https_test_server.SetSSLConfig( |
| 10686 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10687 | https_test_server.ServeFilesFromSourceDirectory( |
| 10688 | base::FilePath(kTestFilePath)); |
| 10689 | ASSERT_TRUE(https_test_server.Start()); |
| 10690 | |
| 10691 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10692 | // and set |ocsp_result| to indicate that OCSP stapling details were not |
| 10693 | // checked on the connection. |
| 10694 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10695 | ASSERT_TRUE(cert); |
| 10696 | MockCertVerifier cert_verifier; |
| 10697 | CertVerifyResult verify_result; |
| 10698 | verify_result.verified_cert = cert; |
| 10699 | verify_result.is_issued_by_known_root = true; |
| 10700 | verify_result.ocsp_result.response_status = OCSPVerifyResult::NOT_CHECKED; |
| 10701 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10702 | |
| 10703 | // Set up a mock report sender so that the test can check that an |
| 10704 | // Expect-Staple report is not sent. |
| 10705 | TransportSecurityState transport_security_state; |
| 10706 | MockCertificateReportSender mock_report_sender; |
| 10707 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10708 | |
| 10709 | TestNetworkDelegate network_delegate; |
| 10710 | TestURLRequestContext context(true); |
| 10711 | |
| 10712 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10713 | MockHostResolver host_resolver; |
| 10714 | context.set_host_resolver(&host_resolver); |
| 10715 | |
| 10716 | context.set_transport_security_state(&transport_security_state); |
| 10717 | context.set_network_delegate(&network_delegate); |
| 10718 | context.set_cert_verifier(&cert_verifier); |
| 10719 | context.Init(); |
| 10720 | |
| 10721 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10722 | // |verify_result| used with the |cert_verifier| will indicate that OCSP |
| 10723 | // stapling details were not checked on the connection, an Expect-Staple |
| 10724 | // report should not be sent. |
| 10725 | TestDelegate d; |
| 10726 | GURL url = https_test_server.GetURL("/"); |
| 10727 | GURL::Replacements replace_host; |
| 10728 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10729 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10730 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10731 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10732 | ok_request->Start(); |
| 10733 | base::RunLoop().Run(); |
| 10734 | |
| 10735 | // Check that no report was sent. |
| 10736 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10737 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10738 | } |
| 10739 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10740 | static const struct OCSPVerifyTestData { |
| 10741 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 10742 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 10743 | OCSPVerifyResult::ResponseStatus response_status; |
| 10744 | bool has_revocation_status; |
| 10745 | OCSPRevocationStatus cert_status; |
| 10746 | } kOCSPVerifyData[] = { |
| 10747 | |
| 10748 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10749 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10750 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10751 | OCSPVerifyResult::PROVIDED, |
| 10752 | true, |
| 10753 | OCSPRevocationStatus::GOOD}, |
| 10754 | |
| 10755 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10756 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10757 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10758 | OCSPVerifyResult::INVALID_DATE, |
| 10759 | false, |
| 10760 | OCSPRevocationStatus::UNKNOWN}, |
| 10761 | |
| 10762 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10763 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10764 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10765 | OCSPVerifyResult::INVALID_DATE, |
| 10766 | false, |
| 10767 | OCSPRevocationStatus::UNKNOWN}, |
| 10768 | |
| 10769 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10770 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10771 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10772 | OCSPVerifyResult::INVALID_DATE, |
| 10773 | false, |
| 10774 | OCSPRevocationStatus::UNKNOWN}, |
| 10775 | |
| 10776 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10777 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10778 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10779 | OCSPVerifyResult::INVALID_DATE, |
| 10780 | false, |
| 10781 | OCSPRevocationStatus::UNKNOWN}, |
| 10782 | |
| 10783 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 10784 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10785 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10786 | OCSPVerifyResult::ERROR_RESPONSE, |
| 10787 | false, |
| 10788 | OCSPRevocationStatus::UNKNOWN}, |
| 10789 | |
| 10790 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 10791 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10792 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10793 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 10794 | false, |
| 10795 | OCSPRevocationStatus::UNKNOWN}, |
| 10796 | |
| 10797 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 10798 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10799 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10800 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 10801 | false, |
| 10802 | OCSPRevocationStatus::UNKNOWN}, |
| 10803 | |
| 10804 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10805 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10806 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10807 | OCSPVerifyResult::INVALID_DATE, |
| 10808 | false, |
| 10809 | OCSPRevocationStatus::UNKNOWN}, |
| 10810 | |
| 10811 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10812 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10813 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10814 | OCSPVerifyResult::PROVIDED, |
| 10815 | true, |
| 10816 | OCSPRevocationStatus::UNKNOWN}, |
| 10817 | |
| 10818 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10819 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10820 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10821 | OCSPVerifyResult::INVALID_DATE, |
| 10822 | false, |
| 10823 | OCSPRevocationStatus::UNKNOWN}, |
| 10824 | |
| 10825 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10826 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10827 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10828 | OCSPVerifyResult::INVALID_DATE, |
| 10829 | false, |
| 10830 | OCSPRevocationStatus::UNKNOWN}, |
| 10831 | |
| 10832 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10833 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10834 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 10835 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10836 | false, |
| 10837 | OCSPRevocationStatus::UNKNOWN}, |
| 10838 | |
| 10839 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10840 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10841 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10842 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10843 | false, |
| 10844 | OCSPRevocationStatus::UNKNOWN}, |
| 10845 | |
| 10846 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10847 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10848 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10849 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10850 | false, |
| 10851 | OCSPRevocationStatus::UNKNOWN}, |
| 10852 | |
| 10853 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10854 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10855 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10856 | OCSPVerifyResult::PROVIDED, |
| 10857 | true, |
| 10858 | OCSPRevocationStatus::REVOKED}, |
| 10859 | |
| 10860 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10861 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10862 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10863 | OCSPVerifyResult::INVALID_DATE, |
| 10864 | false, |
| 10865 | OCSPRevocationStatus::UNKNOWN}, |
| 10866 | |
| 10867 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10868 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10869 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10870 | OCSPVerifyResult::INVALID_DATE, |
| 10871 | false, |
| 10872 | OCSPRevocationStatus::UNKNOWN}, |
| 10873 | |
| 10874 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10875 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10876 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10877 | OCSPVerifyResult::PROVIDED, |
| 10878 | true, |
| 10879 | OCSPRevocationStatus::GOOD}, |
| 10880 | |
| 10881 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10882 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10883 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10884 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10885 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10886 | OCSPVerifyResult::PROVIDED, |
| 10887 | true, |
| 10888 | OCSPRevocationStatus::GOOD}, |
| 10889 | |
| 10890 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10891 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10892 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10893 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10894 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10895 | OCSPVerifyResult::PROVIDED, |
| 10896 | true, |
| 10897 | OCSPRevocationStatus::GOOD}, |
| 10898 | |
| 10899 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10900 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10901 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10902 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10903 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10904 | OCSPVerifyResult::PROVIDED, |
| 10905 | true, |
| 10906 | OCSPRevocationStatus::GOOD}, |
| 10907 | |
| 10908 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10909 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10910 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10911 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10912 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10913 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10914 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10915 | OCSPVerifyResult::INVALID_DATE, |
| 10916 | false, |
| 10917 | OCSPRevocationStatus::UNKNOWN}, |
| 10918 | |
| 10919 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10920 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10921 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10922 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10923 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10924 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10925 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10926 | OCSPVerifyResult::PROVIDED, |
| 10927 | true, |
| 10928 | OCSPRevocationStatus::REVOKED}, |
| 10929 | |
| 10930 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10931 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10932 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10933 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10934 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10935 | OCSPVerifyResult::PROVIDED, |
| 10936 | true, |
| 10937 | OCSPRevocationStatus::UNKNOWN}, |
| 10938 | |
| 10939 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10940 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10941 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10942 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10943 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10944 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10945 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10946 | OCSPVerifyResult::PROVIDED, |
| 10947 | true, |
| 10948 | OCSPRevocationStatus::UNKNOWN}, |
| 10949 | |
| 10950 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10951 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10952 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10953 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10954 | false, |
| 10955 | OCSPRevocationStatus::UNKNOWN}, |
| 10956 | |
| 10957 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10958 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10959 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10960 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10961 | false, |
| 10962 | OCSPRevocationStatus::UNKNOWN}, |
| 10963 | |
| 10964 | }; |
| 10965 | |
| 10966 | class HTTPSOCSPVerifyTest |
| 10967 | : public HTTPSOCSPTest, |
| 10968 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 10969 | |
| 10970 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 10971 | SpawnedTestServer::SSLOptions ssl_options( |
| 10972 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10973 | OCSPVerifyTestData test = GetParam(); |
| 10974 | |
| 10975 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 10976 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 10977 | ssl_options.staple_ocsp_response = true; |
| 10978 | |
| 10979 | SSLInfo ssl_info; |
| 10980 | OCSPErrorTestDelegate delegate; |
| 10981 | ASSERT_NO_FATAL_FAILURE( |
| 10982 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 10983 | |
| 10984 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 10985 | // URLRequest caches certificate errors. |
| 10986 | if (delegate.have_certificate_errors()) { |
| 10987 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 10988 | ssl_info = delegate.ssl_info(); |
| 10989 | } |
| 10990 | |
| 10991 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 10992 | |
| 10993 | if (test.has_revocation_status) |
| 10994 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 10995 | } |
| 10996 | |
| 10997 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 10998 | HTTPSOCSPVerifyTest, |
| 10999 | testing::ValuesIn(kOCSPVerifyData)); |
| 11000 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11001 | static bool SystemSupportsAIA() { |
| 11002 | #if defined(OS_ANDROID) |
| 11003 | return false; |
| 11004 | #else |
| 11005 | return true; |
| 11006 | #endif |
| 11007 | } |
| 11008 | |
| 11009 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11010 | public: |
| 11011 | void SetupContext() override { |
| 11012 | context_.set_ssl_config_service(new TestSSLConfigService( |
| 11013 | false /* check for EV */, false /* online revocation checking */, |
| 11014 | false /* require rev. checking for local anchors */, |
| 11015 | false /* token binding enabled */)); |
| 11016 | } |
| 11017 | }; |
| 11018 | |
| 11019 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11020 | SpawnedTestServer::SSLOptions ssl_options( |
| 11021 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11022 | SpawnedTestServer test_server( |
| 11023 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11024 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11025 | ASSERT_TRUE(test_server.Start()); |
| 11026 | |
| 11027 | TestDelegate d; |
| 11028 | d.set_allow_certificate_errors(true); |
| 11029 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11030 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11032 | |
| 11033 | r->Start(); |
| 11034 | EXPECT_TRUE(r->is_pending()); |
| 11035 | |
| 11036 | base::RunLoop().Run(); |
| 11037 | |
| 11038 | EXPECT_EQ(1, d.response_started_count()); |
| 11039 | |
| 11040 | CertStatus cert_status = r->ssl_info().cert_status; |
| 11041 | if (SystemSupportsAIA()) { |
| 11042 | EXPECT_EQ(OK, d.request_status()); |
| 11043 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11044 | ASSERT_TRUE(r->ssl_info().cert); |
| 11045 | EXPECT_EQ(2u, r->ssl_info().cert->GetIntermediateCertificates().size()); |
| 11046 | } else { |
| 11047 | EXPECT_EQ(CERT_STATUS_AUTHORITY_INVALID, |
| 11048 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11049 | } |
| 11050 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
| 11051 | EXPECT_EQ( |
| 11052 | 0u, r->ssl_info().unverified_cert->GetIntermediateCertificates().size()); |
| 11053 | } |
| 11054 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11055 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11056 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11057 | void SetupContext() override { |
| 11058 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11059 | false /* check for EV */, false /* online revocation checking */, |
| 11060 | true /* require rev. checking for local |
| 11061 | anchors */, |
| 11062 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11063 | } |
| 11064 | }; |
| 11065 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11066 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11067 | if (!SystemSupportsOCSP()) { |
| 11068 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11069 | return; |
| 11070 | } |
| 11071 | |
| 11072 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11073 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11074 | << "revocation checking"; |
| 11075 | return; |
| 11076 | } |
| 11077 | |
| 11078 | SpawnedTestServer::SSLOptions ssl_options( |
| 11079 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11080 | ssl_options.ocsp_status = |
| 11081 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11082 | |
| 11083 | CertStatus cert_status; |
| 11084 | DoConnection(ssl_options, &cert_status); |
| 11085 | |
| 11086 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 11087 | cert_status & CERT_STATUS_REVOKED); |
| 11088 | |
| 11089 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11090 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11091 | } |
| 11092 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11093 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11094 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11095 | void SetupContext() override { |
| 11096 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11097 | true /* check for EV */, false /* online revocation checking */, |
| 11098 | false /* require rev. checking for local |
| 11099 | anchors */, |
| 11100 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11101 | } |
| 11102 | }; |
| 11103 | |
| 11104 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11105 | if (!SystemSupportsOCSP()) { |
| 11106 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11107 | return; |
| 11108 | } |
| 11109 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11110 | SpawnedTestServer::SSLOptions ssl_options( |
| 11111 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11112 | ssl_options.ocsp_status = |
| 11113 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11114 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11115 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11116 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11117 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11118 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11119 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11120 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11121 | |
| 11122 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11123 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11124 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11125 | } |
| 11126 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11127 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11128 | if (!SystemSupportsOCSP()) { |
| 11129 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11130 | return; |
| 11131 | } |
| 11132 | |
| 11133 | SpawnedTestServer::SSLOptions ssl_options( |
| 11134 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11135 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11136 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11137 | |
| 11138 | CertStatus cert_status; |
| 11139 | DoConnection(ssl_options, &cert_status); |
| 11140 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11141 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11142 | // possible to determine whether the check failed because of actual |
| 11143 | // revocation or because there was an OCSP failure. |
| 11144 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11145 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11146 | #else |
| 11147 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11148 | #endif |
| 11149 | |
| 11150 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11151 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11152 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11153 | } |
| 11154 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11155 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11156 | if (!SystemSupportsOCSP()) { |
| 11157 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11158 | return; |
| 11159 | } |
| 11160 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11161 | SpawnedTestServer::SSLOptions ssl_options( |
| 11162 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11163 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11164 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 11165 | |
| 11166 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11167 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11168 | |
| 11169 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11170 | |
| 11171 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11172 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11173 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11174 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11175 | } |
| 11176 | |
| 11177 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11178 | if (!SystemSupportsOCSP()) { |
| 11179 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11180 | return; |
| 11181 | } |
| 11182 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11183 | SpawnedTestServer::SSLOptions ssl_options( |
| 11184 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11185 | ssl_options.ocsp_status = |
| 11186 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11187 | SSLConfigService::SetCRLSet( |
| 11188 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11189 | |
| 11190 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11191 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11192 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11193 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11194 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11195 | |
| 11196 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11197 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11198 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11199 | } |
| 11200 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11201 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11202 | if (!SystemSupportsOCSP()) { |
| 11203 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11204 | return; |
| 11205 | } |
| 11206 | |
| 11207 | SpawnedTestServer::SSLOptions ssl_options( |
| 11208 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11209 | ssl_options.ocsp_status = |
| 11210 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11211 | SSLConfigService::SetCRLSet( |
| 11212 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 11213 | false, &kOCSPTestCertSPKI, ""))); |
| 11214 | |
| 11215 | CertStatus cert_status; |
| 11216 | DoConnection(ssl_options, &cert_status); |
| 11217 | |
| 11218 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11219 | // revocation check for EV. |
| 11220 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11221 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11222 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11223 | EXPECT_FALSE( |
| 11224 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11225 | } |
| 11226 | |
| 11227 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11228 | if (!SystemSupportsOCSP()) { |
| 11229 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11230 | return; |
| 11231 | } |
| 11232 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11233 | SpawnedTestServer::SSLOptions ssl_options( |
| 11234 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11235 | ssl_options.ocsp_status = |
| 11236 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11237 | SSLConfigService::SetCRLSet( |
| 11238 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 11239 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11240 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11241 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11242 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11243 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11244 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11245 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11246 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11247 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11248 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11249 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11250 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11251 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11252 | } |
| 11253 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11254 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 11255 | // Test that when EV verification is requested, but online revocation |
| 11256 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 11257 | // no revocation checking actually happens. |
| 11258 | if (!SystemSupportsOCSP()) { |
| 11259 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11260 | return; |
| 11261 | } |
| 11262 | |
| 11263 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11264 | // checking (as per the user preference) |
| 11265 | ev_test_policy_.reset(); |
| 11266 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11267 | SpawnedTestServer::SSLOptions ssl_options( |
| 11268 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11269 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11270 | SSLConfigService::SetCRLSet( |
| 11271 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11272 | |
| 11273 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11274 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11275 | |
| 11276 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11277 | |
| 11278 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11279 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11280 | } |
| 11281 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11282 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11283 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11284 | void SetupContext() override { |
| 11285 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11286 | false /* check for EV */, false /* online revocation checking */, |
| 11287 | false /* require rev. checking for local |
| 11288 | anchors */, |
| 11289 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11290 | } |
| 11291 | }; |
| 11292 | |
| 11293 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11294 | SpawnedTestServer::SSLOptions ssl_options( |
| 11295 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11296 | ssl_options.ocsp_status = |
| 11297 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11298 | SSLConfigService::SetCRLSet( |
| 11299 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 11300 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11301 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11302 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11303 | |
| 11304 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11305 | // we don't fall back to online revocation checks. |
| 11306 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11307 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11308 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11309 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11310 | |
| 11311 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 11312 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11313 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11314 | return; |
| 11315 | #endif |
| 11316 | |
| 11317 | SpawnedTestServer::SSLOptions ssl_options( |
| 11318 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11319 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11320 | ssl_options.cert_serial = 10; |
| 11321 | SSLConfigService::SetCRLSet( |
| 11322 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 11323 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 11324 | |
| 11325 | CertStatus cert_status = 0; |
| 11326 | DoConnection(ssl_options, &cert_status); |
| 11327 | |
| 11328 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11329 | // reflected without online revocation checking. |
| 11330 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11331 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11332 | EXPECT_FALSE( |
| 11333 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11334 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11335 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11336 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11337 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11338 | // These tests aren't passing on Android. Either the RemoteTestServer isn't |
| 11339 | // starting up successfully, or it can't access the test files. |
| 11340 | // TODO(mmenke): Fix this. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11341 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11342 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11343 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11344 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11345 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11346 | // Can't use |default_context_|'s HostResolver to set up the |
| 11347 | // FTPTransactionFactory because it hasn't been created yet. |
| 11348 | default_context_.set_host_resolver(&host_resolver_); |
| 11349 | } |
| 11350 | |
| 11351 | // URLRequestTest interface: |
| 11352 | void SetUpFactory() override { |
| 11353 | // Add FTP support to the default URLRequestContext. |
| 11354 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11355 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11356 | } |
| 11357 | |
| 11358 | std::string GetTestFileContents() { |
| 11359 | base::FilePath path; |
| 11360 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 11361 | path = path.Append(kTestFilePath); |
| 11362 | path = path.AppendASCII(kFtpTestFile); |
| 11363 | std::string contents; |
| 11364 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11365 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11366 | } |
| 11367 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11368 | protected: |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11369 | // Note that this is destroyed before the FtpProtocolHandler that references |
| 11370 | // it, which is owned by the parent class. Since no requests are made during |
| 11371 | // teardown, this works, though it's not great. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11372 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11373 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11374 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11375 | }; |
| 11376 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11377 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11378 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11379 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11380 | |
| 11381 | TestDelegate d; |
| 11382 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11383 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11384 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11385 | r->Start(); |
| 11386 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11387 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11388 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11389 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11390 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11391 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11392 | } |
| 11393 | } |
| 11394 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11395 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11396 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11397 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11398 | TestDelegate d; |
| 11399 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11400 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11401 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11402 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11403 | r->Start(); |
| 11404 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11405 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11406 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11408 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11409 | EXPECT_EQ(1, d.response_started_count()); |
| 11410 | EXPECT_FALSE(d.received_data_before_response()); |
| 11411 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11412 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11413 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11414 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11415 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11416 | } |
| 11417 | } |
| 11418 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11419 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11420 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11421 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11422 | TestDelegate d; |
| 11423 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11424 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11425 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11426 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11427 | r->Start(); |
| 11428 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11429 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11430 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11432 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11433 | EXPECT_EQ(1, d.response_started_count()); |
| 11434 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11435 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11436 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11437 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11438 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11439 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11440 | } |
| 11441 | } |
| 11442 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11443 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11444 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11445 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11446 | TestDelegate d; |
| 11447 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11448 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11449 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11450 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11451 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11452 | r->Start(); |
| 11453 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11454 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11455 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11457 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11458 | EXPECT_EQ(1, d.response_started_count()); |
| 11459 | EXPECT_FALSE(d.received_data_before_response()); |
| 11460 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11461 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11462 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11463 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11464 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11465 | |
| 11466 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11467 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11468 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11469 | } |
| 11470 | } |
| 11471 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11472 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11473 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11474 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11475 | TestDelegate d; |
| 11476 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11477 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11478 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11479 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11480 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11481 | r->Start(); |
| 11482 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11483 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11484 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11485 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11486 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11487 | EXPECT_EQ(1, d.response_started_count()); |
| 11488 | EXPECT_FALSE(d.received_data_before_response()); |
| 11489 | EXPECT_EQ(d.bytes_received(), 0); |
| 11490 | } |
| 11491 | } |
| 11492 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11493 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11494 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11495 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11496 | TestDelegate d; |
| 11497 | // Set correct login credentials. The delegate will be asked for them when |
| 11498 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11499 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11500 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11501 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11502 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11503 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11504 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11505 | r->Start(); |
| 11506 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11507 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11508 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11509 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11510 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11511 | EXPECT_EQ(1, d.response_started_count()); |
| 11512 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11513 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11514 | } |
| 11515 | } |
| 11516 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11517 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11518 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11519 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11520 | TestDelegate d; |
| 11521 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11522 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11523 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11524 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11525 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11526 | r->Start(); |
| 11527 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11528 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11529 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11530 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11531 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11532 | EXPECT_EQ(1, d.response_started_count()); |
| 11533 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11534 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11535 | } |
| 11536 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11537 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11538 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11539 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11540 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11541 | TestDelegate d; |
| 11542 | // Set correct login credentials. The delegate will be asked for them when |
| 11543 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11544 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11545 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11546 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11547 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11548 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11549 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11550 | r->Start(); |
| 11551 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11552 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11553 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11554 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11555 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11556 | EXPECT_EQ(1, d.response_started_count()); |
| 11557 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11558 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11559 | } |
| 11560 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11561 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11562 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11563 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11564 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11565 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11566 | { |
| 11567 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11568 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11569 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11570 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11571 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11572 | r->Start(); |
| 11573 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11574 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11575 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11576 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11577 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11578 | EXPECT_EQ(1, d->response_started_count()); |
| 11579 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11580 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11581 | } |
| 11582 | |
| 11583 | d.reset(new TestDelegate); |
| 11584 | { |
| 11585 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11586 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11587 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11588 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11589 | r->Start(); |
| 11590 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11591 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11592 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11593 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11594 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11595 | EXPECT_EQ(1, d->response_started_count()); |
| 11596 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11597 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11598 | } |
| 11599 | } |
| 11600 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11601 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11602 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11603 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11604 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11605 | // Set correct login credentials. The delegate will be asked for them when |
| 11606 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11607 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11608 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11609 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11610 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11611 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11612 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11613 | r->Start(); |
| 11614 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11615 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11616 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11617 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11618 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11619 | EXPECT_EQ(1, d->response_started_count()); |
| 11620 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11621 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11622 | } |
| 11623 | |
| 11624 | // Use a new delegate without explicit credentials. The cached ones should be |
| 11625 | // used. |
| 11626 | d.reset(new TestDelegate); |
| 11627 | { |
| 11628 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 11629 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11630 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11631 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11632 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11633 | r->Start(); |
| 11634 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11635 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11636 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11637 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11638 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11639 | EXPECT_EQ(1, d->response_started_count()); |
| 11640 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11641 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11642 | } |
| 11643 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11644 | |
| 11645 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 11646 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11647 | |
| 11648 | TestDelegate d; |
| 11649 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11650 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 11651 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11652 | req->Start(); |
| 11653 | base::RunLoop().Run(); |
| 11654 | |
| 11655 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 11656 | } |
| 11657 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11658 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11659 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11660 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 11661 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11662 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 11663 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11664 | |
| 11665 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11666 | |
| 11667 | req->Start(); |
| 11668 | base::RunLoop().Run(); |
| 11669 | |
| 11670 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 11671 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11672 | } |
| 11673 | |
| 11674 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 11675 | MockHostResolver host_resolver; |
| 11676 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 11677 | TestURLRequestContext context(true); |
| 11678 | context.set_network_delegate(&network_delegate); |
| 11679 | context.set_host_resolver(&host_resolver); |
| 11680 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 11681 | context.Init(); |
| 11682 | |
| 11683 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11684 | std::unique_ptr<URLRequest> req( |
| 11685 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 11686 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11687 | |
| 11688 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11689 | |
| 11690 | req->Start(); |
| 11691 | base::RunLoop().Run(); |
| 11692 | EXPECT_TRUE(req->response_info().network_accessed); |
| 11693 | } |
| 11694 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11695 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11696 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11697 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11698 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11699 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11700 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 11701 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11702 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11703 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11704 | req.get(), &default_network_delegate_, |
| 11705 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11706 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 11707 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11708 | |
| 11709 | req->Start(); |
| 11710 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11711 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11712 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11713 | EXPECT_EQ(0, d.received_redirect_count()); |
| 11714 | } |
| 11715 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11716 | TEST_F(URLRequestTestHTTP, RequestHeadersCallback) { |
| 11717 | ASSERT_TRUE(http_test_server()->Start()); |
| 11718 | TestURLRequestContext context; |
| 11719 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 11720 | TestDelegate delegate; |
| 11721 | HttpRequestHeaders extra_headers; |
| 11722 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11723 | |
| 11724 | { |
| 11725 | HttpRawRequestHeaders raw_headers; |
| 11726 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11727 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11728 | r->SetExtraRequestHeaders(extra_headers); |
| 11729 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
| 11730 | base::Unretained(&raw_headers))); |
| 11731 | r->Start(); |
| 11732 | while (!delegate.response_started_count()) |
| 11733 | base::RunLoop().RunUntilIdle(); |
| 11734 | EXPECT_FALSE(raw_headers.headers().empty()); |
| 11735 | std::string value; |
| 11736 | EXPECT_TRUE(raw_headers.FindHeaderForTest("X-Foo", &value)); |
| 11737 | EXPECT_EQ("bar", value); |
| 11738 | EXPECT_TRUE(raw_headers.FindHeaderForTest("Accept-Encoding", &value)); |
| 11739 | EXPECT_EQ("gzip, deflate", value); |
| 11740 | EXPECT_TRUE(raw_headers.FindHeaderForTest("Connection", &value)); |
| 11741 | EXPECT_TRUE(raw_headers.FindHeaderForTest("Host", &value)); |
| 11742 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_headers.request_line()); |
| 11743 | } |
| 11744 | { |
| 11745 | HttpRawRequestHeaders raw_headers; |
| 11746 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11747 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11748 | r->SetExtraRequestHeaders(extra_headers); |
| 11749 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11750 | FAIL() << "Callback should not be called unless request is sent"; |
| 11751 | })); |
| 11752 | r->Start(); |
| 11753 | base::RunLoop().Run(); |
| 11754 | EXPECT_TRUE(r->was_cached()); |
| 11755 | EXPECT_TRUE(raw_headers.headers().empty()); |
| 11756 | EXPECT_TRUE(raw_headers.request_line().empty()); |
| 11757 | } |
| 11758 | } |
| 11759 | |
| 11760 | TEST_F(URLRequestTestHTTP, RequestHeadersCallbackWithRedirect) { |
| 11761 | ASSERT_TRUE(http_test_server()->Start()); |
| 11762 | HttpRawRequestHeaders raw_headers; |
| 11763 | TestURLRequestContext context; |
| 11764 | TestDelegate delegate; |
| 11765 | HttpRequestHeaders extra_headers; |
| 11766 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11767 | delegate.set_quit_on_redirect(true); |
| 11768 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
| 11769 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11770 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11771 | r->SetExtraRequestHeaders(extra_headers); |
| 11772 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
| 11773 | base::Unretained(&raw_headers))); |
| 11774 | r->Start(); |
| 11775 | base::RunLoop().Run(); |
| 11776 | |
| 11777 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 11778 | std::string value; |
| 11779 | EXPECT_TRUE(raw_headers.FindHeaderForTest("X-Foo", &value)); |
| 11780 | EXPECT_EQ("bar", value); |
| 11781 | EXPECT_TRUE(raw_headers.FindHeaderForTest("Accept-Encoding", &value)); |
| 11782 | EXPECT_EQ("gzip, deflate", value); |
| 11783 | EXPECT_EQ(1, delegate.received_redirect_count()); |
| 11784 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", raw_headers.request_line()); |
| 11785 | |
| 11786 | raw_headers = HttpRawRequestHeaders(); |
| 11787 | r->FollowDeferredRedirect(); |
| 11788 | base::RunLoop().Run(); |
| 11789 | EXPECT_TRUE(raw_headers.FindHeaderForTest("X-Foo", &value)); |
| 11790 | EXPECT_EQ("bar", value); |
| 11791 | EXPECT_TRUE(raw_headers.FindHeaderForTest("Accept-Encoding", &value)); |
| 11792 | EXPECT_EQ("gzip, deflate", value); |
| 11793 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", raw_headers.request_line()); |
| 11794 | } |
| 11795 | |
| 11796 | TEST_F(URLRequestTest, RequestHeadersCallbackConnectFailed) { |
| 11797 | TestDelegate request_delegate; |
| 11798 | |
| 11799 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11800 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 11801 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11802 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11803 | FAIL() << "Callback should not be called unless request is sent"; |
| 11804 | })); |
| 11805 | r->Start(); |
| 11806 | base::RunLoop().Run(); |
| 11807 | EXPECT_FALSE(r->is_pending()); |
| 11808 | } |
| 11809 | |
| 11810 | TEST_F(URLRequestTestHTTP, RequestHeadersCallbackAuthRetry) { |
| 11811 | ASSERT_TRUE(http_test_server()->Start()); |
| 11812 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 11813 | |
| 11814 | TestURLRequestContext context; |
| 11815 | TestDelegate delegate; |
| 11816 | |
| 11817 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 11818 | HttpRequestHeaders extra_headers; |
| 11819 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11820 | |
| 11821 | using HeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 11822 | HeadersVector raw_headers; |
| 11823 | |
| 11824 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11825 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11826 | r->SetExtraRequestHeaders(extra_headers); |
| 11827 | r->SetRequestHeadersCallback(base::Bind( |
| 11828 | [](HeadersVector* vec, HttpRawRequestHeaders headers) { |
| 11829 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 11830 | }, |
| 11831 | &raw_headers)); |
| 11832 | r->Start(); |
| 11833 | base::RunLoop().Run(); |
| 11834 | EXPECT_FALSE(r->is_pending()); |
| 11835 | ASSERT_GE(raw_headers.size(), 2u); |
| 11836 | std::string value; |
| 11837 | EXPECT_FALSE(raw_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 11838 | EXPECT_TRUE(raw_headers[0]->FindHeaderForTest("X-Foo", &value)); |
| 11839 | EXPECT_EQ("bar", value); |
| 11840 | EXPECT_TRUE(raw_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 11841 | EXPECT_TRUE(raw_headers[1]->FindHeaderForTest("X-Foo", &value)); |
| 11842 | EXPECT_EQ("bar", value); |
| 11843 | } |
| 11844 | |
| 11845 | TEST_F(URLRequestTest, RequestHeadersCallbackNonHTTP) { |
| 11846 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 11847 | TestDelegate d; |
| 11848 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11849 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11850 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11851 | FAIL() << "Callback should not be called unless request is sent"; |
| 11852 | })); |
| 11853 | r->Start(); |
| 11854 | base::RunLoop().Run(); |
| 11855 | EXPECT_FALSE(r->is_pending()); |
| 11856 | } |
| 11857 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11858 | } // namespace net |