[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5 | #include <utility> |
| 6 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7 | // This must be before Windows headers |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 10 | #if defined(OS_WIN) |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 11 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 12 | #include <shlobj.h> |
Fan Yang | 5a88d5d | 2017-10-24 01:14:29 | [diff] [blame] | 13 | #include <windows.h> |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 14 | #include <wrl/client.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 15 | #endif |
| 16 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include <algorithm> |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame] | 20 | #include <memory> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 21 | #include <limits> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 23 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 24 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 25 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 26 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 27 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 28 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 29 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 30 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 31 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 32 | #include "base/macros.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 33 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 34 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "base/path_service.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 36 | #include "base/power_monitor/power_monitor.h" |
| 37 | #include "base/power_monitor/power_monitor_source.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 38 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 39 | #include "base/single_thread_task_runner.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 40 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 41 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 42 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 45 | #include "base/strings/utf_string_conversions.h" |
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" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 59 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 60 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 61 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 62 | #include "net/base/upload_bytes_element_reader.h" |
| 63 | #include "net/base/upload_data_stream.h" |
| 64 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 65 | #include "net/base/url_util.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 66 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 67 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 68 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 69 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 70 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 71 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 72 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 73 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 74 | #include "net/cookies/cookie_monster.h" |
| 75 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 76 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 77 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 78 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | #include "net/http/http_cache.h" |
| 80 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 81 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 82 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 83 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 84 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 85 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 86 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 87 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 88 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 89 | #include "net/log/test_net_log_entry.h" |
| 90 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 91 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 92 | #include "net/proxy_resolution/proxy_resolution_service.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 93 | #include "net/quic/chromium/mock_crypto_client_stream_factory.h" |
| 94 | #include "net/quic/chromium/quic_server_info.h" |
| 95 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 96 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 97 | #include "net/ssl/channel_id_service.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 98 | #include "net/ssl/client_cert_identity_test_util.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 99 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 100 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 101 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 102 | #include "net/ssl/ssl_server_config.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 103 | #include "net/ssl/token_binding.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 104 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 105 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 106 | #include "net/test/embedded_test_server/http_request.h" |
| 107 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 108 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 109 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 110 | #include "net/test/test_data_directory.h" |
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" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 114 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 116 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 117 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 118 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 119 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 120 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 121 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 122 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 123 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 124 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 125 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 126 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 127 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 128 | #if defined(OS_FUCHSIA) |
| 129 | #define USE_BUILTIN_CERT_VERIFIER |
| 130 | #endif |
| 131 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 132 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 133 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 134 | #include "net/url_request/file_protocol_handler.h" |
| 135 | #include "net/url_request/url_request_file_dir_job.h" |
| 136 | #endif |
| 137 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 138 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 139 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 140 | #include "net/url_request/ftp_protocol_handler.h" |
| 141 | #endif |
| 142 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 143 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 144 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 145 | #endif |
| 146 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 147 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 148 | #include "net/network_error_logging/network_error_logging_service.h" |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 149 | #include "net/reporting/reporting_policy.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 150 | #include "net/reporting/reporting_service.h" |
| 151 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 152 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 153 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 154 | #include "net/cert/cert_net_fetcher.h" |
| 155 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 156 | #endif |
| 157 | |
| 158 | #if defined(USE_NSS_CERTS) |
| 159 | #include "net/cert_net/nss_ocsp.h" |
| 160 | #endif |
| 161 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 162 | using net::test::IsError; |
| 163 | using net::test::IsOk; |
| 164 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 165 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 166 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 167 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 168 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 169 | namespace net { |
| 170 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 171 | namespace { |
| 172 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 173 | namespace test_default { |
| 174 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 177 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 178 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 179 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 180 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 181 | const base::FilePath::CharType kTestFilePath[] = |
| 182 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 183 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 184 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 185 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 186 | // Test file used in most FTP tests. |
| 187 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 188 | #endif |
| 189 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 190 | // Tests load timing information in the case a fresh connection was used, with |
| 191 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 192 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 193 | int connect_timing_flags) { |
| 194 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 195 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 196 | |
| 197 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 198 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 199 | |
| 200 | EXPECT_LE(load_timing_info.request_start, |
| 201 | load_timing_info.connect_timing.connect_start); |
| 202 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 203 | connect_timing_flags); |
| 204 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 205 | load_timing_info.send_start); |
| 206 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 207 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 208 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 209 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 210 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 211 | } |
| 212 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 213 | // Same as above, but with proxy times. |
| 214 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 215 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 216 | int connect_timing_flags) { |
| 217 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 218 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 219 | |
| 220 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 221 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 222 | |
| 223 | EXPECT_LE(load_timing_info.request_start, |
| 224 | load_timing_info.proxy_resolve_start); |
| 225 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 226 | load_timing_info.proxy_resolve_end); |
| 227 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 228 | load_timing_info.connect_timing.connect_start); |
| 229 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 230 | connect_timing_flags); |
| 231 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 232 | load_timing_info.send_start); |
| 233 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 234 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 235 | } |
| 236 | |
| 237 | // Same as above, but with a reused socket and proxy times. |
| 238 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 239 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 240 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 241 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 242 | |
| 243 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 244 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 245 | |
| 246 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 247 | |
| 248 | EXPECT_LE(load_timing_info.request_start, |
| 249 | load_timing_info.proxy_resolve_start); |
| 250 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 251 | load_timing_info.proxy_resolve_end); |
| 252 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 253 | load_timing_info.send_start); |
| 254 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 255 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 256 | } |
| 257 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 258 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 259 | // Tests load timing information in the case of a cache hit, when no cache |
| 260 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 261 | base::StringPiece TestNetResourceProvider(int key) { |
| 262 | return "header"; |
| 263 | } |
| 264 | |
| 265 | void FillBuffer(char* buffer, size_t len) { |
| 266 | static bool called = false; |
| 267 | if (!called) { |
| 268 | called = true; |
| 269 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 270 | srand(seed); |
| 271 | } |
| 272 | |
| 273 | for (size_t i = 0; i < len; i++) { |
| 274 | buffer[i] = static_cast<char>(rand()); |
| 275 | if (!buffer[i]) |
| 276 | buffer[i] = 'g'; |
| 277 | } |
| 278 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 279 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 280 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 281 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 282 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 283 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 284 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 285 | |
| 286 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 287 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 288 | |
| 289 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 290 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 291 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 292 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 293 | |
| 294 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 295 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 296 | } |
| 297 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 298 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 299 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 300 | // Tests load timing in the case that there is no HTTP response. This can be |
| 301 | // used to test in the case of errors or non-HTTP requests. |
| 302 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 303 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 304 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 305 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 306 | |
| 307 | // Only the request times should be non-null. |
| 308 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 309 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 310 | |
| 311 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 312 | |
| 313 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 314 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 315 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 316 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 317 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 318 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 319 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 320 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 321 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 322 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 323 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 324 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 325 | TestPowerMonitorSource() = default; |
| 326 | ~TestPowerMonitorSource() override = default; |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 327 | |
| 328 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 329 | |
| 330 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 331 | |
| 332 | bool IsOnBatteryPowerImpl() override { return false; } |
| 333 | |
| 334 | private: |
| 335 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 336 | }; |
| 337 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 338 | // Job that allows monitoring of its priority. |
| 339 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 340 | public: |
| 341 | // The latest priority of the job is always written to |request_priority_|. |
| 342 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 343 | NetworkDelegate* network_delegate, |
| 344 | RequestPriority* request_priority) |
| 345 | : URLRequestTestJob(request, network_delegate), |
| 346 | request_priority_(request_priority) { |
| 347 | *request_priority_ = DEFAULT_PRIORITY; |
| 348 | } |
| 349 | |
| 350 | void SetPriority(RequestPriority priority) override { |
| 351 | *request_priority_ = priority; |
| 352 | URLRequestTestJob::SetPriority(priority); |
| 353 | } |
| 354 | |
| 355 | private: |
| 356 | RequestPriority* const request_priority_; |
| 357 | }; |
| 358 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 359 | // Do a case-insensitive search through |haystack| for |needle|. |
| 360 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 361 | std::string::const_iterator it = std::search( |
| 362 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 363 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 364 | return it != haystack.end(); |
| 365 | } |
| 366 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 367 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 368 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 369 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 370 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 373 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 374 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 375 | // -1 means unknown. 0 means no encryption. |
| 376 | EXPECT_GT(ssl_info.security_bits, 0); |
| 377 | |
| 378 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 379 | uint16_t cipher_suite = |
| 380 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 381 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 384 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 385 | const GURL& host_url) { |
| 386 | std::string sent_value; |
| 387 | |
| 388 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 389 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 390 | |
| 391 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 392 | EXPECT_EQ("keep-alive", sent_value); |
| 393 | } |
| 394 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 395 | // A network delegate that allows the user to choose a subset of request stages |
| 396 | // to block in. When blocking, the delegate can do one of the following: |
| 397 | // * synchronously return a pre-specified error code, or |
| 398 | // * asynchronously return that value via an automatically called callback, |
| 399 | // or |
| 400 | // * block and wait for the user to do a callback. |
| 401 | // Additionally, the user may also specify a redirect URL -- then each request |
| 402 | // with the current URL different from the redirect target will be redirected |
| 403 | // to that target, in the on-before-URL-request stage, independent of whether |
| 404 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 405 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 406 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 407 | // Stages in which the delegate can block. |
| 408 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 409 | NOT_BLOCKED = 0, |
| 410 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 411 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 412 | ON_HEADERS_RECEIVED = 1 << 2, |
| 413 | ON_AUTH_REQUIRED = 1 << 3 |
| 414 | }; |
| 415 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 416 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 417 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 418 | enum BlockMode { |
| 419 | SYNCHRONOUS, // No callback, returns specified return values. |
| 420 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 421 | // specified return codes. |
| 422 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 423 | // |auth_retval_| are ignored. In every blocking stage the |
| 424 | // message loop is quit. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 425 | }; |
| 426 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 427 | // Creates a delegate which does not block at all. |
| 428 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 429 | |
| 430 | // For users to trigger a callback returning |response|. |
| 431 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 432 | // Only call if |block_mode_| == USER_CALLBACK. |
| 433 | void DoCallback(int response); |
| 434 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 435 | |
| 436 | // Setters. |
| 437 | void set_retval(int retval) { |
| 438 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 439 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 440 | ASSERT_NE(OK, retval); |
| 441 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 444 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 445 | // |auth_credentials_| will be passed with the response. |
| 446 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 447 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 448 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 449 | auth_retval_ = auth_retval; |
| 450 | } |
| 451 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 452 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 455 | void set_redirect_url(const GURL& url) { |
| 456 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 459 | void set_block_on(int block_on) { |
| 460 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 463 | // Allows the user to check in which state did we block. |
| 464 | Stage stage_blocked_for_callback() const { |
| 465 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 466 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 470 | void RunCallback(int response, const CompletionCallback& callback); |
| 471 | void RunAuthCallback(AuthRequiredResponse response, |
| 472 | const AuthCallback& callback); |
| 473 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 474 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 475 | int OnBeforeURLRequest(URLRequest* request, |
| 476 | const CompletionCallback& callback, |
| 477 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 478 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 479 | int OnBeforeStartTransaction(URLRequest* request, |
| 480 | const CompletionCallback& callback, |
| 481 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 482 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 483 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 484 | URLRequest* request, |
| 485 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 486 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 487 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 488 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 489 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 490 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 491 | URLRequest* request, |
| 492 | const AuthChallengeInfo& auth_info, |
| 493 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 494 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 495 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 496 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 497 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 498 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 499 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 500 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 501 | int MaybeBlockStage(Stage stage, |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 502 | const CompletionCallback& callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 503 | |
| 504 | // Configuration parameters, can be adjusted by public methods: |
| 505 | const BlockMode block_mode_; |
| 506 | |
| 507 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 508 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 509 | int retval_; // To be returned in non-auth stages. |
| 510 | AuthRequiredResponse auth_retval_; |
| 511 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 512 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 513 | int block_on_; // Bit mask: in which stages to block. |
| 514 | |
| 515 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 516 | // callback. |
| 517 | AuthCredentials auth_credentials_; |
| 518 | AuthCredentials* target_auth_credentials_; |
| 519 | |
| 520 | // Internal variables, not set by not the user: |
| 521 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 522 | // USER_CALLBACK). |
| 523 | Stage stage_blocked_for_callback_; |
| 524 | |
| 525 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 526 | CompletionCallback callback_; |
| 527 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 528 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 529 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 530 | |
| 531 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 532 | }; |
| 533 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 534 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 535 | : block_mode_(block_mode), |
| 536 | retval_(OK), |
| 537 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 538 | block_on_(0), |
| 539 | target_auth_credentials_(NULL), |
| 540 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 541 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 545 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 546 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 547 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 548 | CompletionCallback callback = callback_; |
| 549 | Reset(); |
| 550 | RunCallback(response, callback); |
| 551 | } |
| 552 | |
| 553 | void BlockingNetworkDelegate::DoAuthCallback( |
| 554 | NetworkDelegate::AuthRequiredResponse response) { |
| 555 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 556 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 557 | AuthCallback auth_callback = auth_callback_; |
| 558 | Reset(); |
| 559 | RunAuthCallback(response, auth_callback); |
| 560 | } |
| 561 | |
| 562 | void BlockingNetworkDelegate::RunCallback(int response, |
| 563 | const CompletionCallback& callback) { |
| 564 | callback.Run(response); |
| 565 | } |
| 566 | |
| 567 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 568 | const AuthCallback& callback) { |
| 569 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 570 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 571 | *target_auth_credentials_ = auth_credentials_; |
| 572 | } |
| 573 | callback.Run(response); |
| 574 | } |
| 575 | |
| 576 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 577 | URLRequest* request, |
| 578 | const CompletionCallback& callback, |
| 579 | GURL* new_url) { |
| 580 | if (redirect_url_ == request->url()) |
| 581 | return OK; // We've already seen this request and redirected elsewhere. |
| 582 | |
| 583 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 584 | |
| 585 | if (!redirect_url_.is_empty()) |
| 586 | *new_url = redirect_url_; |
| 587 | |
Helen Li | b495c380 | 2018-03-30 13:46:09 | [diff] [blame] | 588 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 589 | } |
| 590 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 591 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 592 | URLRequest* request, |
| 593 | const CompletionCallback& callback, |
| 594 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 595 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 596 | |
Helen Li | b495c380 | 2018-03-30 13:46:09 | [diff] [blame] | 597 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 601 | URLRequest* request, |
| 602 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 603 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 604 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 605 | GURL* allowed_unsafe_redirect_url) { |
| 606 | TestNetworkDelegate::OnHeadersReceived(request, |
| 607 | callback, |
| 608 | original_response_headers, |
| 609 | override_response_headers, |
| 610 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 611 | |
Helen Li | b495c380 | 2018-03-30 13:46:09 | [diff] [blame] | 612 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 616 | URLRequest* request, |
| 617 | const AuthChallengeInfo& auth_info, |
| 618 | const AuthCallback& callback, |
| 619 | AuthCredentials* credentials) { |
| 620 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 621 | credentials); |
| 622 | // Check that the user has provided callback for the previous blocked stage. |
| 623 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 624 | |
| 625 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 626 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 627 | } |
| 628 | |
| 629 | target_auth_credentials_ = credentials; |
| 630 | |
| 631 | switch (block_mode_) { |
| 632 | case SYNCHRONOUS: |
| 633 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 634 | *target_auth_credentials_ = auth_credentials_; |
| 635 | return auth_retval_; |
| 636 | |
| 637 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 638 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 639 | FROM_HERE, |
| 640 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 641 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 642 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 643 | |
| 644 | case USER_CALLBACK: |
| 645 | auth_callback_ = callback; |
| 646 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 647 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 648 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 649 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | } |
| 651 | NOTREACHED(); |
| 652 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 653 | } |
| 654 | |
| 655 | void BlockingNetworkDelegate::Reset() { |
| 656 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 657 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 658 | callback_.Reset(); |
| 659 | auth_callback_.Reset(); |
| 660 | } |
| 661 | |
| 662 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 663 | BlockingNetworkDelegate::Stage stage, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 664 | const CompletionCallback& callback) { |
| 665 | // Check that the user has provided callback for the previous blocked stage. |
| 666 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 667 | |
| 668 | if ((block_on_ & stage) == 0) { |
| 669 | return OK; |
| 670 | } |
| 671 | |
| 672 | switch (block_mode_) { |
| 673 | case SYNCHRONOUS: |
| 674 | EXPECT_NE(OK, retval_); |
| 675 | return retval_; |
| 676 | |
| 677 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 678 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 679 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 680 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 681 | return ERR_IO_PENDING; |
| 682 | |
| 683 | case USER_CALLBACK: |
| 684 | callback_ = callback; |
| 685 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 686 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 687 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 688 | return ERR_IO_PENDING; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 689 | } |
| 690 | NOTREACHED(); |
| 691 | return 0; |
| 692 | } |
| 693 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 694 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 695 | public: |
| 696 | // Does not own |delegate|. |
| 697 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 698 | NetworkDelegate* delegate) |
| 699 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 700 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 701 | ProxyResolutionService::CreateFixed(proxy, |
| 702 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 703 | set_network_delegate(delegate); |
| 704 | Init(); |
| 705 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 706 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 707 | }; |
| 708 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 709 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 710 | // URI and report to be sent. |
| 711 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 712 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 713 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 714 | MockCertificateReportSender() = default; |
| 715 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 716 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 717 | void Send(const GURL& report_uri, |
| 718 | base::StringPiece content_type, |
| 719 | base::StringPiece report, |
| 720 | const base::Callback<void()>& success_callback, |
| 721 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 722 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 723 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 724 | report.CopyToString(&latest_report_); |
| 725 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 726 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 727 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 728 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 729 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 730 | |
| 731 | private: |
| 732 | GURL latest_report_uri_; |
| 733 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 734 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 735 | }; |
| 736 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 737 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 738 | public: |
| 739 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 740 | }; |
| 741 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 742 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 743 | // This is needed because after the certificate failure, the URLRequest will |
| 744 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 745 | // The partial SSLInfo does not have the OCSP information filled out. |
| 746 | class OCSPErrorTestDelegate : public TestDelegate { |
| 747 | public: |
| 748 | void OnSSLCertificateError(URLRequest* request, |
| 749 | const SSLInfo& ssl_info, |
| 750 | bool fatal) override { |
| 751 | ssl_info_ = ssl_info; |
| 752 | on_ssl_certificate_error_called_ = true; |
| 753 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 754 | } |
| 755 | |
| 756 | bool on_ssl_certificate_error_called() { |
| 757 | return on_ssl_certificate_error_called_; |
| 758 | } |
| 759 | |
| 760 | SSLInfo ssl_info() { return ssl_info_; } |
| 761 | |
| 762 | private: |
| 763 | bool on_ssl_certificate_error_called_ = false; |
| 764 | SSLInfo ssl_info_; |
| 765 | }; |
| 766 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 767 | } // namespace |
| 768 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 769 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 770 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 771 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 772 | URLRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 773 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 774 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 775 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 776 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 777 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 778 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 779 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 780 | // URLRequestJobs may post clean-up tasks on destruction. |
| 781 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 782 | |
| 783 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 784 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 785 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 786 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 787 | SetUpFactory(); |
| 788 | default_context_.set_job_factory(job_factory_.get()); |
| 789 | default_context_.Init(); |
| 790 | PlatformTest::SetUp(); |
| 791 | } |
| 792 | |
| 793 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 794 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 795 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 796 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 797 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 798 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 799 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 800 | #endif |
| 801 | } |
| 802 | |
| 803 | TestNetworkDelegate* default_network_delegate() { |
| 804 | return &default_network_delegate_; |
| 805 | } |
| 806 | |
| 807 | const TestURLRequestContext& default_context() const { |
| 808 | return default_context_; |
| 809 | } |
| 810 | |
| 811 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 812 | // Adds the TestJobInterceptor to the default context. |
| 813 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 814 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 815 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 816 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 817 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 818 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 819 | } |
| 820 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 821 | // Creates a temp test file and writes |data| to the file. The file will be |
| 822 | // deleted after the test completes. |
| 823 | void CreateTestFile(const char* data, |
| 824 | size_t data_size, |
| 825 | base::FilePath* test_file) { |
| 826 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 827 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 828 | // now, Mac and Android bots return a path with a symbolic link. |
| 829 | base::FilePath absolute_temp_dir = |
| 830 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 831 | |
| 832 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 833 | ASSERT_EQ(static_cast<int>(data_size), |
| 834 | base::WriteFile(*test_file, data, data_size)); |
| 835 | } |
| 836 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 837 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 838 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 839 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 840 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 841 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 842 | TestURLRequestContext default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 843 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 844 | }; |
| 845 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 846 | // This NetworkDelegate is picky about what files are accessible. Only |
| 847 | // whitelisted files are allowed. |
| 848 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 849 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 850 | CookieBlockingNetworkDelegate() = default; |
| 851 | ; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 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) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 950 | const char kTestFileContent[] = "Hello"; |
| 951 | base::FilePath test_file; |
| 952 | ASSERT_NO_FATAL_FAILURE( |
| 953 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 954 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 955 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 956 | |
| 957 | TestDelegate d; |
| 958 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 959 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 960 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 961 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 962 | r->Start(); |
| 963 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 965 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 966 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 967 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | EXPECT_EQ(1, d.response_started_count()); |
| 969 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 970 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 971 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 972 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 973 | |
| 974 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 975 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 979 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 980 | const char kTestFileContent[] = "Hello"; |
| 981 | base::FilePath test_file; |
| 982 | ASSERT_NO_FATAL_FAILURE( |
| 983 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 984 | |
| 985 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 986 | |
| 987 | TestDelegate d; |
| 988 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 989 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 990 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 991 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 992 | r->Start(); |
| 993 | EXPECT_TRUE(r->is_pending()); |
| 994 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 995 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 996 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 997 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 998 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 999 | } |
| 1000 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1001 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1002 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1003 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1004 | FillBuffer(buffer.get(), buffer_size); |
| 1005 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1006 | base::FilePath test_file; |
| 1007 | ASSERT_NO_FATAL_FAILURE( |
| 1008 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1009 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1010 | |
| 1011 | const size_t first_byte_position = 500; |
| 1012 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1013 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1014 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1015 | buffer.get() + last_byte_position + 1); |
| 1016 | |
| 1017 | TestDelegate d; |
| 1018 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1019 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1020 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1021 | |
| 1022 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1023 | headers.SetHeader( |
| 1024 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1025 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1026 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1027 | r->SetExtraRequestHeaders(headers); |
| 1028 | r->Start(); |
| 1029 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1030 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1031 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1032 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1033 | EXPECT_EQ(1, d.response_started_count()); |
| 1034 | EXPECT_FALSE(d.received_data_before_response()); |
| 1035 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1036 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1037 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1038 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1042 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1043 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1044 | FillBuffer(buffer.get(), buffer_size); |
| 1045 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1046 | base::FilePath test_file; |
| 1047 | ASSERT_NO_FATAL_FAILURE( |
| 1048 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1049 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1050 | |
| 1051 | const size_t first_byte_position = 500; |
| 1052 | const size_t last_byte_position = buffer_size - 1; |
| 1053 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1054 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1055 | buffer.get() + last_byte_position + 1); |
| 1056 | |
| 1057 | TestDelegate d; |
| 1058 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1059 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1060 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1061 | |
| 1062 | HttpRequestHeaders headers; |
| 1063 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1064 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1065 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1066 | r->SetExtraRequestHeaders(headers); |
| 1067 | r->Start(); |
| 1068 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1069 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1070 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1071 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1072 | EXPECT_EQ(1, d.response_started_count()); |
| 1073 | EXPECT_FALSE(d.received_data_before_response()); |
| 1074 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1075 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1076 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1077 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1081 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1082 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1083 | FillBuffer(buffer.get(), buffer_size); |
| 1084 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1085 | base::FilePath test_file; |
| 1086 | ASSERT_NO_FATAL_FAILURE( |
| 1087 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1088 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1089 | |
| 1090 | TestDelegate d; |
| 1091 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1092 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1093 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1094 | |
| 1095 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1096 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1097 | r->SetExtraRequestHeaders(headers); |
| 1098 | r->Start(); |
| 1099 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1100 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1101 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1102 | EXPECT_TRUE(d.request_failed()); |
| 1103 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1104 | } |
| 1105 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1106 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1107 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1108 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1109 | ASSERT_NO_FATAL_FAILURE( |
| 1110 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1111 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1112 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1113 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1114 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1115 | // Hence the test should use the directory name of |test_file|, rather than |
| 1116 | // |absolute_temp_dir|, for whitelisting. |
| 1117 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1118 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1119 | { |
| 1120 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1121 | CookieBlockingNetworkDelegate network_delegate; |
| 1122 | network_delegate.AddToWhitelist(real_temp_dir); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1123 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1124 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1125 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1126 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1127 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1128 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1129 | EXPECT_FALSE(d.request_failed()); |
| 1130 | EXPECT_EQ(test_data, d.data_received()); |
| 1131 | } |
| 1132 | |
| 1133 | { |
| 1134 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1135 | CookieBlockingNetworkDelegate network_delegate; |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1136 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1137 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1138 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1139 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1140 | base::RunLoop().Run(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1141 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1142 | EXPECT_TRUE(d.request_failed()); |
| 1143 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1144 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1148 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1149 | |
| 1150 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1151 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1152 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1153 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1154 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1155 | |
| 1156 | // Create a good directory (will be whitelisted) and a good file. |
| 1157 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1158 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1159 | base::FilePath good_file; |
| 1160 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1161 | std::string good_data("good"); |
| 1162 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1163 | // See the comment in AllowFileURLs() for why this is done. |
| 1164 | base::FilePath real_good_dir = good_file.DirName(); |
| 1165 | |
| 1166 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1167 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1168 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1169 | base::FilePath bad_file; |
| 1170 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1171 | std::string bad_data("bad"); |
| 1172 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1173 | |
| 1174 | // This symlink will point to the good file. Access to the symlink will be |
| 1175 | // allowed as both the symlink and the destination file are in the same |
| 1176 | // good directory. |
| 1177 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1178 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1179 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1180 | // This symlink will point to the bad file. Even though the symlink is in |
| 1181 | // the good directory, access to the symlink will be rejected since it |
| 1182 | // points to the bad file. |
| 1183 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1184 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1185 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1186 | |
| 1187 | CookieBlockingNetworkDelegate network_delegate; |
| 1188 | network_delegate.AddToWhitelist(real_good_dir); |
| 1189 | { |
| 1190 | TestDelegate d; |
| 1191 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1192 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1193 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1194 | r->Start(); |
| 1195 | base::RunLoop().Run(); |
| 1196 | // good_file_url should be allowed. |
| 1197 | EXPECT_FALSE(d.request_failed()); |
| 1198 | EXPECT_EQ(good_data, d.data_received()); |
| 1199 | } |
| 1200 | |
| 1201 | { |
| 1202 | TestDelegate d; |
| 1203 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1204 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1205 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1206 | r->Start(); |
| 1207 | base::RunLoop().Run(); |
| 1208 | // bad_file_url should be rejected. |
| 1209 | EXPECT_TRUE(d.request_failed()); |
| 1210 | EXPECT_EQ("", d.data_received()); |
| 1211 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1216 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1217 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1218 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1219 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1220 | |
| 1221 | // Create a good directory (will be whitelisted). |
| 1222 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1223 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1224 | |
| 1225 | // Create a bad directory (will not be whitelisted). |
| 1226 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1227 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1228 | |
| 1229 | // This symlink will point to the good directory. Access to the symlink |
| 1230 | // will be allowed as the symlink is in the good dir that'll be white |
| 1231 | // listed. |
| 1232 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1233 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1234 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1235 | // This symlink will point to the bad directory. Even though the symlink is |
| 1236 | // in the good directory, access to the symlink will be rejected since it |
| 1237 | // points to the bad directory. |
| 1238 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1239 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1240 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1241 | |
| 1242 | CookieBlockingNetworkDelegate network_delegate; |
| 1243 | network_delegate.AddToWhitelist(good_dir); |
| 1244 | { |
| 1245 | TestDelegate d; |
| 1246 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1247 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1248 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1249 | r->Start(); |
| 1250 | base::RunLoop().Run(); |
| 1251 | // good_file_url should be allowed. |
| 1252 | EXPECT_FALSE(d.request_failed()); |
| 1253 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1254 | } |
| 1255 | |
| 1256 | { |
| 1257 | TestDelegate d; |
| 1258 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1259 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1260 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1261 | r->Start(); |
| 1262 | base::RunLoop().Run(); |
| 1263 | // bad_file_url should be rejected. |
| 1264 | EXPECT_TRUE(d.request_failed()); |
| 1265 | EXPECT_EQ("", d.data_received()); |
| 1266 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1270 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1271 | |
| 1272 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1273 | // Put in mock resource provider. |
| 1274 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1275 | |
| 1276 | TestDelegate d; |
| 1277 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1278 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1279 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1280 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1281 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1282 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1283 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1284 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1285 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1286 | req->Start(); |
| 1287 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1288 | |
| 1289 | d.set_cancel_in_received_data_pending(true); |
| 1290 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1291 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | // Take out mock resource provider. |
| 1295 | NetModule::SetResourceProvider(NULL); |
| 1296 | } |
| 1297 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1298 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1299 | // Verify the general sanity of the the output of the file: |
| 1300 | // directory lister by checking for the output of a known existing |
| 1301 | // file. |
| 1302 | const char sentinel_name[] = "filedir-sentinel"; |
| 1303 | |
| 1304 | base::FilePath path; |
| 1305 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1306 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1307 | |
| 1308 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1309 | std::unique_ptr<URLRequest> req( |
| 1310 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1311 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1312 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1313 | base::RunLoop().Run(); |
| 1314 | |
| 1315 | // Generate entry for the sentinel file. |
| 1316 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1317 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1318 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1319 | EXPECT_GT(info.size, 0); |
| 1320 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1321 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1322 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1323 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1324 | info.last_modified); |
| 1325 | |
| 1326 | ASSERT_LT(0, d.bytes_received()); |
| 1327 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1328 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1329 | // Check for the entry generated for the "sentinel" file. |
| 1330 | const std::string& data = d.data_received(); |
| 1331 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1332 | } |
| 1333 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1334 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1335 | // There is an implicit redirect when loading a file path that matches a |
| 1336 | // directory and does not end with a slash. Ensure that following such |
| 1337 | // redirects does not crash. See http://crbug.com/18686. |
| 1338 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1339 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1340 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1341 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1342 | |
| 1343 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1344 | std::unique_ptr<URLRequest> req( |
| 1345 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1346 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1347 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1348 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1349 | |
| 1350 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1351 | ASSERT_LT(0, d.bytes_received()); |
| 1352 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1353 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | #if defined(OS_WIN) |
| 1357 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1358 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1359 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1360 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1361 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1362 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1363 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1364 | |
| 1365 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1366 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1367 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1368 | #endif // defined(OS_WIN) |
| 1369 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1370 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1371 | |
| 1372 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1373 | TestDelegate d; |
| 1374 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1375 | std::unique_ptr<URLRequest> r( |
| 1376 | default_context_.CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1377 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1378 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1379 | r->Start(); |
| 1380 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1381 | |
| 1382 | base::RunLoop().Run(); |
| 1383 | EXPECT_TRUE(d.request_failed()); |
| 1384 | } |
| 1385 | } |
| 1386 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1387 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1388 | TestURLRequestContext context; |
| 1389 | TestNetworkDelegate network_delegate; |
| 1390 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1391 | context.set_network_delegate(&network_delegate); |
| 1392 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1393 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1394 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1395 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1396 | req->SetReferrer("https://somewhere.com/"); |
| 1397 | |
| 1398 | req->Start(); |
| 1399 | base::RunLoop().Run(); |
| 1400 | EXPECT_TRUE(d.request_failed()); |
| 1401 | } |
| 1402 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1403 | #if defined(OS_WIN) |
| 1404 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1405 | base::FilePath app_path; |
| 1406 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1407 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1408 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1409 | |
| 1410 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1411 | |
| 1412 | base::win::ScopedCOMInitializer com_initializer; |
| 1413 | |
| 1414 | // Temporarily create a shortcut for test |
| 1415 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1416 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1417 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1418 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1419 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1420 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1421 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1422 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1423 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1424 | } |
| 1425 | |
| 1426 | TestDelegate d; |
| 1427 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1428 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1429 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1430 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1432 | r->Start(); |
| 1433 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1434 | |
| 1435 | base::RunLoop().Run(); |
| 1436 | |
| 1437 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1438 | GetFileAttributesEx(app_path.value().c_str(), |
| 1439 | GetFileExInfoStandard, &data); |
| 1440 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1441 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1442 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1443 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1444 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1445 | DWORD read_size; |
| 1446 | BOOL result; |
| 1447 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1448 | &read_size, NULL); |
| 1449 | std::string content(buffer.get(), read_size); |
| 1450 | CloseHandle(file); |
| 1451 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1452 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1453 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1454 | EXPECT_EQ(content, d.data_received()); |
| 1455 | } |
| 1456 | |
| 1457 | // Clean the shortcut |
| 1458 | DeleteFile(lnk_path.c_str()); |
| 1459 | } |
| 1460 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1461 | |
| 1462 | // Custom URLRequestJobs for use with interceptor tests |
| 1463 | class RestartTestJob : public URLRequestTestJob { |
| 1464 | public: |
| 1465 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1466 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1467 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1468 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1469 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1470 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1471 | }; |
| 1472 | |
| 1473 | class CancelTestJob : public URLRequestTestJob { |
| 1474 | public: |
| 1475 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1476 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1477 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1478 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1479 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1480 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1481 | }; |
| 1482 | |
| 1483 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1484 | public: |
| 1485 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1486 | NetworkDelegate* network_delegate) |
| 1487 | : URLRequestTestJob(request, network_delegate, true) { |
| 1488 | } |
| 1489 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1490 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1491 | request_->Cancel(); |
| 1492 | this->NotifyRestartRequired(); |
| 1493 | } |
| 1494 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1495 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1496 | }; |
| 1497 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1498 | // An Interceptor for use with interceptor tests. |
| 1499 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1500 | public: |
| 1501 | // Static getters for canned response header and data strings. |
| 1502 | static std::string ok_data() { |
| 1503 | return URLRequestTestJob::test_data_1(); |
| 1504 | } |
| 1505 | |
| 1506 | static std::string ok_headers() { |
| 1507 | return URLRequestTestJob::test_headers(); |
| 1508 | } |
| 1509 | |
| 1510 | static std::string redirect_data() { |
| 1511 | return std::string(); |
| 1512 | } |
| 1513 | |
| 1514 | static std::string redirect_headers() { |
| 1515 | return URLRequestTestJob::test_redirect_headers(); |
| 1516 | } |
| 1517 | |
| 1518 | static std::string error_data() { |
| 1519 | return std::string("ohhh nooooo mr. bill!"); |
| 1520 | } |
| 1521 | |
| 1522 | static std::string error_headers() { |
| 1523 | return URLRequestTestJob::test_error_headers(); |
| 1524 | } |
| 1525 | |
| 1526 | MockURLRequestInterceptor() |
| 1527 | : intercept_main_request_(false), restart_main_request_(false), |
| 1528 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1529 | simulate_main_network_error_(false), |
| 1530 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1531 | intercept_final_response_(false), cancel_final_request_(false), |
| 1532 | use_url_request_http_job_(false), |
| 1533 | did_intercept_main_(false), did_restart_main_(false), |
| 1534 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1535 | did_simulate_error_main_(false), |
| 1536 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1537 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1538 | } |
| 1539 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1540 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1541 | |
| 1542 | // URLRequestInterceptor implementation: |
| 1543 | URLRequestJob* MaybeInterceptRequest( |
| 1544 | URLRequest* request, |
| 1545 | NetworkDelegate* network_delegate) const override { |
| 1546 | if (restart_main_request_) { |
| 1547 | restart_main_request_ = false; |
| 1548 | did_restart_main_ = true; |
| 1549 | return new RestartTestJob(request, network_delegate); |
| 1550 | } |
| 1551 | if (cancel_main_request_) { |
| 1552 | cancel_main_request_ = false; |
| 1553 | did_cancel_main_ = true; |
| 1554 | return new CancelTestJob(request, network_delegate); |
| 1555 | } |
| 1556 | if (cancel_then_restart_main_request_) { |
| 1557 | cancel_then_restart_main_request_ = false; |
| 1558 | did_cancel_then_restart_main_ = true; |
| 1559 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1560 | } |
| 1561 | if (simulate_main_network_error_) { |
| 1562 | simulate_main_network_error_ = false; |
| 1563 | did_simulate_error_main_ = true; |
| 1564 | if (use_url_request_http_job_) { |
| 1565 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1566 | } |
| 1567 | // This job will result in error since the requested URL is not one of the |
| 1568 | // URLs supported by these tests. |
| 1569 | return new URLRequestTestJob(request, network_delegate, true); |
| 1570 | } |
| 1571 | if (!intercept_main_request_) |
| 1572 | return nullptr; |
| 1573 | intercept_main_request_ = false; |
| 1574 | did_intercept_main_ = true; |
| 1575 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1576 | network_delegate, |
| 1577 | main_headers_, |
| 1578 | main_data_, |
| 1579 | true); |
| 1580 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1581 | return job; |
| 1582 | } |
| 1583 | |
| 1584 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1585 | NetworkDelegate* network_delegate, |
| 1586 | const GURL& location) const override { |
| 1587 | if (cancel_redirect_request_) { |
| 1588 | cancel_redirect_request_ = false; |
| 1589 | did_cancel_redirect_ = true; |
| 1590 | return new CancelTestJob(request, network_delegate); |
| 1591 | } |
| 1592 | if (!intercept_redirect_) |
| 1593 | return nullptr; |
| 1594 | intercept_redirect_ = false; |
| 1595 | did_intercept_redirect_ = true; |
| 1596 | if (use_url_request_http_job_) { |
| 1597 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1598 | } |
| 1599 | return new URLRequestTestJob(request, |
| 1600 | network_delegate, |
| 1601 | redirect_headers_, |
| 1602 | redirect_data_, |
| 1603 | true); |
| 1604 | } |
| 1605 | |
| 1606 | URLRequestJob* MaybeInterceptResponse( |
| 1607 | URLRequest* request, |
| 1608 | NetworkDelegate* network_delegate) const override { |
| 1609 | if (cancel_final_request_) { |
| 1610 | cancel_final_request_ = false; |
| 1611 | did_cancel_final_ = true; |
| 1612 | return new CancelTestJob(request, network_delegate); |
| 1613 | } |
| 1614 | if (!intercept_final_response_) |
| 1615 | return nullptr; |
| 1616 | intercept_final_response_ = false; |
| 1617 | did_intercept_final_ = true; |
| 1618 | if (use_url_request_http_job_) { |
| 1619 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1620 | } |
| 1621 | return new URLRequestTestJob(request, |
| 1622 | network_delegate, |
| 1623 | final_headers_, |
| 1624 | final_data_, |
| 1625 | true); |
| 1626 | } |
| 1627 | |
| 1628 | void set_intercept_main_request(bool intercept_main_request) { |
| 1629 | intercept_main_request_ = intercept_main_request; |
| 1630 | } |
| 1631 | |
| 1632 | void set_main_headers(const std::string& main_headers) { |
| 1633 | main_headers_ = main_headers; |
| 1634 | } |
| 1635 | |
| 1636 | void set_main_data(const std::string& main_data) { |
| 1637 | main_data_ = main_data; |
| 1638 | } |
| 1639 | |
| 1640 | void set_main_request_load_timing_info( |
| 1641 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1642 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1643 | } |
| 1644 | |
| 1645 | void set_restart_main_request(bool restart_main_request) { |
| 1646 | restart_main_request_ = restart_main_request; |
| 1647 | } |
| 1648 | |
| 1649 | void set_cancel_main_request(bool cancel_main_request) { |
| 1650 | cancel_main_request_ = cancel_main_request; |
| 1651 | } |
| 1652 | |
| 1653 | void set_cancel_then_restart_main_request( |
| 1654 | bool cancel_then_restart_main_request) { |
| 1655 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1656 | } |
| 1657 | |
| 1658 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1659 | simulate_main_network_error_ = simulate_main_network_error; |
| 1660 | } |
| 1661 | |
| 1662 | void set_intercept_redirect(bool intercept_redirect) { |
| 1663 | intercept_redirect_ = intercept_redirect; |
| 1664 | } |
| 1665 | |
| 1666 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1667 | redirect_headers_ = redirect_headers; |
| 1668 | } |
| 1669 | |
| 1670 | void set_redirect_data(const std::string& redirect_data) { |
| 1671 | redirect_data_ = redirect_data; |
| 1672 | } |
| 1673 | |
| 1674 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1675 | cancel_redirect_request_ = cancel_redirect_request; |
| 1676 | } |
| 1677 | |
| 1678 | void set_intercept_final_response(bool intercept_final_response) { |
| 1679 | intercept_final_response_ = intercept_final_response; |
| 1680 | } |
| 1681 | |
| 1682 | void set_final_headers(const std::string& final_headers) { |
| 1683 | final_headers_ = final_headers; |
| 1684 | } |
| 1685 | |
| 1686 | void set_final_data(const std::string& final_data) { |
| 1687 | final_data_ = final_data; |
| 1688 | } |
| 1689 | |
| 1690 | void set_cancel_final_request(bool cancel_final_request) { |
| 1691 | cancel_final_request_ = cancel_final_request; |
| 1692 | } |
| 1693 | |
| 1694 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1695 | use_url_request_http_job_ = use_url_request_http_job; |
| 1696 | } |
| 1697 | |
| 1698 | bool did_intercept_main() const { |
| 1699 | return did_intercept_main_; |
| 1700 | } |
| 1701 | |
| 1702 | bool did_restart_main() const { |
| 1703 | return did_restart_main_; |
| 1704 | } |
| 1705 | |
| 1706 | bool did_cancel_main() const { |
| 1707 | return did_cancel_main_; |
| 1708 | } |
| 1709 | |
| 1710 | bool did_cancel_then_restart_main() const { |
| 1711 | return did_cancel_then_restart_main_; |
| 1712 | } |
| 1713 | |
| 1714 | bool did_simulate_error_main() const { |
| 1715 | return did_simulate_error_main_; |
| 1716 | } |
| 1717 | |
| 1718 | bool did_intercept_redirect() const { |
| 1719 | return did_intercept_redirect_; |
| 1720 | } |
| 1721 | |
| 1722 | bool did_cancel_redirect() const { |
| 1723 | return did_cancel_redirect_; |
| 1724 | } |
| 1725 | |
| 1726 | bool did_intercept_final() const { |
| 1727 | return did_intercept_final_; |
| 1728 | } |
| 1729 | |
| 1730 | bool did_cancel_final() const { |
| 1731 | return did_cancel_final_; |
| 1732 | } |
| 1733 | |
| 1734 | private: |
| 1735 | // Indicate whether to intercept the main request, and if so specify the |
| 1736 | // response to return and the LoadTimingInfo to use. |
| 1737 | mutable bool intercept_main_request_; |
| 1738 | mutable std::string main_headers_; |
| 1739 | mutable std::string main_data_; |
| 1740 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1741 | |
| 1742 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1743 | mutable bool restart_main_request_; |
| 1744 | mutable bool cancel_main_request_; |
| 1745 | mutable bool cancel_then_restart_main_request_; |
| 1746 | mutable bool simulate_main_network_error_; |
| 1747 | |
| 1748 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1749 | // return. |
| 1750 | mutable bool intercept_redirect_; |
| 1751 | mutable std::string redirect_headers_; |
| 1752 | mutable std::string redirect_data_; |
| 1753 | |
| 1754 | // Cancel the request within MaybeInterceptRedirect. |
| 1755 | mutable bool cancel_redirect_request_; |
| 1756 | |
| 1757 | // Indicate whether to intercept the final response, and if so specify the |
| 1758 | // response to return. |
| 1759 | mutable bool intercept_final_response_; |
| 1760 | mutable std::string final_headers_; |
| 1761 | mutable std::string final_data_; |
| 1762 | |
| 1763 | // Cancel the final request within MaybeInterceptResponse. |
| 1764 | mutable bool cancel_final_request_; |
| 1765 | |
| 1766 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1767 | mutable bool use_url_request_http_job_; |
| 1768 | |
| 1769 | // These indicate if the interceptor did something or not. |
| 1770 | mutable bool did_intercept_main_; |
| 1771 | mutable bool did_restart_main_; |
| 1772 | mutable bool did_cancel_main_; |
| 1773 | mutable bool did_cancel_then_restart_main_; |
| 1774 | mutable bool did_simulate_error_main_; |
| 1775 | mutable bool did_intercept_redirect_; |
| 1776 | mutable bool did_cancel_redirect_; |
| 1777 | mutable bool did_intercept_final_; |
| 1778 | mutable bool did_cancel_final_; |
| 1779 | }; |
| 1780 | |
| 1781 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1782 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1783 | public: |
| 1784 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1785 | } |
| 1786 | |
| 1787 | ~URLRequestInterceptorTest() override { |
| 1788 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1789 | base::RunLoop().RunUntilIdle(); |
| 1790 | } |
| 1791 | |
| 1792 | void SetUpFactory() override { |
| 1793 | interceptor_ = new MockURLRequestInterceptor(); |
| 1794 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1795 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | MockURLRequestInterceptor* interceptor() const { |
| 1799 | return interceptor_; |
| 1800 | } |
| 1801 | |
| 1802 | private: |
| 1803 | MockURLRequestInterceptor* interceptor_; |
| 1804 | }; |
| 1805 | |
| 1806 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1807 | // Intercept the main request and respond with a simple response. |
| 1808 | interceptor()->set_intercept_main_request(true); |
| 1809 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1810 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1811 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1812 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1813 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1814 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1815 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1816 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1817 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1818 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1819 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1820 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1821 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1822 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1823 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1824 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1825 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1826 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1827 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1828 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1829 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1830 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1831 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1832 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1833 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1834 | EXPECT_EQ(1, d.response_started_count()); |
| 1835 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1836 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1837 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1838 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1839 | // Intercept the main request and respond with a redirect. |
| 1840 | interceptor()->set_intercept_main_request(true); |
| 1841 | interceptor()->set_main_headers( |
| 1842 | MockURLRequestInterceptor::redirect_headers()); |
| 1843 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1844 | |
| 1845 | // Intercept that redirect and respond with a final OK response. |
| 1846 | interceptor()->set_intercept_redirect(true); |
| 1847 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1848 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1849 | |
| 1850 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1851 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1852 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1853 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1854 | req->set_method("GET"); |
| 1855 | req->Start(); |
| 1856 | base::RunLoop().Run(); |
| 1857 | |
| 1858 | // Check that the interceptor got called as expected. |
| 1859 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1860 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1861 | |
| 1862 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1863 | int status = d.request_status(); |
| 1864 | EXPECT_EQ(OK, status); |
| 1865 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1866 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1867 | |
| 1868 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1869 | EXPECT_EQ(1, d.response_started_count()); |
| 1870 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1871 | } |
| 1872 | |
| 1873 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1874 | // Intercept the main request to generate a server error response. |
| 1875 | interceptor()->set_intercept_main_request(true); |
| 1876 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1877 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1878 | |
| 1879 | // Intercept that error and respond with an OK response. |
| 1880 | interceptor()->set_intercept_final_response(true); |
| 1881 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1882 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1883 | |
| 1884 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1885 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1886 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1887 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1888 | req->set_method("GET"); |
| 1889 | req->Start(); |
| 1890 | base::RunLoop().Run(); |
| 1891 | |
| 1892 | // Check that the interceptor got called as expected. |
| 1893 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1894 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1895 | |
| 1896 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1897 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1898 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1899 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1900 | EXPECT_EQ(1, d.response_started_count()); |
| 1901 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1902 | } |
| 1903 | |
| 1904 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1905 | // Intercept the main request to simulate a network error. |
| 1906 | interceptor()->set_simulate_main_network_error(true); |
| 1907 | |
| 1908 | // Intercept that error and respond with an OK response. |
| 1909 | interceptor()->set_intercept_final_response(true); |
| 1910 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1911 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1912 | |
| 1913 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1914 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1915 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1916 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1917 | req->set_method("GET"); |
| 1918 | req->Start(); |
| 1919 | base::RunLoop().Run(); |
| 1920 | |
| 1921 | // Check that the interceptor got called as expected. |
| 1922 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1923 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1924 | |
| 1925 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1926 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1927 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1928 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1929 | EXPECT_EQ(1, d.response_started_count()); |
| 1930 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1931 | } |
| 1932 | |
| 1933 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1934 | // Restart the main request. |
| 1935 | interceptor()->set_restart_main_request(true); |
| 1936 | |
| 1937 | // then intercept the new main request and respond with an OK response |
| 1938 | interceptor()->set_intercept_main_request(true); |
| 1939 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1940 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1941 | |
| 1942 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1943 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1944 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1945 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1946 | req->set_method("GET"); |
| 1947 | req->Start(); |
| 1948 | base::RunLoop().Run(); |
| 1949 | |
| 1950 | // Check that the interceptor got called as expected. |
| 1951 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1952 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1953 | |
| 1954 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1955 | int status = d.request_status(); |
| 1956 | EXPECT_EQ(OK, status); |
| 1957 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1958 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1959 | |
| 1960 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1961 | EXPECT_EQ(1, d.response_started_count()); |
| 1962 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1963 | } |
| 1964 | |
| 1965 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1966 | // Intercept the main request and cancel from within the restarted job. |
| 1967 | interceptor()->set_cancel_main_request(true); |
| 1968 | |
| 1969 | // Set up to intercept the final response and override it with an OK response. |
| 1970 | interceptor()->set_intercept_final_response(true); |
| 1971 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1972 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1973 | |
| 1974 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1975 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1976 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1977 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1978 | req->set_method("GET"); |
| 1979 | req->Start(); |
| 1980 | base::RunLoop().Run(); |
| 1981 | |
| 1982 | // Check that the interceptor got called as expected. |
| 1983 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1984 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1985 | |
| 1986 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1987 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1991 | // Intercept the main request and respond with a redirect. |
| 1992 | interceptor()->set_intercept_main_request(true); |
| 1993 | interceptor()->set_main_headers( |
| 1994 | MockURLRequestInterceptor::redirect_headers()); |
| 1995 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1996 | |
| 1997 | // Intercept the redirect and cancel from within that job. |
| 1998 | interceptor()->set_cancel_redirect_request(true); |
| 1999 | |
| 2000 | // Set up to intercept the final response and override it with an OK response. |
| 2001 | interceptor()->set_intercept_final_response(true); |
| 2002 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2003 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2004 | |
| 2005 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2006 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2007 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2008 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2009 | req->set_method("GET"); |
| 2010 | req->Start(); |
| 2011 | base::RunLoop().Run(); |
| 2012 | |
| 2013 | // Check that the interceptor got called as expected. |
| 2014 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2015 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2016 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2017 | |
| 2018 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2019 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2023 | // Intercept the main request to simulate a network error. |
| 2024 | interceptor()->set_simulate_main_network_error(true); |
| 2025 | |
| 2026 | // Set up to intercept final the response and cancel from within that job. |
| 2027 | interceptor()->set_cancel_final_request(true); |
| 2028 | |
| 2029 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2030 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2031 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2032 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2033 | req->set_method("GET"); |
| 2034 | req->Start(); |
| 2035 | base::RunLoop().Run(); |
| 2036 | |
| 2037 | // Check that the interceptor got called as expected. |
| 2038 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2039 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2040 | |
| 2041 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2042 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2046 | // Intercept the main request and cancel then restart from within that job. |
| 2047 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2048 | |
| 2049 | // Set up to intercept the final response and override it with an OK response. |
| 2050 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2051 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2052 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2053 | |
| 2054 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2055 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2056 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2057 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2058 | req->set_method("GET"); |
| 2059 | req->Start(); |
| 2060 | base::RunLoop().Run(); |
| 2061 | |
| 2062 | // Check that the interceptor got called as expected. |
| 2063 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2064 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2065 | |
| 2066 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2067 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2071 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2072 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2073 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2074 | int connect_time_flags, |
| 2075 | bool used_proxy) { |
| 2076 | LoadTimingInfo load_timing; |
| 2077 | load_timing.socket_log_id = 1; |
| 2078 | |
| 2079 | if (used_proxy) { |
| 2080 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2081 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2082 | } |
| 2083 | |
| 2084 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2085 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2086 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2087 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2088 | } |
| 2089 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2090 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2091 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2092 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2093 | } |
| 2094 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2095 | |
| 2096 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2097 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2098 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2099 | return load_timing; |
| 2100 | } |
| 2101 | |
| 2102 | // Same as above, but in the case of a reused socket. |
| 2103 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2104 | bool used_proxy) { |
| 2105 | LoadTimingInfo load_timing; |
| 2106 | load_timing.socket_log_id = 1; |
| 2107 | load_timing.socket_reused = true; |
| 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 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2115 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2116 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2117 | return load_timing; |
| 2118 | } |
| 2119 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2120 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2121 | const LoadTimingInfo& job_load_timing, |
| 2122 | const URLRequestContext& context, |
| 2123 | MockURLRequestInterceptor* interceptor) { |
| 2124 | interceptor->set_intercept_main_request(true); |
| 2125 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2126 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2127 | std::unique_ptr<URLRequest> req( |
| 2128 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2129 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2130 | req->Start(); |
| 2131 | base::RunLoop().Run(); |
| 2132 | |
| 2133 | LoadTimingInfo resulting_load_timing; |
| 2134 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2135 | |
| 2136 | // None of these should be modified by the URLRequest. |
| 2137 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2138 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2139 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2140 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 2141 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2142 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2143 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2144 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2145 | |
| 2146 | return resulting_load_timing; |
| 2147 | } |
| 2148 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2149 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2150 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2151 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2152 | LoadTimingInfo job_load_timing = |
| 2153 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2154 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2155 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2156 | RunURLRequestInterceptorLoadTimingTest( |
| 2157 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2158 | |
| 2159 | // Nothing should have been changed by the URLRequest. |
| 2160 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2161 | load_timing_result.proxy_resolve_start); |
| 2162 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2163 | load_timing_result.proxy_resolve_end); |
| 2164 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2165 | load_timing_result.connect_timing.dns_start); |
| 2166 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2167 | load_timing_result.connect_timing.dns_end); |
| 2168 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2169 | load_timing_result.connect_timing.connect_start); |
| 2170 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2171 | load_timing_result.connect_timing.connect_end); |
| 2172 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2173 | load_timing_result.connect_timing.ssl_start); |
| 2174 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2175 | load_timing_result.connect_timing.ssl_end); |
| 2176 | |
| 2177 | // Redundant sanity check. |
| 2178 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2179 | } |
| 2180 | |
| 2181 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2182 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2183 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2184 | LoadTimingInfo job_load_timing = |
| 2185 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2186 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2187 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2188 | RunURLRequestInterceptorLoadTimingTest( |
| 2189 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2190 | |
| 2191 | // Nothing should have been changed by the URLRequest. |
| 2192 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2193 | load_timing_result.proxy_resolve_start); |
| 2194 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2195 | load_timing_result.proxy_resolve_end); |
| 2196 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2197 | load_timing_result.connect_timing.dns_start); |
| 2198 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2199 | load_timing_result.connect_timing.dns_end); |
| 2200 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2201 | load_timing_result.connect_timing.connect_start); |
| 2202 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2203 | load_timing_result.connect_timing.connect_end); |
| 2204 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2205 | load_timing_result.connect_timing.ssl_start); |
| 2206 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2207 | load_timing_result.connect_timing.ssl_end); |
| 2208 | |
| 2209 | // Redundant sanity check. |
| 2210 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2211 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2212 | } |
| 2213 | |
| 2214 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2215 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2216 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2217 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2218 | // |
| 2219 | // 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] | 2220 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2221 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2222 | LoadTimingInfo job_load_timing = |
| 2223 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2224 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2225 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2226 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2227 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2228 | job_load_timing.connect_timing.connect_start = |
| 2229 | now - base::TimeDelta::FromDays(2); |
| 2230 | job_load_timing.connect_timing.connect_end = |
| 2231 | now - base::TimeDelta::FromDays(1); |
| 2232 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2233 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2234 | RunURLRequestInterceptorLoadTimingTest( |
| 2235 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2236 | |
| 2237 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2238 | // request_start. |
| 2239 | EXPECT_EQ(load_timing_result.request_start, |
| 2240 | load_timing_result.proxy_resolve_start); |
| 2241 | EXPECT_EQ(load_timing_result.request_start, |
| 2242 | load_timing_result.proxy_resolve_end); |
| 2243 | EXPECT_EQ(load_timing_result.request_start, |
| 2244 | load_timing_result.connect_timing.dns_start); |
| 2245 | EXPECT_EQ(load_timing_result.request_start, |
| 2246 | load_timing_result.connect_timing.dns_end); |
| 2247 | EXPECT_EQ(load_timing_result.request_start, |
| 2248 | load_timing_result.connect_timing.connect_start); |
| 2249 | EXPECT_EQ(load_timing_result.request_start, |
| 2250 | load_timing_result.connect_timing.connect_end); |
| 2251 | |
| 2252 | // Other times should have been left null. |
| 2253 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2254 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2255 | } |
| 2256 | |
| 2257 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2258 | TEST_F(URLRequestInterceptorTest, |
| 2259 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2260 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2261 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2262 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2263 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2264 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2265 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2266 | RunURLRequestInterceptorLoadTimingTest( |
| 2267 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2268 | |
| 2269 | // Proxy times and connect times should all be replaced with request_start. |
| 2270 | EXPECT_EQ(load_timing_result.request_start, |
| 2271 | load_timing_result.proxy_resolve_start); |
| 2272 | EXPECT_EQ(load_timing_result.request_start, |
| 2273 | load_timing_result.proxy_resolve_end); |
| 2274 | |
| 2275 | // Other times should have been left null. |
| 2276 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2277 | } |
| 2278 | |
| 2279 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2280 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2281 | // not considered reused in this test (May be a preconnect). |
| 2282 | // |
| 2283 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2284 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2285 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2286 | LoadTimingInfo job_load_timing = |
| 2287 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2288 | job_load_timing.connect_timing.connect_start = |
| 2289 | now - base::TimeDelta::FromDays(1); |
| 2290 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2291 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2292 | job_load_timing.connect_timing.connect_end = |
| 2293 | now - base::TimeDelta::FromDays(4); |
| 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 | // Connect times, and SSL times should be replaced with request_start. |
| 2300 | EXPECT_EQ(load_timing_result.request_start, |
| 2301 | load_timing_result.connect_timing.connect_start); |
| 2302 | EXPECT_EQ(load_timing_result.request_start, |
| 2303 | load_timing_result.connect_timing.ssl_start); |
| 2304 | EXPECT_EQ(load_timing_result.request_start, |
| 2305 | load_timing_result.connect_timing.ssl_end); |
| 2306 | EXPECT_EQ(load_timing_result.request_start, |
| 2307 | load_timing_result.connect_timing.connect_end); |
| 2308 | |
| 2309 | // Other times should have been left null. |
| 2310 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2311 | } |
| 2312 | |
| 2313 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2314 | // |request_start|, due to reusing a connected socket in the case that there |
| 2315 | // are also proxy times. The connected socket is not considered reused in this |
| 2316 | // test (May be a preconnect). |
| 2317 | // |
| 2318 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2319 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2320 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2321 | LoadTimingInfo job_load_timing = |
| 2322 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2323 | job_load_timing.connect_timing.connect_start = |
| 2324 | now - base::TimeDelta::FromDays(1); |
| 2325 | job_load_timing.connect_timing.connect_end = |
| 2326 | now - base::TimeDelta::FromDays(2); |
| 2327 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2328 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2329 | RunURLRequestInterceptorLoadTimingTest( |
| 2330 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2331 | |
| 2332 | // Connect times should be replaced with proxy_resolve_end. |
| 2333 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2334 | load_timing_result.connect_timing.connect_start); |
| 2335 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2336 | load_timing_result.connect_timing.connect_end); |
| 2337 | |
| 2338 | // Other times should have been left null. |
| 2339 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2340 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2341 | } |
| 2342 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2343 | // Check that two different URL requests have different identifiers. |
| 2344 | TEST_F(URLRequestTest, Identifiers) { |
| 2345 | TestDelegate d; |
| 2346 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2347 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2348 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2349 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2350 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2351 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2352 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2353 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2354 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2355 | } |
| 2356 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2357 | #if defined(OS_IOS) |
| 2358 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2359 | // the network delegate. crbug.com/496743 |
| 2360 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2361 | #else |
| 2362 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2363 | #endif |
| 2364 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2365 | MockHostResolver host_resolver; |
| 2366 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2367 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2368 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2369 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2370 | |
| 2371 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2372 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2373 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2374 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2375 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2376 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2377 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2378 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2379 | |
| 2380 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2381 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2382 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2383 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2384 | |
| 2385 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2386 | EXPECT_THAT(network_delegate.last_error(), |
| 2387 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2388 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2389 | } |
| 2390 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2391 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2392 | // content is empty. |
| 2393 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2394 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2395 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2396 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2397 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2398 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2399 | EXPECT_EQ("", d.data_received()); |
| 2400 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2401 | } |
| 2402 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2403 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2404 | // correctly, both before and after start. |
| 2405 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2406 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2407 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2408 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2409 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2410 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2411 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2412 | req->SetPriority(LOW); |
| 2413 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2414 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2415 | req->Start(); |
| 2416 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2417 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2418 | req->SetPriority(MEDIUM); |
| 2419 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2423 | // Start on it. |
| 2424 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2425 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2426 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2427 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2428 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2429 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2430 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2431 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2432 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2433 | req.get(), &default_network_delegate_, &job_priority)); |
| 2434 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2435 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2436 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2437 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2438 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2439 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2440 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | // Make sure that URLRequest passes on its priority updates to its |
| 2444 | // job. |
| 2445 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2446 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2447 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2448 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2449 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2450 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2451 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2452 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2453 | req.get(), &default_network_delegate_, &job_priority)); |
| 2454 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2455 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2456 | req->SetPriority(LOW); |
| 2457 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2458 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2459 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2460 | req->SetPriority(MEDIUM); |
| 2461 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2462 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2463 | } |
| 2464 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2465 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2466 | // is MAXIMUM_PRIORITY. |
| 2467 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2468 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2469 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2470 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2471 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2472 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2473 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2474 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2475 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2476 | req.get(), &default_network_delegate_, &job_priority)); |
| 2477 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2478 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2479 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2480 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2482 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2483 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2485 | req->Start(); |
| 2486 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2487 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2488 | } |
| 2489 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2490 | namespace { |
| 2491 | |
| 2492 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2493 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2494 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2495 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2496 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2497 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2498 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2499 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2500 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2501 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2502 | } // namespace |
| 2503 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2504 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2505 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2506 | ASSERT_TRUE(test_server.Start()); |
| 2507 | |
| 2508 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2509 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2510 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2511 | |
| 2512 | // Set up a cookie. |
| 2513 | { |
| 2514 | TestNetworkDelegate network_delegate; |
| 2515 | context.set_network_delegate(&network_delegate); |
| 2516 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2517 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2518 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2519 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2520 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2521 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2522 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2523 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2524 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2525 | } |
| 2526 | |
| 2527 | // Verify that the cookie is set. |
| 2528 | { |
| 2529 | TestNetworkDelegate network_delegate; |
| 2530 | context.set_network_delegate(&network_delegate); |
| 2531 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2532 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2533 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2534 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2535 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2536 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2537 | |
| 2538 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2539 | != std::string::npos); |
| 2540 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2541 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2542 | } |
| 2543 | } |
| 2544 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2545 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2546 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2547 | ASSERT_TRUE(test_server.Start()); |
| 2548 | |
| 2549 | // Set up a cookie. |
| 2550 | { |
| 2551 | TestNetworkDelegate network_delegate; |
| 2552 | default_context_.set_network_delegate(&network_delegate); |
| 2553 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2554 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2555 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2556 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2557 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2558 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2559 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2560 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2561 | } |
| 2562 | |
| 2563 | // Verify that the cookie is set. |
| 2564 | { |
| 2565 | TestNetworkDelegate network_delegate; |
| 2566 | default_context_.set_network_delegate(&network_delegate); |
| 2567 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2568 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2569 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2570 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2571 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2572 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2573 | |
| 2574 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2575 | != std::string::npos); |
| 2576 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2577 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2578 | } |
| 2579 | |
| 2580 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2581 | { |
| 2582 | TestNetworkDelegate network_delegate; |
| 2583 | default_context_.set_network_delegate(&network_delegate); |
| 2584 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2585 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2586 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2587 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2588 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2589 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2590 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2591 | |
| 2592 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2593 | == std::string::npos); |
| 2594 | |
| 2595 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2596 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2597 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2602 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2603 | ASSERT_TRUE(test_server.Start()); |
| 2604 | |
| 2605 | // Set up a cookie. |
| 2606 | { |
| 2607 | TestNetworkDelegate network_delegate; |
| 2608 | default_context_.set_network_delegate(&network_delegate); |
| 2609 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2610 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2611 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2612 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2613 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2614 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2615 | |
| 2616 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2617 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2618 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2619 | } |
| 2620 | |
| 2621 | // Try to set-up another cookie and update the previous cookie. |
| 2622 | { |
| 2623 | TestNetworkDelegate network_delegate; |
| 2624 | default_context_.set_network_delegate(&network_delegate); |
| 2625 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2626 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2627 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2628 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2629 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2630 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2631 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2632 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2633 | |
| 2634 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2635 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2636 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2637 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2638 | } |
| 2639 | |
| 2640 | // Verify the cookies weren't saved or updated. |
| 2641 | { |
| 2642 | TestNetworkDelegate network_delegate; |
| 2643 | default_context_.set_network_delegate(&network_delegate); |
| 2644 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2645 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2646 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2647 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2648 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2649 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2650 | |
| 2651 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2652 | == std::string::npos); |
| 2653 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2654 | != std::string::npos); |
| 2655 | |
| 2656 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2657 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2658 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2663 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2664 | ASSERT_TRUE(test_server.Start()); |
| 2665 | |
| 2666 | // Set up a cookie. |
| 2667 | { |
| 2668 | TestNetworkDelegate network_delegate; |
| 2669 | default_context_.set_network_delegate(&network_delegate); |
| 2670 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2671 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2672 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2673 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2674 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2675 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2676 | |
| 2677 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2678 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2679 | } |
| 2680 | |
| 2681 | // Verify that the cookie is set. |
| 2682 | { |
| 2683 | TestNetworkDelegate network_delegate; |
| 2684 | default_context_.set_network_delegate(&network_delegate); |
| 2685 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2686 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2687 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2688 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2689 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2690 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2691 | |
| 2692 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2693 | != std::string::npos); |
| 2694 | |
| 2695 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2696 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2697 | } |
| 2698 | |
| 2699 | // Verify that the cookie isn't sent. |
| 2700 | { |
| 2701 | TestNetworkDelegate network_delegate; |
| 2702 | default_context_.set_network_delegate(&network_delegate); |
| 2703 | TestDelegate d; |
| 2704 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2705 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2706 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2707 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2708 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2709 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2710 | |
| 2711 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2712 | == std::string::npos); |
| 2713 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2714 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2715 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2716 | } |
| 2717 | } |
| 2718 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2719 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2720 | #if defined(OS_IOS) |
| 2721 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2722 | #else |
| 2723 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2724 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2725 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2726 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2727 | ASSERT_TRUE(test_server.Start()); |
| 2728 | |
| 2729 | // Set up a cookie. |
| 2730 | { |
| 2731 | TestNetworkDelegate network_delegate; |
| 2732 | default_context_.set_network_delegate(&network_delegate); |
| 2733 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2734 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2735 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2736 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2737 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2738 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2739 | |
| 2740 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2741 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2742 | } |
| 2743 | |
| 2744 | // Try to set-up another cookie and update the previous cookie. |
| 2745 | { |
| 2746 | TestNetworkDelegate network_delegate; |
| 2747 | default_context_.set_network_delegate(&network_delegate); |
| 2748 | TestDelegate d; |
| 2749 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2750 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2751 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2752 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2753 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2754 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2755 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2756 | |
| 2757 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2758 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2759 | } |
| 2760 | |
| 2761 | // Verify the cookies weren't saved or updated. |
| 2762 | { |
| 2763 | TestNetworkDelegate network_delegate; |
| 2764 | default_context_.set_network_delegate(&network_delegate); |
| 2765 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2766 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2767 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2768 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2769 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2770 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2771 | |
| 2772 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2773 | == std::string::npos); |
| 2774 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2775 | != std::string::npos); |
| 2776 | |
| 2777 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2778 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2783 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2784 | ASSERT_TRUE(test_server.Start()); |
| 2785 | |
| 2786 | // Set up an empty cookie. |
| 2787 | { |
| 2788 | TestNetworkDelegate network_delegate; |
| 2789 | default_context_.set_network_delegate(&network_delegate); |
| 2790 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2791 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2792 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2793 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2794 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2795 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2796 | |
| 2797 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2798 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2799 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2804 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2805 | ASSERT_TRUE(test_server.Start()); |
| 2806 | |
| 2807 | // Set up a cookie. |
| 2808 | { |
| 2809 | TestNetworkDelegate network_delegate; |
| 2810 | default_context_.set_network_delegate(&network_delegate); |
| 2811 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2812 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2813 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2814 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2815 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2816 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2817 | |
| 2818 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2819 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2820 | } |
| 2821 | |
| 2822 | // Verify that the cookie is set. |
| 2823 | { |
| 2824 | TestNetworkDelegate network_delegate; |
| 2825 | default_context_.set_network_delegate(&network_delegate); |
| 2826 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2827 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2828 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2829 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2830 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2831 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2832 | |
| 2833 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2834 | != std::string::npos); |
| 2835 | |
| 2836 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2837 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2838 | } |
| 2839 | |
| 2840 | // Verify that the cookie isn't sent. |
| 2841 | { |
| 2842 | TestNetworkDelegate network_delegate; |
| 2843 | default_context_.set_network_delegate(&network_delegate); |
| 2844 | TestDelegate d; |
| 2845 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2846 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2847 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2848 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2849 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2850 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2851 | |
| 2852 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2853 | == std::string::npos); |
| 2854 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2855 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2856 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2861 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2862 | ASSERT_TRUE(test_server.Start()); |
| 2863 | |
| 2864 | // Set up a cookie. |
| 2865 | { |
| 2866 | TestNetworkDelegate network_delegate; |
| 2867 | default_context_.set_network_delegate(&network_delegate); |
| 2868 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2869 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2870 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2871 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2872 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2873 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2874 | |
| 2875 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2876 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2877 | } |
| 2878 | |
| 2879 | // Try to set-up another cookie and update the previous cookie. |
| 2880 | { |
| 2881 | TestNetworkDelegate network_delegate; |
| 2882 | default_context_.set_network_delegate(&network_delegate); |
| 2883 | TestDelegate d; |
| 2884 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2885 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2886 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2887 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2888 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2889 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2890 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2891 | |
| 2892 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2893 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2894 | } |
| 2895 | |
| 2896 | // Verify the cookies weren't saved or updated. |
| 2897 | { |
| 2898 | TestNetworkDelegate network_delegate; |
| 2899 | default_context_.set_network_delegate(&network_delegate); |
| 2900 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2901 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2902 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2903 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2904 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2905 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2906 | |
| 2907 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2908 | == std::string::npos); |
| 2909 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2910 | != std::string::npos); |
| 2911 | |
| 2912 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2913 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2914 | } |
| 2915 | } |
| 2916 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2917 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2918 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2919 | ASSERT_TRUE(test_server.Start()); |
| 2920 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2921 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2922 | default_context_.set_network_delegate(&network_delegate); |
| 2923 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2924 | const std::string kHost = "example.test"; |
| 2925 | const std::string kSubHost = "subdomain.example.test"; |
| 2926 | const std::string kCrossHost = "cross-origin.test"; |
| 2927 | |
| 2928 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2929 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2930 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2931 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2932 | test_server.GetURL(kHost, |
| 2933 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2934 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2935 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2936 | req->Start(); |
| 2937 | base::RunLoop().Run(); |
| 2938 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2939 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2940 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2941 | } |
| 2942 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2943 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2944 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2945 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2946 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2947 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2948 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2949 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 2950 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2951 | req->Start(); |
| 2952 | base::RunLoop().Run(); |
| 2953 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2954 | EXPECT_NE(std::string::npos, |
| 2955 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2956 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2957 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2958 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2959 | } |
| 2960 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2961 | // Verify that both cookies are sent when the request has no initiator (can |
| 2962 | // happen for main frame browser-initiated navigations). |
| 2963 | { |
| 2964 | TestDelegate d; |
| 2965 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2966 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2967 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2968 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 2969 | req->Start(); |
| 2970 | base::RunLoop().Run(); |
| 2971 | |
| 2972 | EXPECT_NE(std::string::npos, |
| 2973 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2974 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 2975 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2976 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2977 | } |
| 2978 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2979 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2980 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2981 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2982 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2983 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2984 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2985 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 2986 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2987 | req->Start(); |
| 2988 | base::RunLoop().Run(); |
| 2989 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2990 | EXPECT_NE(std::string::npos, |
| 2991 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2992 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2993 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2994 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2995 | } |
| 2996 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2997 | // Verify that neither cookie is not sent for cross-site requests. |
| 2998 | { |
| 2999 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3000 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3001 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3002 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3003 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3004 | req->set_initiator( |
| 3005 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3006 | req->Start(); |
| 3007 | base::RunLoop().Run(); |
| 3008 | |
| 3009 | EXPECT_EQ(std::string::npos, |
| 3010 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3011 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3012 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3013 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3014 | } |
| 3015 | |
| 3016 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3017 | // method is "safe". |
| 3018 | { |
| 3019 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3020 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3021 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3022 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3023 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3024 | req->set_initiator( |
| 3025 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3026 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3027 | req->Start(); |
| 3028 | base::RunLoop().Run(); |
| 3029 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3030 | EXPECT_EQ(std::string::npos, |
| 3031 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3032 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3033 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3034 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3035 | } |
| 3036 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3037 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3038 | // method is unsafe (e.g. POST). |
| 3039 | { |
| 3040 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3041 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3042 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3043 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3044 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3045 | req->set_initiator( |
| 3046 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3047 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3048 | req->Start(); |
| 3049 | base::RunLoop().Run(); |
| 3050 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3051 | EXPECT_EQ(std::string::npos, |
| 3052 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3053 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3054 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3055 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3056 | } |
| 3057 | } |
| 3058 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3059 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3060 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3061 | EmbeddedTestServer http_server; |
| 3062 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3063 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3064 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3065 | https_server.AddDefaultHandlers( |
| 3066 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3067 | ASSERT_TRUE(http_server.Start()); |
| 3068 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3069 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3070 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3071 | TestURLRequestContext context(true); |
| 3072 | context.set_network_delegate(&network_delegate); |
| 3073 | context.Init(); |
| 3074 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3075 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3076 | { |
| 3077 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3078 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3079 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3080 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3081 | req->Start(); |
| 3082 | base::RunLoop().Run(); |
| 3083 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3084 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3085 | } |
| 3086 | |
| 3087 | // Verify that the cookie is not set. |
| 3088 | { |
| 3089 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3090 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3091 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3092 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3093 | req->Start(); |
| 3094 | base::RunLoop().Run(); |
| 3095 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3096 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3097 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3098 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3099 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3100 | } |
| 3101 | } |
| 3102 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3103 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3104 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3105 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3106 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3107 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3108 | |
| 3109 | TestNetworkDelegate network_delegate; |
| 3110 | TestURLRequestContext context(true); |
| 3111 | context.set_network_delegate(&network_delegate); |
| 3112 | context.Init(); |
| 3113 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3114 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3115 | { |
| 3116 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3117 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3118 | https_server.GetURL("/set-cookie?__Secure-foo=1"), 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 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3123 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3124 | } |
| 3125 | |
| 3126 | // Verify that the cookie is not set. |
| 3127 | { |
| 3128 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3129 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3130 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3131 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3132 | req->Start(); |
| 3133 | base::RunLoop().Run(); |
| 3134 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3135 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3136 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3137 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3138 | } |
| 3139 | } |
| 3140 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3141 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3142 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3143 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3144 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3145 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3146 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3147 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3148 | TestURLRequestContext context(true); |
| 3149 | context.set_network_delegate(&network_delegate); |
| 3150 | context.Init(); |
| 3151 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3152 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3153 | { |
| 3154 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3155 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3156 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3157 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3158 | req->Start(); |
| 3159 | base::RunLoop().Run(); |
| 3160 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3161 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3162 | } |
| 3163 | |
| 3164 | // Verify that the cookie is set. |
| 3165 | { |
| 3166 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3167 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3168 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3169 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3170 | req->Start(); |
| 3171 | base::RunLoop().Run(); |
| 3172 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3173 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3174 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3175 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3180 | // cookies are enabled. |
| 3181 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3182 | EmbeddedTestServer http_server; |
| 3183 | http_server.AddDefaultHandlers( |
| 3184 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3185 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3186 | https_server.AddDefaultHandlers( |
| 3187 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3188 | ASSERT_TRUE(http_server.Start()); |
| 3189 | ASSERT_TRUE(https_server.Start()); |
| 3190 | |
| 3191 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3192 | TestURLRequestContext context(true); |
| 3193 | context.set_network_delegate(&network_delegate); |
| 3194 | context.Init(); |
| 3195 | |
| 3196 | // Try to set a Secure cookie, with experimental features enabled. |
| 3197 | { |
| 3198 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3199 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3200 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3201 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3202 | req->Start(); |
| 3203 | base::RunLoop().Run(); |
| 3204 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3205 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3206 | } |
| 3207 | |
| 3208 | // Verify that the cookie is not set. |
| 3209 | { |
| 3210 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3211 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3212 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3213 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3214 | req->Start(); |
| 3215 | base::RunLoop().Run(); |
| 3216 | |
| 3217 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3218 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3219 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 3224 | // cookies are enabled. |
| 3225 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 3226 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3227 | https_server.AddDefaultHandlers( |
| 3228 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3229 | ASSERT_TRUE(https_server.Start()); |
| 3230 | |
| 3231 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3232 | TestURLRequestContext context(true); |
| 3233 | context.set_network_delegate(&network_delegate); |
| 3234 | context.Init(); |
| 3235 | |
| 3236 | // Try to set a Secure cookie, with experimental features enabled. |
| 3237 | { |
| 3238 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3239 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3240 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3241 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3242 | req->Start(); |
| 3243 | base::RunLoop().Run(); |
| 3244 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3245 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3246 | } |
| 3247 | |
| 3248 | // Verify that the cookie is not set. |
| 3249 | { |
| 3250 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3251 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3252 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3253 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3254 | req->Start(); |
| 3255 | base::RunLoop().Run(); |
| 3256 | |
| 3257 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3258 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3259 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3260 | } |
| 3261 | } |
| 3262 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3263 | TEST_F(URLRequestTest, CookieAgeMetrics) { |
| 3264 | EmbeddedTestServer http_server; |
| 3265 | http_server.AddDefaultHandlers( |
| 3266 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3267 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3268 | https_server.AddDefaultHandlers( |
| 3269 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3270 | ASSERT_TRUE(http_server.Start()); |
| 3271 | ASSERT_TRUE(https_server.Start()); |
| 3272 | |
| 3273 | TestNetworkDelegate network_delegate; |
| 3274 | default_context_.set_network_delegate(&network_delegate); |
| 3275 | base::HistogramTester histograms; |
| 3276 | |
| 3277 | const std::string kHost = "example.test"; |
| 3278 | const std::string kCrossHost = "cross-origin.test"; |
| 3279 | |
| 3280 | // Set a test cookie. |
| 3281 | { |
| 3282 | TestDelegate d; |
| 3283 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3284 | http_server.GetURL(kHost, "/set-cookie?cookie=value&cookie2=value2"), |
| 3285 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3286 | req->Start(); |
| 3287 | base::RunLoop().Run(); |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3288 | ASSERT_EQ(2, network_delegate.set_cookie_count()); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3289 | } |
| 3290 | |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3291 | // Make a secure same-site request. |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3292 | { |
| 3293 | TestDelegate d; |
| 3294 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3295 | https_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3296 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3297 | req->set_site_for_cookies(https_server.GetURL(kHost, "/")); |
| 3298 | req->set_initiator(url::Origin::Create(https_server.GetURL(kHost, "/"))); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3299 | req->Start(); |
| 3300 | base::RunLoop().Run(); |
| 3301 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3302 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3303 | histograms.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3304 | histograms.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 1); |
| 3305 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3306 | 0); |
| 3307 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", 0); |
| 3308 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3309 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 2); |
| 3310 | } |
| 3311 | |
| 3312 | // Make a secure cross-site request. |
| 3313 | { |
| 3314 | TestDelegate d; |
| 3315 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3316 | https_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3317 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3318 | req->set_site_for_cookies(https_server.GetURL(kCrossHost, "/")); |
| 3319 | req->set_initiator( |
| 3320 | url::Origin::Create(https_server.GetURL(kCrossHost, "/"))); |
| 3321 | req->Start(); |
| 3322 | base::RunLoop().Run(); |
| 3323 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3324 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3325 | histograms.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 1); |
| 3326 | histograms.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 1); |
| 3327 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3328 | 0); |
| 3329 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", 0); |
| 3330 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 2); |
| 3331 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // Make a non-secure same-site request. |
| 3335 | { |
| 3336 | TestDelegate d; |
| 3337 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3338 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3339 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3340 | req->set_site_for_cookies(http_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3341 | req->set_initiator(url::Origin::Create(http_server.GetURL(kHost, "/"))); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3342 | req->Start(); |
| 3343 | base::RunLoop().Run(); |
| 3344 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3345 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3346 | histograms.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 1); |
| 3347 | histograms.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 1); |
| 3348 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3349 | 0); |
| 3350 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", 2); |
| 3351 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 2); |
| 3352 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Make a non-secure cross-site request. |
| 3356 | { |
| 3357 | TestDelegate d; |
| 3358 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3359 | http_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3360 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3361 | req->set_site_for_cookies(http_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3362 | req->set_initiator( |
| 3363 | url::Origin::Create(http_server.GetURL(kCrossHost, "/"))); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3364 | req->Start(); |
| 3365 | base::RunLoop().Run(); |
| 3366 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 1); |
| 3367 | histograms.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 1); |
Thiemo Nagel | 6c783dbf | 2018-03-21 13:53:30 | [diff] [blame] | 3368 | histograms.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 1); |
| 3369 | histograms.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 1); |
| 3370 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3371 | 2); |
| 3372 | histograms.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", 2); |
| 3373 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 2); |
| 3374 | histograms.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3378 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3379 | // rather than a spawned test server because the connection used to talk to |
| 3380 | // the test server is affected by entering suspend mode on Android. |
| 3381 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3382 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3383 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3384 | |
| 3385 | URLRequestFailedJob::AddUrlHandler(); |
| 3386 | |
| 3387 | TestDelegate d; |
| 3388 | // Request that just hangs. |
| 3389 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3390 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3391 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3392 | r->Start(); |
| 3393 | |
| 3394 | power_monitor_source->Suspend(); |
| 3395 | // Wait for the suspend notification to cause the request to fail. |
| 3396 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3397 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3398 | EXPECT_TRUE(d.request_failed()); |
| 3399 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3400 | |
| 3401 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3402 | |
| 3403 | // Shouldn't be needed, but just in case. |
| 3404 | power_monitor_source->Resume(); |
| 3405 | } |
| 3406 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3407 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3408 | // value for the |fixed_date| argument given to the constructor. |
| 3409 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3410 | public: |
| 3411 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3412 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3413 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3414 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3415 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3416 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3417 | URLRequest* request, |
| 3418 | const CompletionCallback& callback, |
| 3419 | const HttpResponseHeaders* original_response_headers, |
| 3420 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3421 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3422 | |
| 3423 | private: |
| 3424 | std::string fixed_date_; |
| 3425 | |
| 3426 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3427 | }; |
| 3428 | |
| 3429 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3430 | URLRequest* request, |
| 3431 | const CompletionCallback& callback, |
| 3432 | const HttpResponseHeaders* original_response_headers, |
| 3433 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3434 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3435 | HttpResponseHeaders* new_response_headers = |
| 3436 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3437 | |
| 3438 | new_response_headers->RemoveHeader("Date"); |
| 3439 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3440 | |
| 3441 | *override_response_headers = new_response_headers; |
| 3442 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3443 | callback, |
| 3444 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3445 | override_response_headers, |
| 3446 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | // Test that cookie expiration times are adjusted for server/client clock |
| 3450 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3451 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3452 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3453 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3454 | ASSERT_TRUE(test_server.Start()); |
| 3455 | |
| 3456 | // Set up an expired cookie. |
| 3457 | { |
| 3458 | TestNetworkDelegate network_delegate; |
| 3459 | default_context_.set_network_delegate(&network_delegate); |
| 3460 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3461 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3462 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3463 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3464 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3465 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3466 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3467 | } |
| 3468 | // Verify that the cookie is not set. |
| 3469 | { |
| 3470 | TestNetworkDelegate network_delegate; |
| 3471 | default_context_.set_network_delegate(&network_delegate); |
| 3472 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3473 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3474 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3475 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3476 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3477 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3478 | |
| 3479 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3480 | } |
| 3481 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3482 | { |
| 3483 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3484 | default_context_.set_network_delegate(&network_delegate); |
| 3485 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3486 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3487 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3488 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3489 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3490 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3491 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3492 | } |
| 3493 | // Verify that the cookie is set. |
| 3494 | { |
| 3495 | TestNetworkDelegate network_delegate; |
| 3496 | default_context_.set_network_delegate(&network_delegate); |
| 3497 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3498 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3499 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3500 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3501 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3502 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3503 | |
| 3504 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3509 | // Check that it is impossible to change the referrer in the extra headers of |
| 3510 | // an URLRequest. |
| 3511 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3512 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3513 | ASSERT_TRUE(test_server.Start()); |
| 3514 | |
| 3515 | // If extra headers contain referer and the request contains a referer, |
| 3516 | // only the latter shall be respected. |
| 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] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3522 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3523 | |
| 3524 | HttpRequestHeaders headers; |
| 3525 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3526 | req->SetExtraRequestHeaders(headers); |
[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("http://foo.com/", d.data_received()); |
| 3532 | } |
| 3533 | |
| 3534 | // If extra headers contain a referer but the request does not, no referer |
| 3535 | // shall be sent in the header. |
| 3536 | { |
| 3537 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3538 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3539 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3540 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3541 | |
| 3542 | HttpRequestHeaders headers; |
| 3543 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3544 | req->SetExtraRequestHeaders(headers); |
| 3545 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3547 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3548 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3549 | |
| 3550 | EXPECT_EQ("None", d.data_received()); |
| 3551 | } |
| 3552 | } |
| 3553 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3554 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3555 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3556 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3557 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3558 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3559 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3560 | // |request_method| is the method to use for the initial request. |
| 3561 | // |redirect_method| is the method that is expected to be used for the second |
| 3562 | // request, after redirection. |
| 3563 | // If |include_data| is true, data is uploaded with the request. The |
| 3564 | // response body is expected to match it exactly, if and only if |
| 3565 | // |request_method| == |redirect_method|. |
| 3566 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3567 | const std::string& request_method, |
| 3568 | const std::string& redirect_method, |
| 3569 | bool include_data) { |
| 3570 | static const char kData[] = "hello world"; |
| 3571 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3572 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3573 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3574 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3575 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3576 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3577 | HttpRequestHeaders headers; |
| 3578 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 3579 | base::NumberToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3580 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3581 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3582 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3583 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3584 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3585 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3586 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3587 | if (include_data) { |
| 3588 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3589 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3590 | HttpRequestHeaders::kContentLength)); |
| 3591 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3592 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3593 | EXPECT_EQ(kData, d.data_received()); |
| 3594 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3595 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3596 | HttpRequestHeaders::kContentLength)); |
| 3597 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3598 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3599 | EXPECT_NE(kData, d.data_received()); |
| 3600 | } |
| 3601 | } |
| 3602 | if (HasFailure()) |
| 3603 | LOG(WARNING) << "Request method was: " << request_method; |
| 3604 | } |
| 3605 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3606 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 3607 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3608 | // |request_method| is the method to use for the initial request. |
| 3609 | // |redirect_method| is the method that is expected to be used for the second |
| 3610 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3611 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3612 | // redirection. If empty, expects that there will be no Origin header. |
| 3613 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3614 | const std::string& request_method, |
| 3615 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3616 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3617 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3618 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3619 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3620 | req->set_method(request_method); |
| 3621 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3622 | redirect_url.GetOrigin().spec(), false); |
| 3623 | req->Start(); |
| 3624 | |
| 3625 | base::RunLoop().Run(); |
| 3626 | |
| 3627 | EXPECT_EQ(redirect_method, req->method()); |
| 3628 | // Note that there is no check for request success here because, for |
| 3629 | // purposes of testing, the request very well may fail. For example, if the |
| 3630 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3631 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3632 | // request would fail. However, that's fine, as long as the request headers |
| 3633 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3634 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3635 | EXPECT_FALSE( |
| 3636 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3637 | } else { |
| 3638 | std::string origin_header; |
| 3639 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3640 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3641 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3645 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3646 | const int kMsgSize = 20000; // multiple of 10 |
| 3647 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3648 | char* uploadBytes = new char[kMsgSize+1]; |
| 3649 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3650 | char marker = 'a'; |
| 3651 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3652 | memcpy(ptr, "----------", 10); |
| 3653 | ptr += 10; |
| 3654 | if (idx % 100 == 0) { |
| 3655 | ptr--; |
| 3656 | *ptr++ = marker; |
| 3657 | if (++marker > 'z') |
| 3658 | marker = 'a'; |
| 3659 | } |
| 3660 | } |
| 3661 | uploadBytes[kMsgSize] = '\0'; |
| 3662 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3663 | for (int i = 0; i < kIterations; ++i) { |
| 3664 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3665 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3666 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 3667 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3668 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3669 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3670 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3671 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3672 | r->Start(); |
| 3673 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3674 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3675 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3676 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3677 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 3678 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3679 | |
| 3680 | EXPECT_FALSE(d.received_data_before_response()); |
| 3681 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3682 | } |
| 3683 | delete[] uploadBytes; |
| 3684 | } |
| 3685 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3686 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3687 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3688 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3689 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3690 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3691 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3692 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3693 | r->Start(); |
| 3694 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3695 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3696 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3697 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3698 | if (d.request_status() != OK) { |
| 3699 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 3700 | return false; |
| 3701 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3702 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3703 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3704 | } |
| 3705 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3706 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3707 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3708 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3709 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3710 | }; |
| 3711 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3712 | namespace { |
| 3713 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3714 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3715 | const test_server::HttpRequest& request) { |
| 3716 | if (request.headers.find("Host") == request.headers.end() || |
| 3717 | request.headers.at("Host") != "www.redirect.com" || |
| 3718 | request.method != test_server::METHOD_CONNECT) { |
| 3719 | return nullptr; |
| 3720 | } |
| 3721 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3722 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3723 | new test_server::BasicHttpResponse); |
| 3724 | http_response->set_code(HTTP_FOUND); |
| 3725 | http_response->AddCustomHeader("Location", |
| 3726 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3727 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | } // namespace |
| 3731 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3732 | class TestSSLConfigService : public SSLConfigService { |
| 3733 | public: |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 3734 | TestSSLConfigService(bool online_rev_checking, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3735 | bool rev_checking_required_local_anchors, |
| 3736 | bool token_binding_enabled) |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 3737 | : online_rev_checking_(online_rev_checking), |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3738 | rev_checking_required_local_anchors_( |
| 3739 | rev_checking_required_local_anchors), |
| 3740 | token_binding_enabled_(token_binding_enabled), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3741 | min_version_(kDefaultSSLVersionMin), |
| 3742 | max_version_(kDefaultSSLVersionMax) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3743 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3744 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3745 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3746 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3747 | // SSLConfigService: |
| 3748 | void GetSSLConfig(SSLConfig* config) override { |
| 3749 | *config = SSLConfig(); |
| 3750 | config->rev_checking_enabled = online_rev_checking_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3751 | config->rev_checking_required_local_anchors = |
| 3752 | rev_checking_required_local_anchors_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3753 | config->version_min = min_version_; |
| 3754 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3755 | if (token_binding_enabled_) { |
| 3756 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | protected: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3761 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3762 | |
| 3763 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3764 | const bool online_rev_checking_; |
| 3765 | const bool rev_checking_required_local_anchors_; |
| 3766 | const bool token_binding_enabled_; |
| 3767 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 3768 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3769 | }; |
| 3770 | |
| 3771 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3772 | #if !defined(OS_IOS) |
| 3773 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3774 | public: |
fdoray | 9f0b478 | 2017-02-10 12:55:47 | [diff] [blame] | 3775 | TokenBindingURLRequestTest() = default; |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3776 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3777 | void SetUp() override { |
| 3778 | default_context_.set_ssl_config_service( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 3779 | new TestSSLConfigService(false, false, true)); |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 3780 | channel_id_service_.reset( |
| 3781 | new ChannelIDService(new DefaultChannelIDStore(NULL))); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3782 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3783 | URLRequestTestHTTP::SetUp(); |
| 3784 | } |
| 3785 | |
| 3786 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3787 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3788 | }; |
| 3789 | |
| 3790 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3791 | SpawnedTestServer::SSLOptions ssl_options; |
| 3792 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3793 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3794 | ssl_options, |
| 3795 | base::FilePath(kTestFilePath)); |
| 3796 | ASSERT_TRUE(https_test_server.Start()); |
| 3797 | |
| 3798 | TestDelegate d; |
| 3799 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3800 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3801 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d, |
| 3802 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3803 | r->Start(); |
| 3804 | EXPECT_TRUE(r->is_pending()); |
| 3805 | |
| 3806 | base::RunLoop().Run(); |
| 3807 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3808 | EXPECT_EQ(OK, d.request_status()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3809 | |
| 3810 | HttpRequestHeaders headers; |
| 3811 | std::string token_binding_header, token_binding_message; |
| 3812 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3813 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3814 | &token_binding_header)); |
| 3815 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3816 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3817 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3818 | std::vector<TokenBinding> token_bindings; |
| 3819 | ASSERT_TRUE( |
| 3820 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3821 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3822 | |
| 3823 | EXPECT_GT(d.bytes_received(), 0); |
| 3824 | std::string ekm = d.data_received(); |
| 3825 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3826 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3827 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3828 | token_bindings[0].signature, |
| 3829 | TokenBindingType::PROVIDED, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3830 | } |
| 3831 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3832 | |
| 3833 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3834 | SpawnedTestServer::SSLOptions ssl_options; |
| 3835 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3836 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3837 | ssl_options, |
| 3838 | base::FilePath(kTestFilePath)); |
| 3839 | ASSERT_TRUE(https_test_server.Start()); |
| 3840 | |
| 3841 | TestDelegate d; |
| 3842 | { |
| 3843 | GURL redirect_url = |
| 3844 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3845 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3846 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3847 | r->Start(); |
| 3848 | EXPECT_TRUE(r->is_pending()); |
| 3849 | |
| 3850 | base::RunLoop().Run(); |
| 3851 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3852 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3853 | |
| 3854 | HttpRequestHeaders headers; |
| 3855 | std::string token_binding_header, token_binding_message; |
| 3856 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3857 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3858 | &token_binding_header)); |
| 3859 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3860 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3861 | &token_binding_message)); |
| 3862 | std::vector<TokenBinding> token_bindings; |
| 3863 | ASSERT_TRUE( |
| 3864 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3865 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3866 | |
| 3867 | EXPECT_GT(d.bytes_received(), 0); |
| 3868 | std::string ekm = d.data_received(); |
| 3869 | |
| 3870 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3871 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3872 | token_bindings[0].signature, |
| 3873 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3874 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3875 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[1].ec_point, |
| 3876 | token_bindings[1].signature, |
| 3877 | TokenBindingType::REFERRED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3882 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3883 | #if !defined(OS_ANDROID) |
| 3884 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 3885 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, base::FilePath()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3886 | ASSERT_TRUE(http_server.Start()); |
| 3887 | SpawnedTestServer::SSLOptions ssl_options; |
| 3888 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3889 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3890 | ssl_options, |
| 3891 | base::FilePath(kTestFilePath)); |
| 3892 | ASSERT_TRUE(https_test_server.Start()); |
| 3893 | |
| 3894 | TestDelegate d; |
| 3895 | { |
| 3896 | GURL redirect_url = http_server.GetURL( |
| 3897 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3898 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3899 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3900 | r->Start(); |
| 3901 | EXPECT_TRUE(r->is_pending()); |
| 3902 | |
| 3903 | base::RunLoop().Run(); |
| 3904 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3905 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3906 | |
| 3907 | HttpRequestHeaders headers; |
| 3908 | std::string token_binding_header, token_binding_message; |
| 3909 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3910 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3911 | &token_binding_header)); |
| 3912 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3913 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3914 | &token_binding_message)); |
| 3915 | std::vector<TokenBinding> token_bindings; |
| 3916 | ASSERT_TRUE( |
| 3917 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3918 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3919 | |
| 3920 | EXPECT_GT(d.bytes_received(), 0); |
| 3921 | std::string ekm = d.data_received(); |
| 3922 | |
| 3923 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 3924 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 3925 | token_bindings[0].signature, |
| 3926 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | |
| 3930 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 3931 | // 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] | 3932 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3933 | // the redirect. |
| 3934 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3935 | SpawnedTestServer::SSLOptions ssl_options; |
| 3936 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3937 | ssl_options, |
| 3938 | base::FilePath(kTestFilePath)); |
| 3939 | ASSERT_TRUE(https_test_server.Start()); |
| 3940 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3941 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3942 | ssl_options, |
| 3943 | base::FilePath(kTestFilePath)); |
| 3944 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3945 | |
| 3946 | TestDelegate d; |
| 3947 | { |
| 3948 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3949 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3950 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3951 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3952 | r->Start(); |
| 3953 | EXPECT_TRUE(r->is_pending()); |
| 3954 | |
| 3955 | base::RunLoop().Run(); |
| 3956 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3957 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3958 | |
| 3959 | HttpRequestHeaders headers; |
| 3960 | std::string token_binding_header, token_binding_message; |
| 3961 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3962 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3963 | &token_binding_header)); |
| 3964 | } |
| 3965 | } |
| 3966 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3967 | #endif // !defined(OS_IOS) |
| 3968 | |
| 3969 | // 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] | 3970 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3971 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3972 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3973 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3974 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 3975 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3976 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3977 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3978 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3979 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3980 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3981 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3982 | TestDelegate d; |
| 3983 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3984 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3985 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 3986 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3987 | r->Start(); |
| 3988 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3989 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3990 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3991 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3992 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 3993 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3994 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3995 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3996 | // We should not have followed the redirect. |
| 3997 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3998 | } |
| 3999 | } |
| 4000 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4001 | // This is the same as the previous test, but checks that the network delegate |
| 4002 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4003 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4004 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4005 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4006 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4007 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4008 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4009 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4010 | TestDelegate d; |
| 4011 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4012 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4013 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4014 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4015 | r->Start(); |
| 4016 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4017 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4018 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4019 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4020 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4021 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4022 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4023 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4024 | // We should not have followed the redirect. |
| 4025 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4026 | |
| 4027 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4028 | EXPECT_THAT(network_delegate.last_error(), |
| 4029 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4033 | // Tests that we can block and asynchronously return OK in various stages. |
| 4034 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4035 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4036 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4037 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4038 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4039 | }; |
| 4040 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 4041 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4042 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4043 | |
| 4044 | TestDelegate d; |
| 4045 | BlockingNetworkDelegate network_delegate( |
| 4046 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4047 | network_delegate.set_block_on( |
| 4048 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4049 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4050 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4051 | |
| 4052 | TestURLRequestContext context(true); |
| 4053 | context.set_network_delegate(&network_delegate); |
| 4054 | context.Init(); |
| 4055 | |
| 4056 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4057 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4058 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4060 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4061 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4062 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4063 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4064 | EXPECT_EQ(blocking_stages[i], |
| 4065 | network_delegate.stage_blocked_for_callback()); |
| 4066 | network_delegate.DoCallback(OK); |
| 4067 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4068 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4069 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4070 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4071 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4072 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4073 | } |
| 4074 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4075 | } |
| 4076 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4077 | // Tests that the network delegate can block and cancel a request. |
| 4078 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4079 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4080 | |
| 4081 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4082 | BlockingNetworkDelegate network_delegate( |
| 4083 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4084 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4085 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4086 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4087 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4088 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4089 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4090 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4091 | std::unique_ptr<URLRequest> r( |
| 4092 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4093 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4094 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4095 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4096 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4097 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4098 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4099 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4100 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4101 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4102 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4103 | } |
| 4104 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4105 | } |
| 4106 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4107 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4108 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4109 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4110 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4111 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4112 | BlockingNetworkDelegate::Stage stage, |
| 4113 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4114 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4115 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4116 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4117 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4118 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4119 | TestURLRequestContext context(true); |
| 4120 | context.set_network_delegate(&network_delegate); |
| 4121 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4122 | |
| 4123 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4124 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4125 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4126 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4127 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4128 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4129 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4130 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4131 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4132 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4133 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4134 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4135 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4136 | } else { |
| 4137 | NOTREACHED(); |
| 4138 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4139 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4140 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4141 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4142 | } |
| 4143 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4144 | } |
| 4145 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4146 | // The following 3 tests check that the network delegate can cancel a request |
| 4147 | // synchronously in various stages of the request. |
| 4148 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4149 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4150 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4151 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4152 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
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::SYNCHRONOUS, |
| 4158 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
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, NetworkDelegateCancelRequestSynchronously3) { |
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::SYNCHRONOUS, |
| 4165 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
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 | // The following 3 tests check that the network delegate can cancel a request |
| 4170 | // asynchronously in various stages of the request. |
| 4171 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4172 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4173 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4174 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4175 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4179 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4180 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4181 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4182 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4186 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4187 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4188 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4189 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4190 | } |
| 4191 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4192 | // Tests that the network delegate can block and redirect a request to a new |
| 4193 | // URL. |
| 4194 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4195 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4196 | |
| 4197 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4198 | BlockingNetworkDelegate network_delegate( |
| 4199 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4200 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4201 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4202 | network_delegate.set_redirect_url(redirect_url); |
| 4203 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4204 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4205 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4206 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4207 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4208 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4209 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4210 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4211 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4212 | // Quit after hitting the redirect, so can check the headers. |
| 4213 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4214 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4215 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4216 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4217 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4218 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4219 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4220 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4221 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4222 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4223 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4224 | |
| 4225 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4226 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4227 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4228 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4229 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4230 | http_test_server()->host_port_pair()), |
| 4231 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4232 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4233 | // through an untunneled proxy. |
| 4234 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4235 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4236 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4237 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4238 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4239 | EXPECT_EQ(redirect_url, r->url()); |
| 4240 | EXPECT_EQ(original_url, r->original_url()); |
| 4241 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4242 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4243 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4244 | } |
| 4245 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4246 | } |
| 4247 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4248 | // Tests that the network delegate can block and redirect a request to a new |
| 4249 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4250 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4251 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4252 | |
| 4253 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4254 | BlockingNetworkDelegate network_delegate( |
| 4255 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4256 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4257 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4258 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4259 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4260 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4261 | |
| 4262 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4263 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4264 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4265 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4266 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4267 | // Quit after hitting the redirect, so can check the headers. |
| 4268 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4269 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4270 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4271 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4272 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4273 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4274 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4275 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4276 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4277 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4278 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4279 | |
| 4280 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4281 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4282 | base::RunLoop().Run(); |
| 4283 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4284 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4285 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4286 | http_test_server()->host_port_pair()), |
| 4287 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4288 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4289 | // through an untunneled proxy. |
| 4290 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4291 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4292 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4293 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4294 | EXPECT_EQ(redirect_url, r->url()); |
| 4295 | EXPECT_EQ(original_url, r->original_url()); |
| 4296 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4297 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4298 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4299 | } |
| 4300 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4301 | } |
| 4302 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4303 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4304 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4305 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4306 | |
| 4307 | const char kData[] = "hello world"; |
| 4308 | |
| 4309 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4310 | BlockingNetworkDelegate network_delegate( |
| 4311 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4312 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4313 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4314 | network_delegate.set_redirect_url(redirect_url); |
| 4315 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4316 | TestURLRequestContext context(true); |
| 4317 | context.set_network_delegate(&network_delegate); |
| 4318 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4319 | |
| 4320 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4321 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4322 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4323 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4324 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4325 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4326 | HttpRequestHeaders headers; |
| 4327 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 4328 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4329 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4330 | |
| 4331 | // Quit after hitting the redirect, so can check the headers. |
| 4332 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4333 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4334 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4335 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4336 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4337 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4338 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4339 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4340 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4341 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4342 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4343 | |
| 4344 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4345 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4346 | base::RunLoop().Run(); |
| 4347 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4348 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4349 | EXPECT_EQ(redirect_url, r->url()); |
| 4350 | EXPECT_EQ(original_url, r->original_url()); |
| 4351 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4352 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4353 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4354 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4355 | EXPECT_EQ(kData, d.data_received()); |
| 4356 | } |
| 4357 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4358 | } |
| 4359 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4360 | // Tests that the network delegate can block and redirect a request to a new |
| 4361 | // URL during OnHeadersReceived. |
| 4362 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4363 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4364 | |
| 4365 | TestDelegate d; |
| 4366 | BlockingNetworkDelegate network_delegate( |
| 4367 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4368 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4369 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4370 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4371 | |
| 4372 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4373 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4374 | |
| 4375 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4376 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4377 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4378 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4379 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4380 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4381 | base::RunLoop().Run(); |
| 4382 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4383 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4384 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4385 | http_test_server()->host_port_pair()), |
| 4386 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4387 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4388 | // through an untunneled proxy. |
| 4389 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4390 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4391 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4392 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4393 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4394 | EXPECT_EQ(redirect_url, r->url()); |
| 4395 | EXPECT_EQ(original_url, r->original_url()); |
| 4396 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4397 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4398 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4399 | } |
| 4400 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4401 | } |
| 4402 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4403 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4404 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4405 | // handle the challenge, and is passing the buck along to the |
| 4406 | // URLRequest::Delegate. |
| 4407 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4408 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4409 | |
| 4410 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4411 | BlockingNetworkDelegate network_delegate( |
| 4412 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4413 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4414 | TestURLRequestContext context(true); |
| 4415 | context.set_network_delegate(&network_delegate); |
| 4416 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4417 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4418 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4419 | |
| 4420 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4421 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4422 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4423 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4424 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4425 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4426 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4427 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4428 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4429 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4430 | EXPECT_TRUE(d.auth_required_called()); |
| 4431 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4432 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4433 | } |
| 4434 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4435 | } |
| 4436 | |
| 4437 | TEST_F(URLRequestTestHTTP, |
| 4438 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4439 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4440 | |
| 4441 | TestDelegate d; |
| 4442 | BlockingNetworkDelegate network_delegate( |
| 4443 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4444 | |
| 4445 | TestURLRequestContext context(true); |
| 4446 | context.set_network_delegate(&network_delegate); |
| 4447 | context.Init(); |
| 4448 | |
| 4449 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4450 | |
| 4451 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4452 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4453 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4454 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4455 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4456 | |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4457 | base::RunLoop().Run(); |
| 4458 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4459 | { |
| 4460 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4461 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4462 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4463 | } |
| 4464 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4465 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4466 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4467 | EXPECT_TRUE(d.auth_required_called()); |
| 4468 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4469 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4470 | } |
| 4471 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4472 | } |
| 4473 | |
| 4474 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4475 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4476 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4477 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4478 | |
| 4479 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4480 | BlockingNetworkDelegate network_delegate( |
| 4481 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4482 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4483 | network_delegate.set_auth_retval( |
| 4484 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4485 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4486 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4487 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4488 | TestURLRequestContext context(true); |
| 4489 | context.set_network_delegate(&network_delegate); |
| 4490 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4491 | |
| 4492 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4493 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4494 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4495 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4496 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4497 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4498 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4499 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4500 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4501 | EXPECT_FALSE(d.auth_required_called()); |
| 4502 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4503 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4504 | } |
| 4505 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4506 | } |
| 4507 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4508 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4509 | // headers (for the first or second request) when called at the proper times. |
| 4510 | TEST_F(URLRequestTestHTTP, |
| 4511 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4512 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4513 | |
| 4514 | TestDelegate d; |
| 4515 | BlockingNetworkDelegate network_delegate( |
| 4516 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4517 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4518 | network_delegate.set_auth_retval( |
| 4519 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4520 | |
| 4521 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4522 | |
| 4523 | TestURLRequestContext context(true); |
| 4524 | context.set_network_delegate(&network_delegate); |
| 4525 | context.Init(); |
| 4526 | |
| 4527 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4528 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4529 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4530 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4531 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4532 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4533 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4534 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4535 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4536 | EXPECT_FALSE(d.auth_required_called()); |
| 4537 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4538 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4539 | |
| 4540 | { |
| 4541 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4542 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4543 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4544 | } |
| 4545 | } |
| 4546 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4547 | } |
| 4548 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4549 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4550 | // by cancelling authentication. |
| 4551 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4552 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4553 | |
| 4554 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4555 | BlockingNetworkDelegate network_delegate( |
| 4556 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4557 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4558 | network_delegate.set_auth_retval( |
| 4559 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4560 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4561 | TestURLRequestContext context(true); |
| 4562 | context.set_network_delegate(&network_delegate); |
| 4563 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4564 | |
| 4565 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4566 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4567 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4568 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4569 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4570 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4571 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4572 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4573 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4574 | EXPECT_FALSE(d.auth_required_called()); |
| 4575 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4576 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4577 | } |
| 4578 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4579 | } |
| 4580 | |
| 4581 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4582 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4583 | // to handle the challenge, and is passing the buck along to the |
| 4584 | // URLRequest::Delegate. |
| 4585 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4586 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4587 | |
| 4588 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4589 | BlockingNetworkDelegate network_delegate( |
| 4590 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4591 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4592 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4593 | TestURLRequestContext context(true); |
| 4594 | context.set_network_delegate(&network_delegate); |
| 4595 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4596 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4597 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4598 | |
| 4599 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4600 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4601 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4602 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4603 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4604 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4605 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4606 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4607 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4608 | EXPECT_TRUE(d.auth_required_called()); |
| 4609 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4610 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4611 | } |
| 4612 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4613 | } |
| 4614 | |
| 4615 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4616 | // by setting credentials. |
| 4617 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4618 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4619 | |
| 4620 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4621 | BlockingNetworkDelegate network_delegate( |
| 4622 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4623 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4624 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4625 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4626 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4627 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4628 | network_delegate.set_auth_credentials(auth_credentials); |
| 4629 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4630 | TestURLRequestContext context(true); |
| 4631 | context.set_network_delegate(&network_delegate); |
| 4632 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4633 | |
| 4634 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4635 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4636 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4637 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4638 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4639 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4640 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4641 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4642 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4643 | EXPECT_FALSE(d.auth_required_called()); |
| 4644 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4645 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4646 | } |
| 4647 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4648 | } |
| 4649 | |
| 4650 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4651 | // by cancelling authentication. |
| 4652 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4653 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4654 | |
| 4655 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4656 | BlockingNetworkDelegate network_delegate( |
| 4657 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4658 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4659 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4660 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4661 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4662 | TestURLRequestContext context(true); |
| 4663 | context.set_network_delegate(&network_delegate); |
| 4664 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4665 | |
| 4666 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4667 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4668 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4669 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4670 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4671 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4672 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4673 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4674 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4675 | EXPECT_FALSE(d.auth_required_called()); |
| 4676 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4677 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4678 | } |
| 4679 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4680 | } |
| 4681 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4682 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 4683 | // affect the 200 response. This is a regression test for |
| 4684 | // https://crbug.com/801237. |
| 4685 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 4686 | ASSERT_TRUE(http_test_server()->Start()); |
| 4687 | |
| 4688 | TestDelegate d; |
| 4689 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4690 | default_network_delegate_.set_add_header_to_first_response(true); |
| 4691 | |
| 4692 | { |
| 4693 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 4694 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4695 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4696 | r->Start(); |
| 4697 | |
| 4698 | base::RunLoop().Run(); |
| 4699 | |
| 4700 | EXPECT_EQ(OK, d.request_status()); |
| 4701 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4702 | EXPECT_TRUE(d.auth_required_called()); |
| 4703 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 4704 | } |
| 4705 | |
| 4706 | { |
| 4707 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
| 4708 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4709 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4710 | r->Start(); |
| 4711 | |
| 4712 | base::RunLoop().Run(); |
| 4713 | |
| 4714 | // Check that set_add_header_to_first_response normally adds a header. |
| 4715 | EXPECT_EQ(OK, d.request_status()); |
| 4716 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4717 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 4718 | } |
| 4719 | } |
| 4720 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4721 | // Tests that we can handle when a network request was canceled while we were |
| 4722 | // waiting for the network delegate. |
| 4723 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4724 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4725 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4726 | |
| 4727 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4728 | BlockingNetworkDelegate network_delegate( |
| 4729 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4730 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4731 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4732 | TestURLRequestContext context(true); |
| 4733 | context.set_network_delegate(&network_delegate); |
| 4734 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4735 | |
| 4736 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4737 | std::unique_ptr<URLRequest> r( |
| 4738 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4739 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4740 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4741 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4742 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4743 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4744 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4745 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4746 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4747 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4748 | // Ensure that network delegate is notified. |
| 4749 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4750 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4751 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4752 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4753 | } |
| 4754 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4755 | } |
| 4756 | |
| 4757 | // Tests that we can handle when a network request was canceled while we were |
| 4758 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4759 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4760 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4761 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4762 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4763 | |
| 4764 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4765 | BlockingNetworkDelegate network_delegate( |
| 4766 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4767 | network_delegate.set_block_on( |
| 4768 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4769 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4770 | TestURLRequestContext context(true); |
| 4771 | context.set_network_delegate(&network_delegate); |
| 4772 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4773 | |
| 4774 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4775 | std::unique_ptr<URLRequest> r( |
| 4776 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4777 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4778 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4779 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4780 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4781 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4782 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4783 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4784 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4785 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4786 | // Ensure that network delegate is notified. |
| 4787 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4788 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4789 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4790 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4791 | } |
| 4792 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4793 | } |
| 4794 | |
| 4795 | // Tests that we can handle when a network request was canceled while we were |
| 4796 | // waiting for the network delegate. |
| 4797 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4798 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4799 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4800 | |
| 4801 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4802 | BlockingNetworkDelegate network_delegate( |
| 4803 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4804 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4805 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4806 | TestURLRequestContext context(true); |
| 4807 | context.set_network_delegate(&network_delegate); |
| 4808 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4809 | |
| 4810 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4811 | std::unique_ptr<URLRequest> r( |
| 4812 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4813 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4814 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4815 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4816 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4817 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4818 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4819 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4820 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4821 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4822 | // Ensure that network delegate is notified. |
| 4823 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4824 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4825 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4826 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4827 | } |
| 4828 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4829 | } |
| 4830 | |
| 4831 | // Tests that we can handle when a network request was canceled while we were |
| 4832 | // waiting for the network delegate. |
| 4833 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4834 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4836 | |
| 4837 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4838 | BlockingNetworkDelegate network_delegate( |
| 4839 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4840 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4841 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4842 | TestURLRequestContext context(true); |
| 4843 | context.set_network_delegate(&network_delegate); |
| 4844 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4845 | |
| 4846 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4847 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4848 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 4849 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4850 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4851 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4852 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4853 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4854 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4855 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4856 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4857 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4858 | // Ensure that network delegate is notified. |
| 4859 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4860 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4861 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4862 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4863 | } |
| 4864 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4865 | } |
| 4866 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4867 | namespace { |
| 4868 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4869 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4870 | const test_server::HttpRequest& request) { |
| 4871 | if (request.headers.find("Host") == request.headers.end() || |
| 4872 | request.headers.at("Host") != "www.server-auth.com" || |
| 4873 | request.method != test_server::METHOD_CONNECT) { |
| 4874 | return nullptr; |
| 4875 | } |
| 4876 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4877 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4878 | new test_server::BasicHttpResponse); |
| 4879 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4880 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4881 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4882 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | } // namespace |
| 4886 | |
| 4887 | // 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] | 4888 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4889 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4890 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4891 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4892 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4893 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4894 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4895 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4896 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4897 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4898 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4899 | TestDelegate d; |
| 4900 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4901 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4902 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 4903 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4904 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4905 | r->Start(); |
| 4906 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4907 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4908 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4909 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4910 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4911 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4912 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4913 | } |
| 4914 | } |
| 4915 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4916 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4917 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4918 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4919 | TestDelegate d; |
| 4920 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4921 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4922 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4923 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4924 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4925 | r->Start(); |
| 4926 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4927 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4928 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4929 | |
| 4930 | EXPECT_EQ(1, d.response_started_count()); |
| 4931 | EXPECT_FALSE(d.received_data_before_response()); |
| 4932 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4933 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4934 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4935 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4936 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4937 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4938 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4939 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4940 | } |
| 4941 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4942 | // This test has the server send a large number of cookies to the client. |
| 4943 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4944 | // search is used to estimate that maximum number of cookies that are accepted |
| 4945 | // by the browser. Beyond the maximum number, the request will fail with |
| 4946 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 4947 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4948 | // http://crbug.com/177916 |
| 4949 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4950 | #else |
| 4951 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4952 | #endif // defined(OS_WIN) |
| 4953 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4954 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4955 | |
| 4956 | int lower_bound = 0; |
| 4957 | int upper_bound = 1; |
| 4958 | |
| 4959 | // Double the number of cookies until the response header limits are |
| 4960 | // exceeded. |
| 4961 | while (DoManyCookiesRequest(upper_bound)) { |
| 4962 | lower_bound = upper_bound; |
| 4963 | upper_bound *= 2; |
| 4964 | ASSERT_LT(upper_bound, 1000000); |
| 4965 | } |
| 4966 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4967 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4968 | if (tolerance < 2) |
| 4969 | tolerance = 2; |
| 4970 | |
| 4971 | // Perform a binary search to find the highest possible number of cookies, |
| 4972 | // within the desired tolerance. |
| 4973 | while (upper_bound - lower_bound >= tolerance) { |
| 4974 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4975 | |
| 4976 | if (DoManyCookiesRequest(num_cookies)) |
| 4977 | lower_bound = num_cookies; |
| 4978 | else |
| 4979 | upper_bound = num_cookies; |
| 4980 | } |
| 4981 | // Success: the test did not crash. |
| 4982 | } |
| 4983 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4984 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4985 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4986 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4987 | TestDelegate d; |
| 4988 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4989 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4990 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4991 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4992 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4993 | r->Start(); |
| 4994 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4995 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4996 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4997 | |
| 4998 | EXPECT_EQ(1, d.response_started_count()); |
| 4999 | EXPECT_FALSE(d.received_data_before_response()); |
| 5000 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5001 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5002 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5003 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5004 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5005 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5006 | } |
| 5007 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5008 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5009 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5010 | |
| 5011 | TestDelegate d; |
| 5012 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5013 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5014 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5015 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5016 | |
| 5017 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5018 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5019 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5020 | r->Start(); |
| 5021 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5022 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5023 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5024 | |
| 5025 | EXPECT_EQ(1, d.response_started_count()); |
| 5026 | EXPECT_FALSE(d.received_data_before_response()); |
| 5027 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5028 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5029 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5030 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5031 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5032 | |
| 5033 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5034 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5035 | } |
| 5036 | } |
| 5037 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5038 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5039 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5040 | |
| 5041 | TestDelegate d; |
| 5042 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5043 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5044 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5045 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5046 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5047 | r->Start(); |
| 5048 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5049 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5050 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5051 | |
| 5052 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5053 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5054 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5055 | |
| 5056 | EXPECT_EQ(1, d.response_started_count()); |
| 5057 | EXPECT_FALSE(d.received_data_before_response()); |
| 5058 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5059 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5060 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5061 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5062 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5063 | } |
| 5064 | } |
| 5065 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5066 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5067 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5068 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5069 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5070 | base::FilePath(kTestFilePath)); |
| 5071 | |
| 5072 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5073 | |
| 5074 | // Parameter that specifies the Content-Length field in the response: |
| 5075 | // C - Compressed length. |
| 5076 | // U - Uncompressed length. |
| 5077 | // L - Large length (larger than both C & U). |
| 5078 | // M - Medium length (between C & U). |
| 5079 | // S - Small length (smaller than both C & U). |
| 5080 | const char test_parameters[] = "CULMS"; |
| 5081 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 5082 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5083 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5084 | // S has too little data, but we seem to accept it. |
| 5085 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5086 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5087 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5088 | base::FilePath file_path; |
| 5089 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 5090 | file_path = file_path.Append(kTestFilePath); |
| 5091 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5092 | std::string expected_content; |
| 5093 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5094 | |
| 5095 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5096 | TestDelegate d; |
| 5097 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5098 | std::string test_file = base::StringPrintf( |
| 5099 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5100 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5101 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5102 | TestURLRequestContext context(true); |
| 5103 | context.set_network_delegate(&network_delegate); |
| 5104 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5105 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5106 | std::unique_ptr<URLRequest> r( |
| 5107 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5108 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5109 | r->Start(); |
| 5110 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5111 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5112 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5113 | |
| 5114 | EXPECT_EQ(1, d.response_started_count()); |
| 5115 | EXPECT_FALSE(d.received_data_before_response()); |
| 5116 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5117 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5118 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5119 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5120 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5121 | if (test_parameters[i] == 'S') { |
| 5122 | // When content length is smaller than both compressed length and |
| 5123 | // uncompressed length, HttpStreamParser might not read the full |
| 5124 | // response body. |
| 5125 | continue; |
| 5126 | } |
| 5127 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5128 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5129 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5130 | << " Parameter = \"" << test_file << "\""; |
| 5131 | } |
| 5132 | } |
| 5133 | } |
| 5134 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5135 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5136 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5137 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5138 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5139 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5140 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5141 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5142 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5143 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5144 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5145 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5146 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5147 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5148 | |
| 5149 | EXPECT_EQ(1, d.response_started_count()); |
| 5150 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5151 | EXPECT_EQ(destination_url, req->url()); |
| 5152 | EXPECT_EQ(original_url, req->original_url()); |
| 5153 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5154 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5155 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5156 | |
| 5157 | LoadTimingInfo load_timing_info_before_redirect; |
| 5158 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5159 | &load_timing_info_before_redirect)); |
| 5160 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5161 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5162 | |
| 5163 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5164 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5165 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5166 | |
| 5167 | // Check that a new socket was used on redirect, since the server does not |
| 5168 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5169 | // before the ones recorded for the second request. |
| 5170 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5171 | load_timing_info.socket_log_id); |
| 5172 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5173 | load_timing_info.connect_timing.connect_start); |
| 5174 | } |
| 5175 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5176 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5177 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5178 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5179 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5180 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5181 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5182 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5183 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5184 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5185 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5186 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5187 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5188 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5189 | |
| 5190 | EXPECT_EQ(1, d.response_started_count()); |
| 5191 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5192 | EXPECT_EQ(destination_url, req->url()); |
| 5193 | EXPECT_EQ(original_url, req->original_url()); |
| 5194 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5195 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5196 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5197 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5198 | } |
| 5199 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5200 | // First and second pieces of information logged by delegates to URLRequests. |
| 5201 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5202 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5203 | |
| 5204 | // Logs delegate information to a URLRequest. The first string is logged |
| 5205 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5206 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5207 | // another asynchronous call is used to clear the delegate information |
| 5208 | // before calling a callback. The object then deletes itself. |
| 5209 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5210 | public: |
| 5211 | typedef base::Callback<void()> Callback; |
| 5212 | |
| 5213 | // Each time delegate information is added to the URLRequest, the resulting |
| 5214 | // load state is checked. The expected load state after each request is |
| 5215 | // passed in as an argument. |
| 5216 | static void Run(URLRequest* url_request, |
| 5217 | LoadState expected_first_load_state, |
| 5218 | LoadState expected_second_load_state, |
| 5219 | LoadState expected_third_load_state, |
| 5220 | const Callback& callback) { |
| 5221 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 5222 | url_request, |
| 5223 | expected_first_load_state, |
| 5224 | expected_second_load_state, |
| 5225 | expected_third_load_state, |
| 5226 | callback); |
| 5227 | logger->Start(); |
| 5228 | } |
| 5229 | |
| 5230 | // Checks that the log entries, starting with log_position, contain the |
| 5231 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5232 | // recorded. Returns the index of entry after the expected number of |
| 5233 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5234 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5235 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5236 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5237 | if (log_position + 3 >= entries.size()) { |
| 5238 | ADD_FAILURE() << "Not enough log entries"; |
| 5239 | return entries.size(); |
| 5240 | } |
| 5241 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5242 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5243 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5244 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5245 | &delegate_info)); |
| 5246 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5247 | |
| 5248 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5249 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5250 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5251 | |
| 5252 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5253 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5254 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5255 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5256 | &delegate_info)); |
| 5257 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5258 | |
| 5259 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5260 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5261 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5262 | |
| 5263 | return log_position + 1; |
| 5264 | } |
| 5265 | |
| 5266 | private: |
| 5267 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5268 | |
| 5269 | AsyncDelegateLogger(URLRequest* url_request, |
| 5270 | LoadState expected_first_load_state, |
| 5271 | LoadState expected_second_load_state, |
| 5272 | LoadState expected_third_load_state, |
| 5273 | const Callback& callback) |
| 5274 | : url_request_(url_request), |
| 5275 | expected_first_load_state_(expected_first_load_state), |
| 5276 | expected_second_load_state_(expected_second_load_state), |
| 5277 | expected_third_load_state_(expected_third_load_state), |
| 5278 | callback_(callback) { |
| 5279 | } |
| 5280 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5281 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5282 | |
| 5283 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5284 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5285 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5286 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5287 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5288 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5289 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5293 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5294 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5295 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5296 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5297 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5298 | } else { |
| 5299 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5300 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5301 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 5302 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5306 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5307 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5308 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5309 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5310 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5311 | callback_.Run(); |
| 5312 | } |
| 5313 | |
| 5314 | URLRequest* url_request_; |
| 5315 | const int expected_first_load_state_; |
| 5316 | const int expected_second_load_state_; |
| 5317 | const int expected_third_load_state_; |
| 5318 | const Callback callback_; |
| 5319 | |
| 5320 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5321 | }; |
| 5322 | |
| 5323 | // NetworkDelegate that logs delegate information before a request is started, |
| 5324 | // before headers are sent, when headers are read, and when auth information |
| 5325 | // is requested. Uses AsyncDelegateLogger. |
| 5326 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5327 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5328 | AsyncLoggingNetworkDelegate() = default; |
| 5329 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5330 | |
| 5331 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5332 | int OnBeforeURLRequest(URLRequest* request, |
| 5333 | const CompletionCallback& callback, |
| 5334 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5335 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 5336 | return RunCallbackAsynchronously(request, callback); |
| 5337 | } |
| 5338 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5339 | int OnBeforeStartTransaction(URLRequest* request, |
| 5340 | const CompletionCallback& callback, |
| 5341 | HttpRequestHeaders* headers) override { |
| 5342 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5343 | return RunCallbackAsynchronously(request, callback); |
| 5344 | } |
| 5345 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5346 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5347 | URLRequest* request, |
| 5348 | const CompletionCallback& callback, |
| 5349 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5350 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5351 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5352 | TestNetworkDelegate::OnHeadersReceived(request, |
| 5353 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5354 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5355 | override_response_headers, |
| 5356 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5357 | return RunCallbackAsynchronously(request, callback); |
| 5358 | } |
| 5359 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5360 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5361 | URLRequest* request, |
| 5362 | const AuthChallengeInfo& auth_info, |
| 5363 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5364 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5365 | AsyncDelegateLogger::Run( |
| 5366 | request, |
| 5367 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5368 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5369 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5370 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5371 | callback, credentials)); |
| 5372 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5373 | } |
| 5374 | |
| 5375 | private: |
| 5376 | static int RunCallbackAsynchronously( |
| 5377 | URLRequest* request, |
| 5378 | const CompletionCallback& callback) { |
| 5379 | AsyncDelegateLogger::Run( |
| 5380 | request, |
| 5381 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5382 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5383 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5384 | base::Bind(callback, OK)); |
| 5385 | return ERR_IO_PENDING; |
| 5386 | } |
| 5387 | |
| 5388 | static void SetAuthAndResume(const AuthCallback& callback, |
| 5389 | AuthCredentials* credentials) { |
| 5390 | *credentials = AuthCredentials(kUser, kSecret); |
| 5391 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5392 | } |
| 5393 | |
| 5394 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5395 | }; |
| 5396 | |
| 5397 | // URLRequest::Delegate that logs delegate information when the headers |
| 5398 | // are received, when each read completes, and during redirects. Uses |
| 5399 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5400 | // |
| 5401 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5402 | // advancing to the next step in most cases, as well as cancellation. |
| 5403 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5404 | public: |
| 5405 | enum CancelStage { |
| 5406 | NO_CANCEL = 0, |
| 5407 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5408 | CANCEL_ON_RESPONSE_STARTED, |
| 5409 | CANCEL_ON_READ_COMPLETED |
| 5410 | }; |
| 5411 | |
| 5412 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5413 | : cancel_stage_(cancel_stage) { |
| 5414 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5415 | set_cancel_in_received_redirect(true); |
| 5416 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5417 | set_cancel_in_response_started(true); |
| 5418 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5419 | set_cancel_in_received_data(true); |
| 5420 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5421 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5422 | |
| 5423 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5424 | void OnReceivedRedirect(URLRequest* request, |
| 5425 | const RedirectInfo& redirect_info, |
| 5426 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5427 | *defer_redirect = true; |
| 5428 | AsyncDelegateLogger::Run( |
| 5429 | request, |
| 5430 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5431 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5432 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5433 | base::Bind( |
| 5434 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5435 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5436 | } |
| 5437 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5438 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5439 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5440 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5441 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5442 | base::Bind( |
| 5443 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5444 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5445 | } |
| 5446 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5447 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5448 | AsyncDelegateLogger::Run( |
| 5449 | request, |
| 5450 | LOAD_STATE_IDLE, |
| 5451 | LOAD_STATE_IDLE, |
| 5452 | LOAD_STATE_IDLE, |
| 5453 | base::Bind( |
| 5454 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5455 | base::Unretained(this), request, bytes_read)); |
| 5456 | } |
| 5457 | |
| 5458 | private: |
| 5459 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5460 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5461 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5462 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5463 | // FollowDeferredRedirect should not be called after cancellation. |
| 5464 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5465 | return; |
| 5466 | if (!defer_redirect) |
| 5467 | request->FollowDeferredRedirect(); |
| 5468 | } |
| 5469 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5470 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5471 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5472 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5476 | // The parent class continues the request. |
| 5477 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5478 | } |
| 5479 | |
| 5480 | const CancelStage cancel_stage_; |
| 5481 | |
| 5482 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5483 | }; |
| 5484 | |
| 5485 | // Tests handling of delegate info before a request starts. |
| 5486 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5487 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5488 | |
| 5489 | TestDelegate request_delegate; |
| 5490 | TestURLRequestContext context(true); |
| 5491 | context.set_network_delegate(NULL); |
| 5492 | context.set_net_log(&net_log_); |
| 5493 | context.Init(); |
| 5494 | |
| 5495 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5496 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5497 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5498 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5499 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5500 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5501 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5502 | |
| 5503 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5504 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5505 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5506 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5507 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5508 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5509 | |
| 5510 | base::RunLoop().Run(); |
| 5511 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5512 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5513 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5514 | } |
| 5515 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5516 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5517 | net_log_.GetEntries(&entries); |
| 5518 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5519 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5520 | |
| 5521 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5522 | |
| 5523 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5524 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5525 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | // Tests handling of delegate info from a network delegate. |
| 5529 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5530 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5531 | |
| 5532 | TestDelegate request_delegate; |
| 5533 | AsyncLoggingNetworkDelegate network_delegate; |
| 5534 | TestURLRequestContext context(true); |
| 5535 | context.set_network_delegate(&network_delegate); |
| 5536 | context.set_net_log(&net_log_); |
| 5537 | context.Init(); |
| 5538 | |
| 5539 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5540 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5541 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5542 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5543 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5544 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5545 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5547 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5548 | base::RunLoop().Run(); |
| 5549 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5550 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5551 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5552 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5553 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5554 | } |
| 5555 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5556 | |
| 5557 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5558 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5559 | net_log_.GetEntries(&entries); |
| 5560 | for (size_t i = 0; i < 3; ++i) { |
| 5561 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5562 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5563 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5564 | |
| 5565 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5566 | log_position + 1); |
| 5567 | |
| 5568 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5569 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5570 | entries[log_position].type); |
| 5571 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5572 | } |
| 5573 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5574 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5575 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | // Tests handling of delegate info from a network delegate in the case of an |
| 5579 | // HTTP redirect. |
| 5580 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5581 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5582 | |
| 5583 | TestDelegate request_delegate; |
| 5584 | AsyncLoggingNetworkDelegate network_delegate; |
| 5585 | TestURLRequestContext context(true); |
| 5586 | context.set_network_delegate(&network_delegate); |
| 5587 | context.set_net_log(&net_log_); |
| 5588 | context.Init(); |
| 5589 | |
| 5590 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5591 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5592 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5593 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5594 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5595 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5596 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5597 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5598 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5599 | base::RunLoop().Run(); |
| 5600 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5601 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5602 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5603 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5604 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5605 | } |
| 5606 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5607 | |
| 5608 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5609 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5610 | net_log_.GetEntries(&entries); |
| 5611 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5612 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5613 | for (size_t i = 0; i < 3; ++i) { |
| 5614 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5615 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5616 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5617 | |
| 5618 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5619 | log_position + 1); |
| 5620 | |
| 5621 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5622 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5623 | entries[log_position].type); |
| 5624 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5628 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5629 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5630 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5631 | |
| 5632 | // The NetworkDelegate logged information in the same three events as before. |
| 5633 | for (size_t i = 0; i < 3; ++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 | |
| 5651 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5652 | // AUTH. |
| 5653 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5654 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5655 | |
| 5656 | TestDelegate request_delegate; |
| 5657 | AsyncLoggingNetworkDelegate network_delegate; |
| 5658 | TestURLRequestContext context(true); |
| 5659 | context.set_network_delegate(&network_delegate); |
| 5660 | context.set_net_log(&net_log_); |
| 5661 | context.Init(); |
| 5662 | |
| 5663 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5664 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5665 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5666 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5667 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5668 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5669 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5671 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | base::RunLoop().Run(); |
| 5673 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5674 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5675 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5676 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5677 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5678 | } |
| 5679 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5680 | |
| 5681 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5682 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5683 | net_log_.GetEntries(&entries); |
| 5684 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5685 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5686 | // in |
| 5687 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5688 | for (size_t i = 0; i < 6; ++i) { |
| 5689 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5690 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5691 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5692 | |
| 5693 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5694 | log_position + 1); |
| 5695 | |
| 5696 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5697 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5698 | entries[log_position].type); |
| 5699 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5700 | } |
| 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 | } |
| 5705 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5706 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5707 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5708 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5709 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5710 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5711 | base::FilePath(kTestFilePath)); |
| 5712 | |
| 5713 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5714 | |
| 5715 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5716 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5717 | TestURLRequestContext context(true); |
| 5718 | context.set_network_delegate(NULL); |
| 5719 | context.set_net_log(&net_log_); |
| 5720 | context.Init(); |
| 5721 | |
| 5722 | { |
| 5723 | // A chunked response with delays between chunks is used to make sure that |
| 5724 | // attempts by the URLRequest delegate to log information while reading the |
| 5725 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5726 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5727 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5728 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5729 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5730 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5731 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5732 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5733 | base::RunLoop().Run(); |
| 5734 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5735 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5736 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5737 | } |
| 5738 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5739 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5740 | net_log_.GetEntries(&entries); |
| 5741 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5742 | size_t log_position = 0; |
| 5743 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5744 | // The delegate info should only have been logged on header complete. Other |
| 5745 | // times it should silently be ignored. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5746 | log_position = ExpectLogContainsSomewhereAfter( |
| 5747 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5748 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5749 | |
| 5750 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5751 | log_position + 1); |
| 5752 | |
| 5753 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5754 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5755 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5756 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5757 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5758 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5759 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5760 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5761 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5762 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5763 | |
| 5764 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5765 | // an HTTP redirect. |
| 5766 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5767 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5768 | |
| 5769 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5770 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5771 | TestURLRequestContext context(true); |
| 5772 | context.set_network_delegate(NULL); |
| 5773 | context.set_net_log(&net_log_); |
| 5774 | context.Init(); |
| 5775 | |
| 5776 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5777 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5778 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5779 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5780 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5781 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5782 | base::RunLoop().Run(); |
| 5783 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5784 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5785 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5786 | } |
| 5787 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5788 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5789 | net_log_.GetEntries(&entries); |
| 5790 | |
| 5791 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5792 | // OnResponseStarted. |
| 5793 | size_t log_position = 0; |
| 5794 | for (int i = 0; i < 2; ++i) { |
| 5795 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5796 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5797 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5798 | |
| 5799 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5800 | log_position + 1); |
| 5801 | |
| 5802 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5803 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5804 | entries[log_position].type); |
| 5805 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5806 | } |
| 5807 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5808 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5809 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5810 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5811 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5815 | // an HTTP redirect, with cancellation at various points. |
| 5816 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5817 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5818 | |
| 5819 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5820 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5821 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5822 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5823 | }; |
| 5824 | |
| 5825 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5826 | ++test_case) { |
| 5827 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5828 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 5829 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5830 | context.set_network_delegate(NULL); |
| 5831 | context.set_net_log(&net_log); |
| 5832 | context.Init(); |
| 5833 | |
| 5834 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5835 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5836 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5837 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5838 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5839 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5840 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5841 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5842 | } |
| 5843 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5844 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5845 | net_log.GetEntries(&entries); |
| 5846 | |
| 5847 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5848 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5849 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5850 | // still currently supported in that call. |
| 5851 | size_t log_position = 0; |
| 5852 | for (int i = 0; i < 2; ++i) { |
| 5853 | log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5854 | entries, log_position, NetLogEventType::URL_REQUEST_DELEGATE, |
| 5855 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5856 | |
| 5857 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5858 | log_position + 1); |
| 5859 | |
| 5860 | ASSERT_LT(log_position, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5861 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE, |
| 5862 | entries[log_position].type); |
| 5863 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5864 | } |
| 5865 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5866 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5867 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5868 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5869 | entries, log_position + 1, NetLogEventType::URL_REQUEST_DELEGATE)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5873 | namespace { |
| 5874 | |
| 5875 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5876 | const char kExtraValue[] = "fubar"; |
| 5877 | |
| 5878 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5879 | void OnReceivedRedirect(URLRequest* request, |
| 5880 | const RedirectInfo& redirect_info, |
| 5881 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5882 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5883 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5884 | } |
| 5885 | }; |
| 5886 | |
| 5887 | } // namespace |
| 5888 | |
| 5889 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5890 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5891 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5892 | GURL destination_url = |
| 5893 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5894 | GURL original_url = |
| 5895 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5896 | RedirectWithAdditionalHeadersDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5897 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5898 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5899 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5900 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5901 | |
| 5902 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5903 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5904 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5905 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5906 | EXPECT_FALSE(req->is_pending()); |
| 5907 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5908 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5909 | } |
| 5910 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5911 | namespace { |
| 5912 | |
| 5913 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5914 | |
| 5915 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5916 | void OnReceivedRedirect(URLRequest* request, |
| 5917 | const RedirectInfo& redirect_info, |
| 5918 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5919 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5920 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5921 | } |
| 5922 | }; |
| 5923 | |
| 5924 | } // namespace |
| 5925 | |
| 5926 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5927 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5928 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5929 | GURL destination_url = http_test_server()->GetURL( |
| 5930 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5931 | GURL original_url = |
| 5932 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5933 | RedirectWithHeaderRemovalDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5934 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5935 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5936 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5937 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5938 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5939 | |
| 5940 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5941 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5942 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5943 | EXPECT_FALSE(req->is_pending()); |
| 5944 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5945 | EXPECT_EQ("None", d.data_received()); |
| 5946 | } |
| 5947 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5948 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5949 | TestDelegate d; |
| 5950 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5951 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5952 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 5953 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5954 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5955 | r->Start(); |
| 5956 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5957 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5958 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5959 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5960 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5961 | |
| 5962 | // We expect to receive OnResponseStarted even though the request has been |
| 5963 | // cancelled. |
| 5964 | EXPECT_EQ(1, d.response_started_count()); |
| 5965 | EXPECT_EQ(0, d.bytes_received()); |
| 5966 | EXPECT_FALSE(d.received_data_before_response()); |
| 5967 | } |
| 5968 | } |
| 5969 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5970 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5971 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5972 | |
| 5973 | TestDelegate d; |
| 5974 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5975 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5976 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 5977 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5978 | |
| 5979 | d.set_cancel_in_response_started(true); |
| 5980 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5981 | r->Start(); |
| 5982 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5983 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5984 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5985 | |
| 5986 | EXPECT_EQ(1, d.response_started_count()); |
| 5987 | EXPECT_EQ(0, d.bytes_received()); |
| 5988 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5989 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 5993 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5994 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5995 | |
| 5996 | TestDelegate d; |
| 5997 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5998 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5999 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6001 | |
| 6002 | d.set_cancel_in_received_data(true); |
| 6003 | |
| 6004 | r->Start(); |
| 6005 | EXPECT_TRUE(r->is_pending()); |
| 6006 | |
| 6007 | base::RunLoop().Run(); |
| 6008 | |
| 6009 | EXPECT_EQ(1, d.response_started_count()); |
| 6010 | EXPECT_NE(0, d.received_bytes_count()); |
| 6011 | EXPECT_FALSE(d.received_data_before_response()); |
| 6012 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6017 | ASSERT_TRUE(http_test_server()->Start()); |
| 6018 | |
| 6019 | TestDelegate d; |
| 6020 | { |
| 6021 | // This returns an empty response (With headers). |
| 6022 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6023 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6024 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6025 | |
| 6026 | d.set_cancel_in_received_data(true); |
| 6027 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6028 | r->Start(); |
| 6029 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6030 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6031 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6032 | |
| 6033 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6034 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6035 | EXPECT_FALSE(d.received_data_before_response()); |
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 | } |
| 6038 | } |
| 6039 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6040 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6041 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6042 | |
| 6043 | TestDelegate d; |
| 6044 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6045 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6046 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6047 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6048 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6049 | r->Start(); |
| 6050 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6051 | |
| 6052 | // The request will be implicitly canceled when it is destroyed. The |
| 6053 | // test delegate must not post a quit message when this happens because |
| 6054 | // this test doesn't actually have a message loop. The quit message would |
| 6055 | // get put on this thread's message queue and the next test would exit |
| 6056 | // early, causing problems. |
| 6057 | d.set_quit_on_complete(false); |
| 6058 | } |
| 6059 | // expect things to just cleanup properly. |
| 6060 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6061 | // 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] | 6062 | // message loop |
| 6063 | EXPECT_FALSE(d.received_data_before_response()); |
| 6064 | EXPECT_EQ(0, d.bytes_received()); |
| 6065 | } |
| 6066 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6067 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6068 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6069 | |
| 6070 | // populate cache |
| 6071 | { |
| 6072 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6073 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6074 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6075 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6076 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6077 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6078 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6079 | } |
| 6080 | |
| 6081 | // cancel read from cache (see bug 990242) |
| 6082 | { |
| 6083 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6084 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6085 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6086 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6087 | r->Start(); |
| 6088 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6089 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6090 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6091 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6092 | EXPECT_EQ(1, d.response_started_count()); |
| 6093 | EXPECT_EQ(0, d.bytes_received()); |
| 6094 | EXPECT_FALSE(d.received_data_before_response()); |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6099 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6100 | HTTPUploadDataOperationTest("POST"); |
| 6101 | } |
| 6102 | |
| 6103 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6104 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6105 | HTTPUploadDataOperationTest("PUT"); |
| 6106 | } |
| 6107 | |
| 6108 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6109 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6110 | |
| 6111 | TestDelegate d; |
| 6112 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6113 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6114 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6115 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6116 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6117 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6118 | r->Start(); |
| 6119 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6120 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6121 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6122 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6123 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6124 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6125 | |
| 6126 | EXPECT_FALSE(d.received_data_before_response()); |
| 6127 | EXPECT_TRUE(d.data_received().empty()); |
| 6128 | } |
| 6129 | } |
| 6130 | |
| 6131 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6132 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6133 | |
| 6134 | TestDelegate d; |
| 6135 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6136 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6137 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6138 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6139 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6140 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6141 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6142 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6143 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6144 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6145 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6146 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6147 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6148 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6149 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6150 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6151 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6152 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6153 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6154 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6155 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6156 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6157 | r->Start(); |
| 6158 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6159 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6160 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6161 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6162 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6163 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6164 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6165 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6166 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6167 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6168 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6169 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6170 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6171 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6172 | |
| 6173 | EXPECT_FALSE(d.received_data_before_response()); |
| 6174 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6175 | EXPECT_EQ(size, d.bytes_received()); |
| 6176 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6177 | } |
| 6178 | } |
| 6179 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6180 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6181 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6182 | |
| 6183 | TestDelegate d; |
| 6184 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6185 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6186 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6187 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6188 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6189 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6190 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6191 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6192 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6193 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6194 | base::FilePath(FILE_PATH_LITERAL( |
| 6195 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6196 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6197 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6198 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6199 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6200 | r->Start(); |
| 6201 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6202 | |
| 6203 | base::RunLoop().Run(); |
| 6204 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6205 | EXPECT_TRUE(d.request_failed()); |
| 6206 | EXPECT_FALSE(d.received_data_before_response()); |
| 6207 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6208 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6212 | namespace { |
| 6213 | |
| 6214 | // Adds a standard set of data to an upload for chunked upload integration |
| 6215 | // tests. |
| 6216 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6217 | writer->AppendData("a", 1, false); |
| 6218 | writer->AppendData("bcd", 3, false); |
| 6219 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6220 | writer->AppendData("\r\n\r\n", 4, false); |
| 6221 | writer->AppendData("0", 1, false); |
| 6222 | writer->AppendData("2323", 4, true); |
| 6223 | } |
| 6224 | |
| 6225 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6226 | // the server. |
| 6227 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6228 | // This should match the chunks sent by AddChunksToUpload(). |
| 6229 | const std::string expected_data = |
| 6230 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6231 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6232 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6233 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6234 | |
| 6235 | EXPECT_FALSE(d->received_data_before_response()); |
| 6236 | |
| 6237 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6238 | EXPECT_EQ(expected_data, d->data_received()); |
| 6239 | } |
| 6240 | |
| 6241 | } // namespace |
| 6242 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6243 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6244 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6245 | |
| 6246 | TestDelegate d; |
| 6247 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6248 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6249 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6250 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6251 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6252 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6253 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6254 | upload_data_stream->CreateWriter(); |
| 6255 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6256 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6257 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6258 | r->Start(); |
| 6259 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6260 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6261 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6262 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6263 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6264 | } |
| 6265 | } |
| 6266 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6267 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6268 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6269 | |
| 6270 | TestDelegate d; |
| 6271 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6272 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6273 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6274 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6275 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6276 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6277 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6278 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6279 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6280 | r->set_method("POST"); |
| 6281 | r->Start(); |
| 6282 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6283 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6284 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6285 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6286 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6287 | } |
| 6288 | } |
| 6289 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6290 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6291 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6292 | |
| 6293 | TestDelegate d; |
| 6294 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6295 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6296 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6297 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6298 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6299 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6300 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6301 | upload_data_stream->CreateWriter(); |
| 6302 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6303 | r->set_method("POST"); |
| 6304 | r->Start(); |
| 6305 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6306 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6307 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6308 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6309 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6310 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6311 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6316 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6317 | |
| 6318 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6319 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6320 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6321 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6322 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6323 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6324 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6325 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6326 | |
| 6327 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6328 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6329 | |
| 6330 | std::string header; |
| 6331 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6332 | EXPECT_EQ("private", header); |
| 6333 | |
| 6334 | header.clear(); |
| 6335 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6336 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6337 | |
| 6338 | // The response has two "X-Multiple-Entries" headers. |
| 6339 | // This verfies our output has them concatenated together. |
| 6340 | header.clear(); |
| 6341 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6342 | EXPECT_EQ("a, b", header); |
| 6343 | } |
| 6344 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6345 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6346 | // security state. (see http://crbug.com/550977). |
| 6347 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6348 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6349 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6350 | https_test_server.SetSSLConfig( |
| 6351 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6352 | https_test_server.ServeFilesFromSourceDirectory( |
| 6353 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6354 | ASSERT_TRUE(https_test_server.Start()); |
| 6355 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6356 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6357 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6358 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6359 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6360 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6361 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6362 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6363 | |
| 6364 | TransportSecurityState* security_state = |
| 6365 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6366 | TransportSecurityState::STSState sts_state; |
| 6367 | TransportSecurityState::PKPState pkp_state; |
| 6368 | EXPECT_TRUE( |
| 6369 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6370 | EXPECT_FALSE( |
| 6371 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6372 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6373 | sts_state.upgrade_mode); |
| 6374 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6375 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6376 | #if defined(OS_ANDROID) |
| 6377 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6378 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6379 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6380 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6381 | } |
| 6382 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6383 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6384 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6385 | https_test_server.ServeFilesFromSourceDirectory( |
| 6386 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6387 | ASSERT_TRUE(https_test_server.Start()); |
| 6388 | // Make sure this test fails if the test server is changed to not |
| 6389 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6390 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6391 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6392 | |
| 6393 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6394 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6395 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6396 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6397 | request->Start(); |
| 6398 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6399 | TransportSecurityState* security_state = |
| 6400 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6401 | TransportSecurityState::STSState sts_state; |
| 6402 | EXPECT_FALSE( |
| 6403 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6404 | } |
| 6405 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6406 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6407 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
| 6408 | const char kExpectStapleStaticHostname[] = "expect-staple.preloaded.test"; |
| 6409 | const char kExpectStapleReportURI[] = |
| 6410 | "http://report-uri.preloaded.test/expect-staple"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6411 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 6412 | } // namespace |
| 6413 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6414 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 6415 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6416 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6417 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6418 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6419 | https_test_server.SetSSLConfig( |
| 6420 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6421 | https_test_server.ServeFilesFromSourceDirectory( |
| 6422 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6423 | ASSERT_TRUE(https_test_server.Start()); |
| 6424 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6425 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6426 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6427 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6428 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6429 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6430 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6431 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6432 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6433 | TransportSecurityState* security_state = |
| 6434 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6435 | TransportSecurityState::STSState sts_state; |
| 6436 | TransportSecurityState::PKPState pkp_state; |
| 6437 | EXPECT_FALSE( |
| 6438 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6439 | EXPECT_TRUE( |
| 6440 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6441 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6442 | sts_state.upgrade_mode); |
| 6443 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6444 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6445 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6446 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6447 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6448 | } |
| 6449 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6450 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6451 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6452 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6453 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6454 | https_test_server.SetSSLConfig( |
| 6455 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6456 | https_test_server.ServeFilesFromSourceDirectory( |
| 6457 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6458 | ASSERT_TRUE(https_test_server.Start()); |
| 6459 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6460 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6461 | |
| 6462 | // Set up a pin for |test_server_hostname|. |
| 6463 | TransportSecurityState security_state; |
| 6464 | const base::Time current_time(base::Time::Now()); |
| 6465 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6466 | HashValueVector hashes; |
| 6467 | HashValue hash1; |
| 6468 | HashValue hash2; |
| 6469 | // The values here don't matter, as long as they are different from |
| 6470 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6471 | ASSERT_TRUE( |
| 6472 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6473 | ASSERT_TRUE( |
| 6474 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6475 | hashes.push_back(hash1); |
| 6476 | hashes.push_back(hash2); |
| 6477 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6478 | false, /* include subdomains */ |
| 6479 | hashes, report_uri); |
| 6480 | |
| 6481 | MockCertificateReportSender mock_report_sender; |
| 6482 | security_state.SetReportSender(&mock_report_sender); |
| 6483 | |
| 6484 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6485 | // set pin. |
| 6486 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6487 | ASSERT_TRUE(cert); |
| 6488 | |
| 6489 | MockCertVerifier cert_verifier; |
| 6490 | CertVerifyResult verify_result; |
| 6491 | verify_result.verified_cert = cert; |
| 6492 | verify_result.is_issued_by_known_root = true; |
| 6493 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6494 | ASSERT_TRUE( |
| 6495 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6496 | verify_result.public_key_hashes.push_back(hash3); |
| 6497 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6498 | |
| 6499 | TestNetworkDelegate network_delegate; |
| 6500 | TestURLRequestContext context(true); |
| 6501 | context.set_transport_security_state(&security_state); |
| 6502 | context.set_network_delegate(&network_delegate); |
| 6503 | context.set_cert_verifier(&cert_verifier); |
| 6504 | context.Init(); |
| 6505 | |
| 6506 | // Now send a request to trigger the violation. |
| 6507 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6508 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6509 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 6510 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6511 | violating_request->Start(); |
| 6512 | base::RunLoop().Run(); |
| 6513 | |
| 6514 | // Check that a report was sent. |
| 6515 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6516 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6517 | EXPECT_EQ("application/json; charset=utf-8", |
| 6518 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6519 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6520 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6521 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6522 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6523 | base::DictionaryValue* report_dict; |
| 6524 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6525 | std::string report_hostname; |
| 6526 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6527 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6528 | } |
| 6529 | |
| 6530 | // Tests that reports get sent on requests with |
| 6531 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6532 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6533 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6534 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6535 | https_test_server.SetSSLConfig( |
| 6536 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6537 | https_test_server.ServeFilesFromSourceDirectory( |
| 6538 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6539 | ASSERT_TRUE(https_test_server.Start()); |
| 6540 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6541 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6542 | |
| 6543 | TransportSecurityState security_state; |
| 6544 | MockCertificateReportSender mock_report_sender; |
| 6545 | security_state.SetReportSender(&mock_report_sender); |
| 6546 | |
| 6547 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6548 | // header. |
| 6549 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6550 | ASSERT_TRUE(cert); |
| 6551 | |
| 6552 | MockCertVerifier cert_verifier; |
| 6553 | CertVerifyResult verify_result; |
| 6554 | verify_result.verified_cert = cert; |
| 6555 | verify_result.is_issued_by_known_root = true; |
| 6556 | HashValue hash; |
| 6557 | // This value doesn't matter, as long as it is different from the pins |
| 6558 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6559 | ASSERT_TRUE( |
| 6560 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6561 | verify_result.public_key_hashes.push_back(hash); |
| 6562 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6563 | |
| 6564 | TestNetworkDelegate network_delegate; |
| 6565 | TestURLRequestContext context(true); |
| 6566 | context.set_transport_security_state(&security_state); |
| 6567 | context.set_network_delegate(&network_delegate); |
| 6568 | context.set_cert_verifier(&cert_verifier); |
| 6569 | context.Init(); |
| 6570 | |
| 6571 | // Now send a request to trigger the violation. |
| 6572 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6573 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6574 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6575 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6576 | violating_request->Start(); |
| 6577 | base::RunLoop().Run(); |
| 6578 | |
| 6579 | // Check that a report was sent. |
| 6580 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6581 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6582 | EXPECT_EQ("application/json; charset=utf-8", |
| 6583 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6584 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6585 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6586 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6587 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6588 | base::DictionaryValue* report_dict; |
| 6589 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6590 | std::string report_hostname; |
| 6591 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6592 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6593 | } |
| 6594 | |
| 6595 | // Tests that reports do not get sent on requests with |
| 6596 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6597 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6598 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6599 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6600 | https_test_server.SetSSLConfig( |
| 6601 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6602 | https_test_server.ServeFilesFromSourceDirectory( |
| 6603 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6604 | ASSERT_TRUE(https_test_server.Start()); |
| 6605 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6606 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6607 | |
| 6608 | TransportSecurityState security_state; |
| 6609 | MockCertificateReportSender mock_report_sender; |
| 6610 | security_state.SetReportSender(&mock_report_sender); |
| 6611 | |
| 6612 | TestNetworkDelegate network_delegate; |
| 6613 | MockCertVerifier mock_cert_verifier; |
| 6614 | TestURLRequestContext context(true); |
| 6615 | context.set_transport_security_state(&security_state); |
| 6616 | context.set_network_delegate(&network_delegate); |
| 6617 | context.set_cert_verifier(&mock_cert_verifier); |
| 6618 | mock_cert_verifier.set_default_result(OK); |
| 6619 | context.Init(); |
| 6620 | |
| 6621 | // Now send a request that does not trigger the violation. |
| 6622 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6623 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6624 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6625 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6626 | request->Start(); |
| 6627 | base::RunLoop().Run(); |
| 6628 | |
| 6629 | // Check that a report was not sent. |
| 6630 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6631 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6632 | } |
| 6633 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6634 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6635 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6636 | https_test_server.ServeFilesFromSourceDirectory( |
| 6637 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6638 | ASSERT_TRUE(https_test_server.Start()); |
| 6639 | // Make sure this test fails if the test server is changed to not |
| 6640 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6641 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6642 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6643 | |
| 6644 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6645 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6646 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6647 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6648 | request->Start(); |
| 6649 | base::RunLoop().Run(); |
| 6650 | |
| 6651 | TransportSecurityState* security_state = |
| 6652 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6653 | TransportSecurityState::PKPState pkp_state; |
| 6654 | EXPECT_FALSE( |
| 6655 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6656 | } |
| 6657 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6658 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6659 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6660 | https_test_server.SetSSLConfig( |
| 6661 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6662 | https_test_server.ServeFilesFromSourceDirectory( |
| 6663 | base::FilePath(kTestFilePath)); |
| 6664 | ASSERT_TRUE(https_test_server.Start()); |
| 6665 | |
| 6666 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6667 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6668 | ASSERT_TRUE(cert); |
| 6669 | |
| 6670 | MockCertVerifier cert_verifier; |
| 6671 | CertVerifyResult verify_result; |
| 6672 | verify_result.verified_cert = cert; |
| 6673 | verify_result.is_issued_by_known_root = false; |
| 6674 | HashValue hash; |
| 6675 | ASSERT_TRUE( |
| 6676 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6677 | verify_result.public_key_hashes.push_back(hash); |
| 6678 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6679 | cert_verifier.set_default_result(OK); |
| 6680 | |
| 6681 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6682 | |
| 6683 | // Set up HPKP |
| 6684 | base::Time current_time = base::Time::Now(); |
| 6685 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6686 | HashValue pin; |
| 6687 | ASSERT_TRUE( |
| 6688 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6689 | HashValueVector hashes; |
| 6690 | hashes.push_back(pin); |
| 6691 | GURL report_uri(kHPKPReportUri); |
| 6692 | TransportSecurityState security_state; |
| 6693 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6694 | false, /* include subdomains */ |
| 6695 | hashes, report_uri); |
| 6696 | |
| 6697 | TestNetworkDelegate network_delegate; |
| 6698 | TestURLRequestContext context(true); |
| 6699 | context.set_transport_security_state(&security_state); |
| 6700 | context.set_network_delegate(&network_delegate); |
| 6701 | context.set_cert_verifier(&cert_verifier); |
| 6702 | context.Init(); |
| 6703 | |
| 6704 | TestDelegate d; |
| 6705 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6706 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6707 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6708 | request->Start(); |
| 6709 | base::RunLoop().Run(); |
| 6710 | |
| 6711 | TransportSecurityState::PKPState pkp_state; |
| 6712 | EXPECT_TRUE( |
| 6713 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6714 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6715 | } |
| 6716 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6717 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6718 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6719 | https_test_server.SetSSLConfig( |
| 6720 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6721 | https_test_server.ServeFilesFromSourceDirectory( |
| 6722 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6723 | ASSERT_TRUE(https_test_server.Start()); |
| 6724 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6725 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6726 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6727 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6728 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6729 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6730 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6731 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6732 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6733 | |
| 6734 | // We should have set parameters from the first header, not the second. |
| 6735 | TransportSecurityState* security_state = |
| 6736 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6737 | TransportSecurityState::STSState sts_state; |
| 6738 | EXPECT_TRUE( |
| 6739 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6740 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6741 | sts_state.upgrade_mode); |
| 6742 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6743 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6744 | } |
| 6745 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6746 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6747 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6748 | https_test_server.SetSSLConfig( |
| 6749 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6750 | https_test_server.ServeFilesFromSourceDirectory( |
| 6751 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6752 | ASSERT_TRUE(https_test_server.Start()); |
| 6753 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6754 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6755 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6756 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6757 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6758 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6759 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6760 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6761 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6762 | |
| 6763 | // We should have set parameters from the first header, not the second. |
| 6764 | TransportSecurityState* security_state = |
| 6765 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6766 | TransportSecurityState::STSState sts_state; |
| 6767 | TransportSecurityState::PKPState pkp_state; |
| 6768 | EXPECT_TRUE( |
| 6769 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6770 | EXPECT_TRUE( |
| 6771 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6772 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6773 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6774 | #if defined(OS_ANDROID) |
| 6775 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6776 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6777 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6778 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6779 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6780 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6781 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6782 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6783 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6784 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6785 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6786 | } |
| 6787 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6788 | // Tests that when multiple HPKP headers are present, asserting different |
| 6789 | // policies, that only the first such policy is processed. |
| 6790 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6791 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6792 | https_test_server.SetSSLConfig( |
| 6793 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6794 | https_test_server.ServeFilesFromSourceDirectory( |
| 6795 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6796 | ASSERT_TRUE(https_test_server.Start()); |
| 6797 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6798 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6799 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6800 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6801 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6802 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6803 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6804 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6805 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6806 | |
| 6807 | TransportSecurityState* security_state = |
| 6808 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6809 | TransportSecurityState::STSState sts_state; |
| 6810 | TransportSecurityState::PKPState pkp_state; |
| 6811 | EXPECT_TRUE( |
| 6812 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6813 | EXPECT_TRUE( |
| 6814 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6815 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6816 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6817 | #if defined(OS_ANDROID) |
| 6818 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6819 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6820 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6821 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6822 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6823 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6824 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6825 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6826 | } |
| 6827 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6828 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6829 | // called. |
| 6830 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6831 | public: |
| 6832 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6833 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6834 | |
| 6835 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6836 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6837 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6838 | const X509Certificate* validated_certificate_chain, |
| 6839 | const X509Certificate* served_certificate_chain, |
| 6840 | const SignedCertificateTimestampAndStatusList& |
| 6841 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6842 | num_failures_++; |
| 6843 | } |
| 6844 | |
| 6845 | uint32_t num_failures() { return num_failures_; } |
| 6846 | |
| 6847 | private: |
| 6848 | uint32_t num_failures_; |
| 6849 | }; |
| 6850 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6851 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6852 | // for every certificate. |
| 6853 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6854 | public: |
| 6855 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6856 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6857 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6858 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6859 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6860 | X509Certificate* cert, |
| 6861 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6862 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6863 | return default_result_; |
| 6864 | } |
| 6865 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6866 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6867 | default_result_ = default_result; |
| 6868 | } |
| 6869 | |
| 6870 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6871 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6872 | }; |
| 6873 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6874 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 6875 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6876 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6877 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6878 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6879 | https_test_server.SetSSLConfig( |
| 6880 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6881 | https_test_server.ServeFilesFromSourceDirectory( |
| 6882 | base::FilePath(kTestFilePath)); |
| 6883 | ASSERT_TRUE(https_test_server.Start()); |
| 6884 | |
| 6885 | MockExpectCTReporter reporter; |
| 6886 | TransportSecurityState transport_security_state; |
| 6887 | transport_security_state.enable_static_expect_ct_ = true; |
| 6888 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6889 | |
| 6890 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6891 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6892 | ASSERT_TRUE(cert); |
| 6893 | MockCertVerifier cert_verifier; |
| 6894 | CertVerifyResult verify_result; |
| 6895 | verify_result.verified_cert = cert; |
| 6896 | verify_result.is_issued_by_known_root = true; |
| 6897 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6898 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 6899 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 6900 | // CT violation. |
| 6901 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6902 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6903 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6904 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6905 | |
| 6906 | TestNetworkDelegate network_delegate; |
| 6907 | // Use a MockHostResolver (which by default maps all hosts to |
| 6908 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6909 | // CT preload list. |
| 6910 | MockHostResolver host_resolver; |
| 6911 | TestURLRequestContext context(true); |
| 6912 | context.set_host_resolver(&host_resolver); |
| 6913 | context.set_transport_security_state(&transport_security_state); |
| 6914 | context.set_network_delegate(&network_delegate); |
| 6915 | context.set_cert_verifier(&cert_verifier); |
| 6916 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 6917 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6918 | context.Init(); |
| 6919 | |
| 6920 | // Now send a request to trigger the violation. |
| 6921 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6922 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6923 | GURL::Replacements replace_host; |
| 6924 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6925 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6926 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 6927 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6928 | violating_request->Start(); |
| 6929 | base::RunLoop().Run(); |
| 6930 | |
| 6931 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6932 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6933 | |
| 6934 | // Tests that Expect CT HTTP headers are processed correctly. |
| 6935 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6936 | base::test::ScopedFeatureList feature_list; |
| 6937 | feature_list.InitAndEnableFeature( |
| 6938 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6939 | |
| 6940 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6941 | https_test_server.SetSSLConfig( |
| 6942 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6943 | https_test_server.ServeFilesFromSourceDirectory( |
| 6944 | base::FilePath(kTestFilePath)); |
| 6945 | ASSERT_TRUE(https_test_server.Start()); |
| 6946 | |
| 6947 | MockExpectCTReporter reporter; |
| 6948 | TransportSecurityState transport_security_state; |
| 6949 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6950 | |
| 6951 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6952 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6953 | ASSERT_TRUE(cert); |
| 6954 | MockCertVerifier cert_verifier; |
| 6955 | CertVerifyResult verify_result; |
| 6956 | verify_result.verified_cert = cert; |
| 6957 | verify_result.is_issued_by_known_root = true; |
| 6958 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6959 | |
| 6960 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6961 | // compliance. |
| 6962 | DoNothingCTVerifier ct_verifier; |
| 6963 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6964 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6965 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6966 | |
| 6967 | TestNetworkDelegate network_delegate; |
| 6968 | // Use a MockHostResolver (which by default maps all hosts to |
| 6969 | // 127.0.0.1). |
| 6970 | MockHostResolver host_resolver; |
| 6971 | TestURLRequestContext context(true); |
| 6972 | context.set_host_resolver(&host_resolver); |
| 6973 | context.set_transport_security_state(&transport_security_state); |
| 6974 | context.set_network_delegate(&network_delegate); |
| 6975 | context.set_cert_verifier(&cert_verifier); |
| 6976 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6977 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6978 | context.Init(); |
| 6979 | |
| 6980 | // Now send a request to trigger the header processing. |
| 6981 | TestDelegate d; |
| 6982 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6983 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6984 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6985 | request->Start(); |
| 6986 | base::RunLoop().Run(); |
| 6987 | |
| 6988 | TransportSecurityState::ExpectCTState state; |
| 6989 | ASSERT_TRUE( |
| 6990 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6991 | EXPECT_TRUE(state.enforce); |
| 6992 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6993 | } |
| 6994 | |
| 6995 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 6996 | // processed. |
| 6997 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 6998 | base::test::ScopedFeatureList feature_list; |
| 6999 | feature_list.InitAndEnableFeature( |
| 7000 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7001 | |
| 7002 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7003 | https_test_server.SetSSLConfig( |
| 7004 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7005 | https_test_server.ServeFilesFromSourceDirectory( |
| 7006 | base::FilePath(kTestFilePath)); |
| 7007 | ASSERT_TRUE(https_test_server.Start()); |
| 7008 | |
| 7009 | MockExpectCTReporter reporter; |
| 7010 | TransportSecurityState transport_security_state; |
| 7011 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7012 | |
| 7013 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7014 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7015 | ASSERT_TRUE(cert); |
| 7016 | MockCertVerifier cert_verifier; |
| 7017 | CertVerifyResult verify_result; |
| 7018 | verify_result.verified_cert = cert; |
| 7019 | verify_result.is_issued_by_known_root = true; |
| 7020 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7021 | |
| 7022 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7023 | // compliance. |
| 7024 | DoNothingCTVerifier ct_verifier; |
| 7025 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7026 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7027 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7028 | |
| 7029 | TestNetworkDelegate network_delegate; |
| 7030 | // Use a MockHostResolver (which by default maps all hosts to |
| 7031 | // 127.0.0.1). |
| 7032 | MockHostResolver host_resolver; |
| 7033 | TestURLRequestContext context(true); |
| 7034 | context.set_host_resolver(&host_resolver); |
| 7035 | context.set_transport_security_state(&transport_security_state); |
| 7036 | context.set_network_delegate(&network_delegate); |
| 7037 | context.set_cert_verifier(&cert_verifier); |
| 7038 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7039 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7040 | context.Init(); |
| 7041 | |
| 7042 | // Now send a request to trigger the header processing. |
| 7043 | TestDelegate d; |
| 7044 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7045 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7046 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7047 | request->Start(); |
| 7048 | base::RunLoop().Run(); |
| 7049 | |
| 7050 | TransportSecurityState::ExpectCTState state; |
| 7051 | ASSERT_TRUE( |
| 7052 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7053 | EXPECT_TRUE(state.enforce); |
| 7054 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7055 | } |
| 7056 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7057 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7058 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7059 | #if BUILDFLAG(ENABLE_REPORTING) |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7060 | namespace { |
| 7061 | |
| 7062 | class TestReportingService : public ReportingService { |
| 7063 | public: |
| 7064 | struct Header { |
| 7065 | GURL url; |
| 7066 | std::string header_value; |
| 7067 | }; |
| 7068 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7069 | const std::vector<Header>& headers() { return headers_; } |
| 7070 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7071 | // ReportingService implementation: |
| 7072 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7073 | ~TestReportingService() override = default; |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7074 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7075 | void QueueReport(const GURL& url, |
| 7076 | const std::string& group, |
| 7077 | const std::string& type, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7078 | std::unique_ptr<const base::Value> body, |
| 7079 | int depth) override { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7080 | NOTIMPLEMENTED(); |
| 7081 | } |
| 7082 | |
| 7083 | void ProcessHeader(const GURL& url, |
| 7084 | const std::string& header_value) override { |
| 7085 | headers_.push_back({url, header_value}); |
| 7086 | } |
| 7087 | |
Julia Tuttle | 227a6ff | 2017-12-19 19:44:24 | [diff] [blame] | 7088 | void RemoveBrowsingData(int data_type_mask, |
| 7089 | const base::RepeatingCallback<bool(const GURL&)>& |
| 7090 | origin_filter) override { |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 7091 | NOTIMPLEMENTED(); |
| 7092 | } |
| 7093 | |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7094 | int GetUploadDepth(const URLRequest& request) override { |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 7095 | NOTIMPLEMENTED(); |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7096 | return 0; |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 7097 | } |
| 7098 | |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 7099 | const ReportingPolicy& GetPolicy() const override { |
| 7100 | static ReportingPolicy dummy_policy_; |
| 7101 | NOTIMPLEMENTED(); |
| 7102 | return dummy_policy_; |
| 7103 | } |
| 7104 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7105 | private: |
| 7106 | std::vector<Header> headers_; |
| 7107 | }; |
| 7108 | |
| 7109 | std::unique_ptr<test_server::HttpResponse> SendReportToHeader( |
| 7110 | const test_server::HttpRequest& request) { |
| 7111 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7112 | new test_server::BasicHttpResponse); |
| 7113 | http_response->set_code(HTTP_OK); |
| 7114 | http_response->AddCustomHeader("Report-To", "foo"); |
| 7115 | http_response->AddCustomHeader("Report-To", "bar"); |
| 7116 | return std::move(http_response); |
| 7117 | } |
| 7118 | |
| 7119 | } // namespace |
| 7120 | |
| 7121 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderNoService) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7122 | http_test_server()->RegisterRequestHandler( |
| 7123 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7124 | ASSERT_TRUE(http_test_server()->Start()); |
| 7125 | GURL request_url = http_test_server()->GetURL("/"); |
| 7126 | |
| 7127 | TestNetworkDelegate network_delegate; |
| 7128 | TestURLRequestContext context(true); |
| 7129 | context.set_network_delegate(&network_delegate); |
| 7130 | context.Init(); |
| 7131 | |
| 7132 | TestDelegate d; |
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 | |
| 7139 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderHTTP) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7140 | http_test_server()->RegisterRequestHandler( |
| 7141 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7142 | ASSERT_TRUE(http_test_server()->Start()); |
| 7143 | GURL request_url = http_test_server()->GetURL("/"); |
| 7144 | |
| 7145 | TestNetworkDelegate network_delegate; |
| 7146 | TestReportingService reporting_service; |
| 7147 | TestURLRequestContext context(true); |
| 7148 | context.set_network_delegate(&network_delegate); |
| 7149 | context.set_reporting_service(&reporting_service); |
| 7150 | context.Init(); |
| 7151 | |
| 7152 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7153 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7154 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7155 | request->Start(); |
| 7156 | base::RunLoop().Run(); |
| 7157 | |
| 7158 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7159 | } |
| 7160 | |
| 7161 | TEST_F(URLRequestTestHTTP, ProcessReportToHeaderHTTPS) { |
| 7162 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7163 | https_test_server.RegisterRequestHandler( |
| 7164 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7165 | ASSERT_TRUE(https_test_server.Start()); |
| 7166 | GURL request_url = https_test_server.GetURL("/"); |
| 7167 | |
| 7168 | TestNetworkDelegate network_delegate; |
| 7169 | TestReportingService reporting_service; |
| 7170 | TestURLRequestContext context(true); |
| 7171 | context.set_network_delegate(&network_delegate); |
| 7172 | context.set_reporting_service(&reporting_service); |
| 7173 | context.Init(); |
| 7174 | |
| 7175 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7176 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7177 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7178 | request->Start(); |
| 7179 | base::RunLoop().Run(); |
| 7180 | |
| 7181 | ASSERT_EQ(1u, reporting_service.headers().size()); |
| 7182 | EXPECT_EQ(request_url, reporting_service.headers()[0].url); |
| 7183 | EXPECT_EQ("foo, bar", reporting_service.headers()[0].header_value); |
| 7184 | } |
| 7185 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7186 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderInvalidHttps) { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7187 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7188 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7189 | https_test_server.RegisterRequestHandler( |
| 7190 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7191 | ASSERT_TRUE(https_test_server.Start()); |
| 7192 | GURL request_url = https_test_server.GetURL("/"); |
| 7193 | |
| 7194 | TestNetworkDelegate network_delegate; |
| 7195 | TestReportingService reporting_service; |
| 7196 | TestURLRequestContext context(true); |
| 7197 | context.set_network_delegate(&network_delegate); |
| 7198 | context.set_reporting_service(&reporting_service); |
| 7199 | context.Init(); |
| 7200 | |
| 7201 | TestDelegate d; |
| 7202 | d.set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7203 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7204 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7205 | request->Start(); |
| 7206 | base::RunLoop().Run(); |
| 7207 | |
| 7208 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7209 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7210 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7211 | } |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7212 | |
| 7213 | // Network Error Logging is dependent on the Reporting API, so only run NEL |
| 7214 | // tests if Reporting is enabled in the build. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7215 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7216 | namespace { |
| 7217 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7218 | class TestNetworkErrorLoggingService : public NetworkErrorLoggingService { |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7219 | public: |
| 7220 | struct Header { |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7221 | Header() = default; |
| 7222 | ~Header() = default; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7223 | |
| 7224 | url::Origin origin; |
| 7225 | std::string value; |
| 7226 | }; |
| 7227 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7228 | const std::vector<Header>& headers() { return headers_; } |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7229 | const std::vector<RequestDetails>& errors() { return errors_; } |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7230 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7231 | // NetworkErrorLoggingService implementation: |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7232 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7233 | ~TestNetworkErrorLoggingService() override = default; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7234 | |
| 7235 | void OnHeader(const url::Origin& origin, const std::string& value) override { |
| 7236 | Header header; |
| 7237 | header.origin = origin; |
| 7238 | header.value = value; |
| 7239 | headers_.push_back(header); |
| 7240 | } |
| 7241 | |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7242 | void OnRequest(const RequestDetails& details) override { |
Julia Tuttle | b0d4547 | 2017-09-25 20:14:43 | [diff] [blame] | 7243 | errors_.push_back(details); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7244 | } |
| 7245 | |
Julia Tuttle | a059db8 | 2018-01-04 18:46:43 | [diff] [blame] | 7246 | void RemoveBrowsingData(const base::RepeatingCallback<bool(const GURL&)>& |
| 7247 | origin_filter) override { |
| 7248 | NOTREACHED(); |
| 7249 | } |
| 7250 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7251 | private: |
| 7252 | std::vector<Header> headers_; |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7253 | std::vector<RequestDetails> errors_; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7254 | }; |
| 7255 | |
| 7256 | std::unique_ptr<test_server::HttpResponse> SendNelHeader( |
| 7257 | const test_server::HttpRequest& request) { |
| 7258 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7259 | new test_server::BasicHttpResponse); |
| 7260 | http_response->set_code(HTTP_OK); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7261 | http_response->AddCustomHeader(NetworkErrorLoggingService::kHeaderName, |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7262 | "foo"); |
| 7263 | return std::move(http_response); |
| 7264 | } |
| 7265 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7266 | std::unique_ptr<test_server::HttpResponse> SendEmptyResponse( |
| 7267 | const test_server::HttpRequest& request) { |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame] | 7268 | return std::make_unique<test_server::RawHttpResponse>("", ""); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7269 | } |
| 7270 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7271 | } // namespace |
| 7272 | |
| 7273 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderNoDelegate) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7274 | http_test_server()->RegisterRequestHandler( |
| 7275 | base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7276 | ASSERT_TRUE(http_test_server()->Start()); |
| 7277 | GURL request_url = http_test_server()->GetURL("/"); |
| 7278 | |
| 7279 | TestNetworkDelegate network_delegate; |
| 7280 | TestURLRequestContext context(true); |
| 7281 | context.set_network_delegate(&network_delegate); |
| 7282 | context.Init(); |
| 7283 | |
| 7284 | TestDelegate d; |
| 7285 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7286 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7287 | request->Start(); |
| 7288 | base::RunLoop().Run(); |
| 7289 | } |
| 7290 | |
| 7291 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderHttp) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7292 | http_test_server()->RegisterRequestHandler( |
| 7293 | base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7294 | ASSERT_TRUE(http_test_server()->Start()); |
| 7295 | GURL request_url = http_test_server()->GetURL("/"); |
| 7296 | |
| 7297 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7298 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7299 | TestURLRequestContext context(true); |
| 7300 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7301 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7302 | context.Init(); |
| 7303 | |
| 7304 | TestDelegate d; |
| 7305 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7306 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7307 | request->Start(); |
| 7308 | base::RunLoop().Run(); |
| 7309 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7310 | EXPECT_TRUE(nel_service.headers().empty()); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | TEST_F(URLRequestTestHTTP, ProcessNelHeaderHttps) { |
| 7314 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7315 | https_test_server.RegisterRequestHandler(base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7316 | ASSERT_TRUE(https_test_server.Start()); |
| 7317 | GURL request_url = https_test_server.GetURL("/"); |
| 7318 | |
| 7319 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7320 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7321 | TestURLRequestContext context(true); |
| 7322 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7323 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7324 | context.Init(); |
| 7325 | |
| 7326 | TestDelegate d; |
| 7327 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7328 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7329 | request->Start(); |
| 7330 | base::RunLoop().Run(); |
| 7331 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7332 | ASSERT_EQ(1u, nel_service.headers().size()); |
| 7333 | EXPECT_EQ(url::Origin::Create(request_url), nel_service.headers()[0].origin); |
| 7334 | EXPECT_EQ("foo", nel_service.headers()[0].value); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7335 | } |
| 7336 | |
| 7337 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderInvalidHttps) { |
| 7338 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7339 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7340 | https_test_server.RegisterRequestHandler(base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7341 | ASSERT_TRUE(https_test_server.Start()); |
| 7342 | GURL request_url = https_test_server.GetURL("/"); |
| 7343 | |
| 7344 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7345 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7346 | TestURLRequestContext context(true); |
| 7347 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7348 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7349 | context.Init(); |
| 7350 | |
| 7351 | TestDelegate d; |
| 7352 | d.set_allow_certificate_errors(true); |
| 7353 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7354 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7355 | request->Start(); |
| 7356 | base::RunLoop().Run(); |
| 7357 | |
| 7358 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7359 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7360 | EXPECT_TRUE(nel_service.headers().empty()); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7361 | } |
| 7362 | |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7363 | TEST_F(URLRequestTestHTTP, DontForwardErrorToNelNoDelegate) { |
| 7364 | URLRequestFailedJob::AddUrlHandler(); |
| 7365 | |
| 7366 | GURL request_url = |
| 7367 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7368 | |
| 7369 | TestNetworkDelegate network_delegate; |
| 7370 | TestURLRequestContext context(true); |
| 7371 | context.set_network_delegate(&network_delegate); |
| 7372 | context.Init(); |
| 7373 | |
| 7374 | TestDelegate d; |
| 7375 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7376 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7377 | request->Start(); |
| 7378 | base::RunLoop().Run(); |
| 7379 | |
| 7380 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7381 | } |
| 7382 | |
| 7383 | // TODO(juliatuttle): Figure out whether this restriction should be in place, |
| 7384 | // and either implement it or remove this test. |
| 7385 | TEST_F(URLRequestTestHTTP, DISABLED_DontForwardErrorToNelHttp) { |
| 7386 | URLRequestFailedJob::AddUrlHandler(); |
| 7387 | |
| 7388 | GURL request_url = |
| 7389 | URLRequestFailedJob::GetMockHttpUrl(ERR_CONNECTION_REFUSED); |
| 7390 | |
| 7391 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7392 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7393 | TestURLRequestContext context(true); |
| 7394 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7395 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7396 | context.Init(); |
| 7397 | |
| 7398 | TestDelegate d; |
| 7399 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7400 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7401 | request->Start(); |
| 7402 | base::RunLoop().Run(); |
| 7403 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7404 | EXPECT_TRUE(nel_service.errors().empty()); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7405 | |
| 7406 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7407 | } |
| 7408 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7409 | TEST_F(URLRequestTestHTTP, ForwardErrorToNelHttps_Mock) { |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7410 | URLRequestFailedJob::AddUrlHandler(); |
| 7411 | |
| 7412 | GURL request_url = |
| 7413 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7414 | |
| 7415 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7416 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7417 | TestURLRequestContext context(true); |
| 7418 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7419 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7420 | context.Init(); |
| 7421 | |
| 7422 | TestDelegate d; |
| 7423 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7424 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7425 | request->Start(); |
| 7426 | base::RunLoop().Run(); |
| 7427 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7428 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7429 | EXPECT_EQ(request_url, nel_service.errors()[0].uri); |
| 7430 | EXPECT_EQ(0, nel_service.errors()[0].status_code); |
| 7431 | EXPECT_EQ(ERR_CONNECTION_REFUSED, nel_service.errors()[0].type); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7432 | |
| 7433 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7434 | } |
| 7435 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7436 | // Also test with a real server, to exercise interactions with |
| 7437 | // URLRequestHttpJob. |
| 7438 | TEST_F(URLRequestTestHTTP, ForwardErrorToNelHttps_Real) { |
| 7439 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7440 | https_test_server.RegisterRequestHandler( |
| 7441 | base::BindRepeating(&SendEmptyResponse)); |
| 7442 | ASSERT_TRUE(https_test_server.Start()); |
| 7443 | GURL request_url = https_test_server.GetURL("/"); |
| 7444 | |
| 7445 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7446 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7447 | TestURLRequestContext context(true); |
| 7448 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7449 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7450 | context.Init(); |
| 7451 | |
| 7452 | TestDelegate d; |
| 7453 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7454 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7455 | request->Start(); |
| 7456 | base::RunLoop().Run(); |
| 7457 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7458 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7459 | EXPECT_EQ(request_url, nel_service.errors()[0].uri); |
| 7460 | EXPECT_EQ(0, nel_service.errors()[0].status_code); |
| 7461 | EXPECT_EQ(ERR_EMPTY_RESPONSE, nel_service.errors()[0].type); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7462 | } |
| 7463 | |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7464 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7465 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7466 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7467 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7468 | |
| 7469 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7470 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7471 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7472 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7473 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7474 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7475 | |
| 7476 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7477 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7478 | EXPECT_EQ("text/html", mime_type); |
| 7479 | |
| 7480 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7481 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7482 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7483 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7484 | } |
| 7485 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7486 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7487 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7488 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7489 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7490 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7491 | |
| 7492 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7493 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7494 | } |
| 7495 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7496 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7497 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7498 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7499 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7500 | FileProtocolHandler file_protocol_handler( |
| 7501 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7502 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7503 | |
| 7504 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7505 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7506 | } |
| 7507 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7508 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7509 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7510 | |
| 7511 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7512 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7513 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7514 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7515 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7516 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7517 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7518 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7519 | |
| 7520 | // The redirect should have been rejected before reporting it to the caller. |
| 7521 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7522 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7523 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7524 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7525 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7526 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7527 | |
| 7528 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7529 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7530 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7531 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7532 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 7533 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7534 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7535 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7536 | |
| 7537 | // The redirect should have been rejected before reporting it to the |
| 7538 | // caller. See https://crbug.com/723796 |
| 7539 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7540 | } |
| 7541 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7542 | // Test that redirects to invalid URLs are rejected. See |
| 7543 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7544 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7545 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7546 | |
| 7547 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7548 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7549 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7550 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7551 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7552 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7553 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7554 | EXPECT_EQ(1, d.response_started_count()); |
| 7555 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7556 | |
| 7557 | // The redirect should have been rejected before reporting it to the caller. |
| 7558 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7559 | } |
| 7560 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7561 | // Make sure redirects are cached, despite not reading their bodies. |
| 7562 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7563 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7564 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7565 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7566 | |
| 7567 | { |
| 7568 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7569 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7570 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7571 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7572 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7573 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7574 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7575 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7576 | } |
| 7577 | |
| 7578 | { |
| 7579 | TestDelegate d; |
| 7580 | d.set_quit_on_redirect(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7581 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7582 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7583 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7584 | base::RunLoop().Run(); |
| 7585 | |
| 7586 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7587 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7588 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7590 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7591 | base::RunLoop().Run(); |
| 7592 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7593 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7594 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7595 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7600 | // when the headers are read, since the body won't have been read. |
| 7601 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7603 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7604 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7605 | |
| 7606 | { |
| 7607 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7608 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7609 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7610 | redirect_to_url); |
| 7611 | |
| 7612 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7613 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7614 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7615 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7616 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7617 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7618 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7619 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7620 | } |
| 7621 | |
| 7622 | { |
| 7623 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7624 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7625 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7626 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7627 | base::RunLoop().Run(); |
| 7628 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7629 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7630 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7631 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7632 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7633 | } |
| 7634 | } |
| 7635 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7636 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7637 | // safe. |
| 7638 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7639 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7640 | |
| 7641 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7642 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7643 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7644 | |
| 7645 | TestDelegate d; |
| 7646 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7647 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7648 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7649 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7650 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7651 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7652 | base::RunLoop().Run(); |
| 7653 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7654 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7655 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7656 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7657 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7658 | } |
| 7659 | } |
| 7660 | |
| 7661 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7662 | // some other URL to the whitelist. |
| 7663 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7664 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7665 | |
| 7666 | GURL unsafe_url("data:text/html,something"); |
| 7667 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7668 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7669 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7670 | different_unsafe_url); |
| 7671 | |
| 7672 | TestDelegate d; |
| 7673 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7674 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7675 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7676 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7677 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7678 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7679 | base::RunLoop().Run(); |
| 7680 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7681 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7682 | |
| 7683 | // The redirect should have been rejected before reporting it to the caller. |
| 7684 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7685 | } |
| 7686 | } |
| 7687 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7688 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7689 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7690 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7691 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7692 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7693 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7694 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7695 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7696 | |
| 7697 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7698 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7699 | |
| 7700 | TestDelegate d; |
| 7701 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7702 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7703 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7704 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7705 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7706 | base::RunLoop().Run(); |
| 7707 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7708 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7709 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7710 | EXPECT_EQ(original_url, r->original_url()); |
| 7711 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7712 | } |
| 7713 | } |
| 7714 | |
| 7715 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7716 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7717 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7718 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7719 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7720 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7721 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7722 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7723 | GURL expected_redirect_url( |
| 7724 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7725 | |
| 7726 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7727 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7728 | |
| 7729 | TestDelegate d; |
| 7730 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7731 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7732 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7733 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7734 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7735 | base::RunLoop().Run(); |
| 7736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7737 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7738 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7739 | EXPECT_EQ(original_url, r->original_url()); |
| 7740 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7741 | } |
| 7742 | } |
| 7743 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7744 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7745 | // URL should not be changed. In particular, the reference fragment should not |
| 7746 | // be modified. |
| 7747 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7748 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7749 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7750 | GURL original_url( |
| 7751 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7752 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7753 | |
| 7754 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7755 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7756 | |
| 7757 | TestDelegate d; |
| 7758 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7759 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7760 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7761 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7762 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7763 | base::RunLoop().Run(); |
| 7764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7765 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7766 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7767 | EXPECT_EQ(original_url, r->original_url()); |
| 7768 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7769 | } |
| 7770 | } |
| 7771 | |
| 7772 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7773 | // the reference fragment of the target URL must not be modified. |
| 7774 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7775 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7776 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7777 | GURL original_url( |
| 7778 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7779 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7780 | |
| 7781 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7782 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7783 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7784 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7785 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7786 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7787 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7788 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7789 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7790 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7791 | base::RunLoop().Run(); |
| 7792 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7793 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7794 | EXPECT_EQ(original_url, r->original_url()); |
| 7795 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7796 | } |
| 7797 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7798 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7799 | ASSERT_TRUE(http_test_server()->Start()); |
| 7800 | |
| 7801 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7802 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7803 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7804 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7805 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7806 | req->SetReferrer(referrer); |
| 7807 | req->Start(); |
| 7808 | base::RunLoop().Run(); |
| 7809 | |
| 7810 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7811 | } |
| 7812 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7813 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7814 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7815 | |
| 7816 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7817 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7818 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7819 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7820 | req->SetReferrer("http://user:[email protected]/"); |
| 7821 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7822 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7823 | |
| 7824 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7825 | } |
| 7826 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7827 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7828 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7829 | |
| 7830 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7831 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7832 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7833 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7834 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7835 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7836 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7837 | |
| 7838 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7839 | } |
| 7840 | |
| 7841 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7842 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7843 | |
| 7844 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7845 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7846 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7847 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7848 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7849 | req->SetReferrer(""); |
| 7850 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7851 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7852 | |
| 7853 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7854 | } |
| 7855 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7856 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7857 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7858 | |
| 7859 | TestDelegate d; |
| 7860 | { |
| 7861 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7862 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7863 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7864 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7865 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7866 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7867 | |
| 7868 | EXPECT_EQ(1, d.response_started_count()); |
| 7869 | EXPECT_EQ(0, d.bytes_received()); |
| 7870 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7871 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7872 | } |
| 7873 | } |
| 7874 | |
| 7875 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7876 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7877 | |
| 7878 | TestDelegate d; |
| 7879 | { |
| 7880 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7881 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7882 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7883 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7884 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7885 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7886 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7887 | |
| 7888 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 7889 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7890 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7891 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7892 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7893 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7894 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7895 | |
| 7896 | EXPECT_EQ(1, d.response_started_count()); |
| 7897 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7898 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7899 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7900 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7901 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7902 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7903 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7904 | |
| 7905 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7906 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7907 | EXPECT_EQ(contents, d.data_received()); |
| 7908 | } |
| 7909 | } |
| 7910 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7911 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7912 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7913 | |
| 7914 | TestDelegate d; |
| 7915 | { |
| 7916 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7917 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7918 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7919 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7920 | |
| 7921 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7922 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7923 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7924 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7925 | |
| 7926 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7927 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7928 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7929 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7930 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7931 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7932 | EXPECT_EQ(1, d.response_started_count()); |
| 7933 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7934 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7935 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7936 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7937 | |
| 7938 | base::FilePath path; |
| 7939 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7940 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7941 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7942 | |
| 7943 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7944 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7945 | EXPECT_EQ(contents, d.data_received()); |
| 7946 | } |
| 7947 | } |
| 7948 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7949 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7950 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7951 | |
| 7952 | TestDelegate d; |
| 7953 | { |
| 7954 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7955 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7956 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7957 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7958 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7959 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7960 | |
| 7961 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7962 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7963 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7964 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7965 | |
| 7966 | EXPECT_EQ(1, d.response_started_count()); |
| 7967 | EXPECT_EQ(0, d.bytes_received()); |
| 7968 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7969 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7970 | } |
| 7971 | } |
| 7972 | |
| 7973 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7974 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7975 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7976 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7977 | { |
| 7978 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7979 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7980 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7981 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7982 | HttpRequestHeaders headers; |
| 7983 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7984 | req->SetExtraRequestHeaders(headers); |
| 7985 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7986 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7987 | |
| 7988 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7989 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7990 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7991 | } |
| 7992 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7993 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7994 | { |
| 7995 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7996 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7997 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7998 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7999 | HttpRequestHeaders headers; |
| 8000 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8001 | req->SetExtraRequestHeaders(headers); |
| 8002 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8003 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8004 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8005 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8006 | |
| 8007 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8008 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8009 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8010 | } |
| 8011 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8012 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8013 | { |
| 8014 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8015 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8016 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8017 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8018 | HttpRequestHeaders headers; |
| 8019 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8020 | req->SetExtraRequestHeaders(headers); |
| 8021 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8022 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8023 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8024 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8025 | |
| 8026 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8027 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8028 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8029 | } |
| 8030 | } |
| 8031 | |
| 8032 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8033 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8034 | |
| 8035 | // populate the cache |
| 8036 | { |
| 8037 | TestDelegate d; |
| 8038 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8039 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8040 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8041 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8042 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8043 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8044 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8045 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8046 | |
| 8047 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8048 | } |
| 8049 | |
| 8050 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8051 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8052 | // response should be fetched from the cache. |
| 8053 | { |
| 8054 | TestDelegate d; |
| 8055 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8056 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8057 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8058 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8060 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8061 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8062 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8063 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8064 | |
| 8065 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8066 | |
| 8067 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8068 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8069 | } |
| 8070 | } |
| 8071 | |
| 8072 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8073 | // http://crbug.com/6450 |
| 8074 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8075 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8076 | |
| 8077 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8078 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8079 | |
| 8080 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8081 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8082 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8083 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8084 | TestURLRequestContext context(true); |
| 8085 | context.set_network_delegate(&network_delegate); |
| 8086 | context.Init(); |
| 8087 | |
| 8088 | TestDelegate d; |
| 8089 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8090 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8091 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8092 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8093 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8094 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8095 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8096 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8097 | |
| 8098 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8099 | |
| 8100 | // Make sure we sent the cookie in the restarted transaction. |
| 8101 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8102 | != std::string::npos); |
| 8103 | } |
| 8104 | |
| 8105 | // Same test as above, except this time the restart is initiated earlier |
| 8106 | // (without user intervention since identity is embedded in the URL). |
| 8107 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8108 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8109 | TestURLRequestContext context(true); |
| 8110 | context.set_network_delegate(&network_delegate); |
| 8111 | context.Init(); |
| 8112 | |
| 8113 | TestDelegate d; |
| 8114 | |
| 8115 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8116 | replacements.SetUsernameStr("user2"); |
| 8117 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8118 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8119 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8120 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8121 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8122 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8123 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8124 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8125 | |
| 8126 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8127 | |
| 8128 | // Make sure we sent the cookie in the restarted transaction. |
| 8129 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8130 | != std::string::npos); |
| 8131 | } |
| 8132 | } |
| 8133 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8134 | // Tests that load timing works as expected with auth and the cache. |
| 8135 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8136 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8137 | |
| 8138 | // populate the cache |
| 8139 | { |
| 8140 | TestDelegate d; |
| 8141 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8142 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8143 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8144 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8145 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8146 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8147 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8148 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8149 | |
| 8150 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8151 | |
| 8152 | LoadTimingInfo load_timing_info_before_auth; |
| 8153 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8154 | &load_timing_info_before_auth)); |
| 8155 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8156 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8157 | |
| 8158 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8159 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8160 | // The test server does not support keep alive sockets, so the second |
| 8161 | // request with auth should use a new socket. |
| 8162 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8163 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8164 | load_timing_info.socket_log_id); |
| 8165 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8166 | load_timing_info.connect_timing.connect_start); |
| 8167 | } |
| 8168 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8169 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8170 | // 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] | 8171 | // response should be fetched from the cache. |
| 8172 | { |
| 8173 | TestDelegate d; |
| 8174 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8175 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8176 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8177 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8178 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8179 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8180 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8181 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8182 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8183 | |
| 8184 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8185 | |
| 8186 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8187 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8188 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8189 | // Since there was a request that went over the wire, the load timing |
| 8190 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8191 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8192 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8193 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8194 | } |
| 8195 | } |
| 8196 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8197 | // In this test, we do a POST which the server will 302 redirect. |
| 8198 | // The subsequent transaction should use GET, and should not send the |
| 8199 | // Content-Type header. |
| 8200 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8201 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8202 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8203 | |
| 8204 | const char kData[] = "hello world"; |
| 8205 | |
| 8206 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8207 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8208 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8209 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8210 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8211 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8212 | |
| 8213 | // Set headers (some of which are specific to the POST). |
| 8214 | HttpRequestHeaders headers; |
| 8215 | headers.AddHeadersFromString( |
| 8216 | "Content-Type: multipart/form-data; " |
| 8217 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 8218 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 8219 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 8220 | "Accept-Language: en-US,en\r\n" |
| 8221 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 8222 | "Content-Length: 11\r\n" |
| 8223 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8224 | req->SetExtraRequestHeaders(headers); |
| 8225 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8226 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8227 | |
| 8228 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8229 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8230 | EXPECT_EQ("text/html", mime_type); |
| 8231 | |
| 8232 | const std::string& data = d.data_received(); |
| 8233 | |
| 8234 | // Check that the post-specific headers were stripped: |
| 8235 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8236 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8237 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8238 | |
| 8239 | // These extra request headers should not have been stripped. |
| 8240 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8241 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8242 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8243 | } |
| 8244 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8245 | // The following tests check that we handle mutating the request for HTTP |
| 8246 | // redirects as expected. |
| 8247 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8248 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8249 | |
| 8250 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8251 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8252 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8253 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8254 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8255 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8256 | |
| 8257 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8258 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8259 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8260 | |
| 8261 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8262 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8263 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8264 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8265 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8266 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8267 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8268 | } |
| 8269 | |
| 8270 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8271 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8272 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8273 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8274 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8275 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8276 | |
| 8277 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8278 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8279 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8280 | |
| 8281 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8282 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8283 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8284 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8285 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8286 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8287 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8291 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8292 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8293 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8294 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8295 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8296 | |
| 8297 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8298 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8299 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8300 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8301 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8302 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8303 | std::string()); |
| 8304 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8305 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8306 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8307 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8308 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8309 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8310 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8311 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8312 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8313 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8314 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8315 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8316 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8317 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8318 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8319 | } |
| 8320 | |
| 8321 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8322 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8323 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8324 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8325 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8326 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8327 | |
| 8328 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8329 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8330 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8331 | |
| 8332 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8333 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8334 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8335 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8336 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8337 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8338 | } |
| 8339 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8340 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8341 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8342 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8343 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8344 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8345 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8346 | |
| 8347 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8348 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8349 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8350 | |
| 8351 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8352 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8353 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8354 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8355 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8356 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8357 | } |
| 8358 | |
| 8359 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8360 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8361 | // (Like Google Drive). |
| 8362 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8363 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8364 | |
| 8365 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8366 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8367 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8368 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
| 8369 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8370 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8371 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8372 | base::RunLoop().Run(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8373 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8374 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8375 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8376 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8377 | } |
| 8378 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8379 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8380 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8381 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8382 | GURL original_url( |
| 8383 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8384 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8385 | |
| 8386 | TestDelegate d; |
| 8387 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8388 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8389 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8390 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8391 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8392 | base::RunLoop().Run(); |
| 8393 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8394 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8395 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8396 | EXPECT_EQ(original_url, r->original_url()); |
| 8397 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8398 | } |
| 8399 | } |
| 8400 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8401 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8402 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8403 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8404 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8405 | GURL first_party_url("http://example.com"); |
| 8406 | |
| 8407 | TestDelegate d; |
| 8408 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8409 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8410 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8411 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8412 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8413 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8414 | base::RunLoop().Run(); |
| 8415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8416 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8417 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8418 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8419 | } |
| 8420 | } |
| 8421 | |
| 8422 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8423 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8424 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8425 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8426 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8427 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8428 | |
| 8429 | TestDelegate d; |
| 8430 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8431 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8432 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8433 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8434 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8435 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8436 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8437 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8438 | base::RunLoop().Run(); |
| 8439 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8440 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8441 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8442 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8443 | } |
| 8444 | } |
| 8445 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8446 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8447 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8448 | |
| 8449 | const char kData[] = "hello world"; |
| 8450 | |
| 8451 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8452 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8453 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8454 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8455 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8456 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8457 | HttpRequestHeaders headers; |
| 8458 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 8459 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8460 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8461 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8462 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8463 | req.get(), &default_network_delegate_, |
| 8464 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8465 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8466 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8467 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8468 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8469 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8470 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8471 | } |
| 8472 | |
| 8473 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8474 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8475 | |
| 8476 | const char kData[] = "hello world"; |
| 8477 | |
| 8478 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8479 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8480 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8482 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8483 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8484 | HttpRequestHeaders headers; |
| 8485 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 8486 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8487 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8488 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8489 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8490 | req.get(), &default_network_delegate_, |
| 8491 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8492 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8493 | "Very Good Reason")); |
| 8494 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8495 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8496 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8497 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8498 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8499 | EXPECT_EQ(kData, d.data_received()); |
| 8500 | } |
| 8501 | |
| 8502 | // Check that default A-L header is sent. |
| 8503 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8504 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8505 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8506 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8507 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8508 | TestURLRequestContext context(true); |
| 8509 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8510 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8511 | context.Init(); |
| 8512 | |
| 8513 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8514 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8515 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8516 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8517 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8518 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8519 | EXPECT_EQ("en", d.data_received()); |
| 8520 | } |
| 8521 | |
| 8522 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8523 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8524 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8525 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8526 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8527 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8528 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8529 | TestURLRequestContext context(true); |
| 8530 | context.set_network_delegate(&network_delegate); |
| 8531 | context.Init(); |
| 8532 | // We override the language after initialization because empty entries |
| 8533 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8534 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8535 | |
| 8536 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8537 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8538 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8539 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8540 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8541 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8542 | EXPECT_EQ("None", d.data_received()); |
| 8543 | } |
| 8544 | |
| 8545 | // Check that if request overrides the A-L header, the default is not appended. |
| 8546 | // See http://crbug.com/20894 |
| 8547 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8548 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8549 | |
| 8550 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8551 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8552 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8553 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8554 | HttpRequestHeaders headers; |
| 8555 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8556 | req->SetExtraRequestHeaders(headers); |
| 8557 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8558 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8559 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8560 | } |
| 8561 | |
| 8562 | // Check that default A-E header is sent. |
| 8563 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8564 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8565 | |
| 8566 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8567 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8568 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8569 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8570 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8571 | req->SetExtraRequestHeaders(headers); |
| 8572 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8573 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8574 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8575 | } |
| 8576 | |
| 8577 | // Check that if request overrides the A-E header, the default is not appended. |
| 8578 | // See http://crbug.com/47381 |
| 8579 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8580 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8581 | |
| 8582 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8583 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8584 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8585 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8586 | HttpRequestHeaders headers; |
| 8587 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8588 | req->SetExtraRequestHeaders(headers); |
| 8589 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8590 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8591 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8592 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8593 | } |
| 8594 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8595 | // Check that setting the A-C header sends the proper header. |
| 8596 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8597 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8598 | |
| 8599 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8600 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8601 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8602 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8603 | HttpRequestHeaders headers; |
| 8604 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8605 | req->SetExtraRequestHeaders(headers); |
| 8606 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8607 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8608 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8609 | } |
| 8610 | |
| 8611 | // Check that default User-Agent header is sent. |
| 8612 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8613 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8614 | |
| 8615 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8616 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8617 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8618 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8619 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8620 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8621 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8622 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8623 | } |
| 8624 | |
| 8625 | // Check that if request overrides the User-Agent header, |
| 8626 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8627 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8628 | #if defined(OS_IOS) |
| 8629 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8630 | #else |
| 8631 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8632 | #endif |
| 8633 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8634 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8635 | |
| 8636 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8637 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8638 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8639 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8640 | HttpRequestHeaders headers; |
| 8641 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8642 | req->SetExtraRequestHeaders(headers); |
| 8643 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8644 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8645 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8646 | } |
| 8647 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8648 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8649 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8650 | // Accept-Charset headers. |
| 8651 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8652 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8653 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8654 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8655 | TestURLRequestContext context(true); |
| 8656 | context.set_network_delegate(&network_delegate); |
| 8657 | context.Init(); |
| 8658 | // We override the HttpUserAgentSettings after initialization because empty |
| 8659 | // entries get overridden by Init(). |
| 8660 | context.set_http_user_agent_settings(NULL); |
| 8661 | |
| 8662 | struct { |
| 8663 | const char* request; |
| 8664 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8665 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8666 | {"/echoheader?Accept-Charset", "None"}, |
| 8667 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8668 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 8669 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8670 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8671 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8672 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8673 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8674 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8675 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8676 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8677 | << " Request = \"" << tests[i].request << "\""; |
| 8678 | } |
| 8679 | } |
| 8680 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8681 | // Make sure that URLRequest passes on its priority updates to |
| 8682 | // newly-created jobs after the first one. |
| 8683 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8684 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8685 | |
| 8686 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8687 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8688 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8689 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8690 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8691 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8692 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8693 | req.get(), &default_network_delegate_, |
| 8694 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8695 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8696 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8697 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8698 | req->SetPriority(LOW); |
| 8699 | req->Start(); |
| 8700 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8701 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8702 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8703 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8704 | req.get(), &default_network_delegate_, &job_priority)); |
| 8705 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8706 | |
| 8707 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8708 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8709 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8710 | } |
| 8711 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8712 | // Check that creating a network request while entering/exiting suspend mode |
| 8713 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8714 | // does not return an HttpTransaction. |
| 8715 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 8716 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8717 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8718 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8719 | network_layer->OnSuspend(); |
| 8720 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8721 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 8722 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 8723 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8724 | |
| 8725 | TestURLRequestContext context(true); |
| 8726 | context.set_http_transaction_factory(&http_cache); |
| 8727 | context.Init(); |
| 8728 | |
| 8729 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8730 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8731 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8732 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8733 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8734 | base::RunLoop().Run(); |
| 8735 | |
| 8736 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8737 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8738 | } |
| 8739 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8740 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8741 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8742 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 8743 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 8744 | public: |
| 8745 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 8746 | : network_session_(network_session) {} |
| 8747 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 8748 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8749 | |
| 8750 | // HttpTransactionFactory methods: |
| 8751 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8752 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8753 | return ERR_FAILED; |
| 8754 | } |
| 8755 | |
| 8756 | HttpCache* GetCache() override { return nullptr; } |
| 8757 | |
| 8758 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 8759 | |
| 8760 | private: |
| 8761 | HttpNetworkSession* network_session_; |
| 8762 | |
| 8763 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 8764 | }; |
| 8765 | |
| 8766 | } // namespace |
| 8767 | |
| 8768 | // Check that when a request that fails to create an HttpTransaction can be |
| 8769 | // cancelled while the failure notification is pending, and doesn't send two |
| 8770 | // failure notifications. |
| 8771 | // |
| 8772 | // This currently only happens when in suspend mode and there's no cache, but |
| 8773 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 8774 | // behaviors. |
| 8775 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 8776 | FailingHttpTransactionFactory http_transaction_factory( |
| 8777 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8778 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8779 | context.set_http_transaction_factory(&http_transaction_factory); |
| 8780 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8781 | context.Init(); |
| 8782 | |
| 8783 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8784 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8785 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8786 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8787 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 8788 | // try to create an HttpNetworkTransaction synchronously on start). |
| 8789 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8790 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8791 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8792 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8793 | // Run pending error task, if there is one. |
| 8794 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8795 | |
| 8796 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8797 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8798 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8799 | |
| 8800 | // NetworkDelegate should see the cancellation, but not the error. |
| 8801 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 8802 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8803 | } |
| 8804 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8805 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8806 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8807 | |
| 8808 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8809 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8810 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8811 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8812 | |
| 8813 | req->Start(); |
| 8814 | base::RunLoop().Run(); |
| 8815 | |
| 8816 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8817 | } |
| 8818 | |
| 8819 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8820 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8821 | |
| 8822 | // Populate the cache. |
| 8823 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8824 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8825 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8826 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8827 | req->Start(); |
| 8828 | base::RunLoop().Run(); |
| 8829 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8830 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8831 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8832 | EXPECT_FALSE(req->response_info().was_cached); |
| 8833 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8834 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8835 | DEFAULT_PRIORITY, &d, |
| 8836 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8837 | req->Start(); |
| 8838 | base::RunLoop().Run(); |
| 8839 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8840 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8841 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8842 | EXPECT_TRUE(req->response_info().was_cached); |
| 8843 | } |
| 8844 | |
| 8845 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8846 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8847 | |
| 8848 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8849 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8850 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8851 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 8852 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8853 | |
| 8854 | req->Start(); |
| 8855 | base::RunLoop().Run(); |
| 8856 | |
| 8857 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8858 | } |
| 8859 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8860 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8861 | // correctly in the absence of contention. |
| 8862 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 8863 | ASSERT_TRUE(http_test_server()->Start()); |
| 8864 | |
| 8865 | TestDelegate d; |
| 8866 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8867 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8868 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8869 | req->Start(); |
| 8870 | base::RunLoop().Run(); |
| 8871 | |
| 8872 | EXPECT_TRUE(req->status().is_success()); |
| 8873 | } |
| 8874 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8875 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 8876 | ASSERT_TRUE(http_test_server()->Start()); |
| 8877 | |
| 8878 | TestDelegate d; |
| 8879 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8880 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 8881 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8882 | req->Start(); |
| 8883 | base::RunLoop().Run(); |
| 8884 | |
| 8885 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 8886 | } |
| 8887 | |
| 8888 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 8889 | ASSERT_TRUE(http_test_server()->Start()); |
| 8890 | |
| 8891 | TestDelegate d; |
| 8892 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8893 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 8894 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8895 | req->Start(); |
| 8896 | base::RunLoop().Run(); |
| 8897 | |
| 8898 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 8899 | } |
| 8900 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8901 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 8902 | // the delegate isn't called back synchronously. |
| 8903 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 8904 | ASSERT_TRUE(http_test_server()->Start()); |
| 8905 | default_network_delegate_.set_before_start_transaction_fails(); |
| 8906 | |
| 8907 | TestDelegate d; |
| 8908 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8909 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 8910 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8911 | req->Start(); |
| 8912 | DCHECK(!d.response_completed()); |
| 8913 | base::RunLoop().Run(); |
| 8914 | DCHECK(d.response_completed()); |
| 8915 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 8916 | } |
| 8917 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8918 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 8919 | public: |
| 8920 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 8921 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 8922 | // tests into the factory tests. |
| 8923 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 8924 | } |
| 8925 | |
| 8926 | void SetUpFactory() override { |
| 8927 | interceptor_ = new MockURLRequestInterceptor(); |
| 8928 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8929 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8930 | } |
| 8931 | |
| 8932 | MockURLRequestInterceptor* interceptor() const { |
| 8933 | return interceptor_; |
| 8934 | } |
| 8935 | |
| 8936 | private: |
| 8937 | MockURLRequestInterceptor* interceptor_; |
| 8938 | }; |
| 8939 | |
| 8940 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8941 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 8942 | interceptor()->set_intercept_redirect(true); |
| 8943 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 8944 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 8945 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8946 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8947 | |
| 8948 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8949 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8950 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8951 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8952 | req->Start(); |
| 8953 | base::RunLoop().Run(); |
| 8954 | |
| 8955 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 8956 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8957 | int status = d.request_status(); |
| 8958 | EXPECT_EQ(OK, status); |
| 8959 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8960 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8961 | |
| 8962 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8963 | EXPECT_EQ(1, d.response_started_count()); |
| 8964 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8965 | |
| 8966 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8967 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8968 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 8969 | } |
| 8970 | |
| 8971 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8972 | NetworkDelegateNotificationOnErrorIntercept) { |
| 8973 | // Intercept that error and respond with an OK response. |
| 8974 | interceptor()->set_intercept_final_response(true); |
| 8975 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 8976 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 8977 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 8978 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8979 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8980 | |
| 8981 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8982 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8983 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8984 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8985 | req->set_method("GET"); |
| 8986 | req->Start(); |
| 8987 | base::RunLoop().Run(); |
| 8988 | |
| 8989 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 8990 | |
| 8991 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8992 | int status = d.request_status(); |
| 8993 | EXPECT_EQ(OK, status); |
| 8994 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8995 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8996 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8997 | EXPECT_EQ(1, d.response_started_count()); |
| 8998 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8999 | |
| 9000 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9001 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9002 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9003 | } |
| 9004 | |
| 9005 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9006 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9007 | // Intercept that error and respond with an OK response. |
| 9008 | interceptor()->set_intercept_final_response(true); |
| 9009 | |
| 9010 | // Intercept with a real URLRequestHttpJob. |
| 9011 | interceptor()->set_use_url_request_http_job(true); |
| 9012 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9013 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9014 | |
| 9015 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9016 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9017 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9018 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9019 | req->set_method("GET"); |
| 9020 | req->Start(); |
| 9021 | base::RunLoop().Run(); |
| 9022 | |
| 9023 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9024 | |
| 9025 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9026 | int status = d.request_status(); |
| 9027 | EXPECT_EQ(OK, status); |
| 9028 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9029 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9030 | EXPECT_EQ("hello", d.data_received()); |
| 9031 | EXPECT_EQ(1, d.response_started_count()); |
| 9032 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9033 | |
| 9034 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9035 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9036 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9037 | } |
| 9038 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9039 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9040 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9041 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9042 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9043 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9044 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9045 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9046 | origin_server_->AddDefaultHandlers( |
| 9047 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9048 | } else { |
| 9049 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9050 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9051 | ASSERT_TRUE(origin_server_->Start()); |
| 9052 | } |
| 9053 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9054 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9055 | net::EmbeddedTestServer::Type dest_type) { |
| 9056 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9057 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9058 | origin_server_->AddDefaultHandlers( |
| 9059 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9060 | } else { |
| 9061 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9062 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9063 | ASSERT_TRUE(origin_server_->Start()); |
| 9064 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9065 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9066 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9067 | destination_server_->AddDefaultHandlers( |
| 9068 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9069 | } else { |
| 9070 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9071 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9072 | ASSERT_TRUE(destination_server_->Start()); |
| 9073 | } |
| 9074 | |
| 9075 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9076 | const GURL& referrer, |
| 9077 | const GURL& expected) { |
| 9078 | // Create and execute the request: we'll only have a |destination_server_| |
| 9079 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9080 | // |origin_server_| for both endpoints. |
| 9081 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9082 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9083 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9084 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9085 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9086 | |
| 9087 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9088 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9089 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9090 | req->set_referrer_policy(policy); |
| 9091 | req->SetReferrer(referrer.spec()); |
| 9092 | req->Start(); |
| 9093 | base::RunLoop().Run(); |
| 9094 | |
| 9095 | EXPECT_EQ(1, d.response_started_count()); |
| 9096 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9097 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9098 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9099 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9100 | |
| 9101 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9102 | if (expected.is_empty()) |
| 9103 | EXPECT_EQ("None", d.data_received()); |
| 9104 | else |
| 9105 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9106 | } |
| 9107 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9108 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9109 | |
| 9110 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9111 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9112 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9113 | }; |
| 9114 | |
| 9115 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9116 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9117 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9118 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9119 | VerifyReferrerAfterRedirect( |
| 9120 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9121 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9122 | |
| 9123 | VerifyReferrerAfterRedirect( |
| 9124 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9125 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9126 | |
| 9127 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9128 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9129 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9130 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9131 | referrer); |
| 9132 | |
| 9133 | // The original referrer set on the request is expected to obey the referrer |
| 9134 | // policy and already be stripped to the origin; thus this test case just |
| 9135 | // checks that this policy doesn't cause the referrer to change when following |
| 9136 | // a redirect. |
| 9137 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9138 | referrer.GetOrigin()); |
| 9139 | |
| 9140 | VerifyReferrerAfterRedirect( |
| 9141 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9142 | referrer); |
| 9143 | |
| 9144 | // The original referrer set on the request is expected to obey the referrer |
| 9145 | // policy and already be stripped to the origin; thus this test case just |
| 9146 | // checks that this policy doesn't cause the referrer to change when following |
| 9147 | // a redirect. |
| 9148 | VerifyReferrerAfterRedirect( |
| 9149 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9150 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9151 | |
| 9152 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9153 | } |
| 9154 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9155 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9156 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9157 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9158 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9159 | |
| 9160 | VerifyReferrerAfterRedirect( |
| 9161 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9162 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9163 | |
| 9164 | VerifyReferrerAfterRedirect( |
| 9165 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9166 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9167 | |
| 9168 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9169 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9170 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9171 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9172 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9173 | referrer); |
| 9174 | |
| 9175 | // The original referrer set on the request is expected to obey the referrer |
| 9176 | // policy and already be stripped to the origin; thus this test case just |
| 9177 | // checks that this policy doesn't cause the referrer to change when following |
| 9178 | // a redirect. |
| 9179 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9180 | referrer.GetOrigin()); |
| 9181 | |
| 9182 | VerifyReferrerAfterRedirect( |
| 9183 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9184 | |
| 9185 | // The original referrer set on the request is expected to obey the referrer |
| 9186 | // policy and already be stripped to the origin; thus this test case just |
| 9187 | // checks that this policy doesn't cause the referrer to change when following |
| 9188 | // a redirect. |
| 9189 | VerifyReferrerAfterRedirect( |
| 9190 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9191 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9192 | |
| 9193 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9197 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9198 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9199 | |
| 9200 | VerifyReferrerAfterRedirect( |
| 9201 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9202 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9203 | |
| 9204 | VerifyReferrerAfterRedirect( |
| 9205 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9206 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9207 | |
| 9208 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9209 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9210 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9211 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9212 | referrer); |
| 9213 | |
| 9214 | // The original referrer set on the request is expected to obey the referrer |
| 9215 | // policy and already be stripped to the origin; thus this test case just |
| 9216 | // checks that this policy doesn't cause the referrer to change when following |
| 9217 | // a redirect. |
| 9218 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9219 | referrer.GetOrigin()); |
| 9220 | |
| 9221 | VerifyReferrerAfterRedirect( |
| 9222 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, 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( |
| 9230 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9231 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9232 | |
| 9233 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9234 | } |
| 9235 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9236 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9237 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9238 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9239 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9240 | |
| 9241 | VerifyReferrerAfterRedirect( |
| 9242 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9243 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9244 | |
| 9245 | VerifyReferrerAfterRedirect( |
| 9246 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9247 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9248 | |
| 9249 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9250 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9251 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9252 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9253 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9254 | referrer); |
| 9255 | |
| 9256 | // The original referrer set on the request is expected to obey the referrer |
| 9257 | // policy and already be stripped to the origin; thus this test case just |
| 9258 | // checks that this policy doesn't cause the referrer to change when following |
| 9259 | // a redirect. |
| 9260 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9261 | referrer.GetOrigin()); |
| 9262 | |
| 9263 | VerifyReferrerAfterRedirect( |
| 9264 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9265 | |
| 9266 | // The original referrer set on the request is expected to obey the referrer |
| 9267 | // policy and already be stripped to the origin; thus this test case just |
| 9268 | // checks that this policy doesn't cause the referrer to change when following |
| 9269 | // a redirect. |
| 9270 | VerifyReferrerAfterRedirect( |
| 9271 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9272 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9273 | |
| 9274 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9275 | } |
| 9276 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9277 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9278 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9279 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9280 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9281 | |
| 9282 | VerifyReferrerAfterRedirect( |
| 9283 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9284 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9285 | |
| 9286 | VerifyReferrerAfterRedirect( |
| 9287 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9288 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9289 | |
| 9290 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9291 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9292 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9293 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9294 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9295 | referrer); |
| 9296 | |
| 9297 | // The original referrer set on the request is expected to obey the referrer |
| 9298 | // policy and already be stripped to the origin; thus this test case just |
| 9299 | // checks that this policy doesn't cause the referrer to change when following |
| 9300 | // a redirect. |
| 9301 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9302 | referrer.GetOrigin()); |
| 9303 | |
| 9304 | VerifyReferrerAfterRedirect( |
| 9305 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9306 | |
| 9307 | // The original referrer set on the request is expected to obey the referrer |
| 9308 | // policy and already be stripped to the origin; thus this test case just |
| 9309 | // checks that this policy doesn't cause the referrer to change when following |
| 9310 | // a redirect. |
| 9311 | VerifyReferrerAfterRedirect( |
| 9312 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9313 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9314 | |
| 9315 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9316 | } |
| 9317 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9318 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9319 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9320 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9321 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9322 | |
| 9323 | VerifyReferrerAfterRedirect( |
| 9324 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9325 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9326 | |
| 9327 | VerifyReferrerAfterRedirect( |
| 9328 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9329 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9330 | |
| 9331 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9332 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9333 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9334 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9335 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9336 | referrer); |
| 9337 | |
| 9338 | // The original referrer set on the request is expected to obey the referrer |
| 9339 | // policy and already be stripped to the origin; thus this test case just |
| 9340 | // checks that this policy doesn't cause the referrer to change when following |
| 9341 | // a redirect. |
| 9342 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9343 | referrer.GetOrigin()); |
| 9344 | |
| 9345 | VerifyReferrerAfterRedirect( |
| 9346 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9347 | |
| 9348 | // The original referrer set on the request is expected to obey the referrer |
| 9349 | // policy and already be stripped to the origin, though it should be |
| 9350 | // subsequently cleared during the downgrading redirect. |
| 9351 | VerifyReferrerAfterRedirect( |
| 9352 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9353 | referrer.GetOrigin(), GURL()); |
| 9354 | |
| 9355 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9356 | } |
| 9357 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9358 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9359 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9360 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9361 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9362 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9363 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9364 | ~HTTPSRequestTest() override { |
| 9365 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9366 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9367 | |
| 9368 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9369 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9370 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9371 | }; |
| 9372 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9373 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9374 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9375 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9376 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9377 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9378 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9379 | TestDelegate d; |
| 9380 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9381 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9382 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9383 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9384 | r->Start(); |
| 9385 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9386 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9387 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9388 | |
| 9389 | EXPECT_EQ(1, d.response_started_count()); |
| 9390 | EXPECT_FALSE(d.received_data_before_response()); |
| 9391 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9392 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9393 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9394 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9395 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9396 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9397 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9398 | } |
| 9399 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9400 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9401 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9402 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9403 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9404 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9405 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9406 | |
| 9407 | bool err_allowed = true; |
| 9408 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9409 | TestDelegate d; |
| 9410 | { |
| 9411 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9412 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9413 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9414 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9416 | r->Start(); |
| 9417 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9418 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9419 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9420 | |
| 9421 | EXPECT_EQ(1, d.response_started_count()); |
| 9422 | EXPECT_FALSE(d.received_data_before_response()); |
| 9423 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9424 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9425 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9426 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9427 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9428 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9429 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9430 | } |
| 9431 | } |
| 9432 | } |
| 9433 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9434 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9435 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9436 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9437 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9438 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9439 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9440 | |
| 9441 | // Iterate from false to true, just so that we do the opposite of the |
| 9442 | // previous test in order to increase test coverage. |
| 9443 | bool err_allowed = false; |
| 9444 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9445 | TestDelegate d; |
| 9446 | { |
| 9447 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9448 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9449 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9450 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9451 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9452 | r->Start(); |
| 9453 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9454 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9455 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9456 | |
| 9457 | EXPECT_EQ(1, d.response_started_count()); |
| 9458 | EXPECT_FALSE(d.received_data_before_response()); |
| 9459 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9460 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9461 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9462 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9463 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9464 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9465 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9466 | } |
| 9467 | } |
| 9468 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9469 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9470 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9471 | // security state. (see http://crbug.com/550977). |
| 9472 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9473 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9474 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9475 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9476 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9477 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9478 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9479 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9480 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9481 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9482 | ASSERT_TRUE(test_server.Start()); |
| 9483 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9484 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9485 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9486 | // means that we have to use a MockHostResolver in order to direct |
| 9487 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9488 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9489 | |
| 9490 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9491 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9492 | TestURLRequestContext context(true); |
| 9493 | context.set_network_delegate(&network_delegate); |
| 9494 | context.set_host_resolver(&host_resolver); |
| 9495 | TransportSecurityState transport_security_state; |
| 9496 | context.set_transport_security_state(&transport_security_state); |
| 9497 | context.Init(); |
| 9498 | |
| 9499 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9500 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9501 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9502 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9503 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9504 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9505 | r->Start(); |
| 9506 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9507 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9508 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9509 | |
| 9510 | EXPECT_EQ(1, d.response_started_count()); |
| 9511 | EXPECT_FALSE(d.received_data_before_response()); |
| 9512 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9513 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9514 | } |
| 9515 | |
| 9516 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9517 | // TransportSecurityState. |
| 9518 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9519 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9520 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9521 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9522 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9523 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9524 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9525 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9526 | ASSERT_TRUE(test_server.Start()); |
| 9527 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9528 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9529 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9530 | // means that we have to use a MockHostResolver in order to direct |
| 9531 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9532 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9533 | |
| 9534 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9535 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9536 | TestURLRequestContext context(true); |
| 9537 | context.set_network_delegate(&network_delegate); |
| 9538 | context.set_host_resolver(&host_resolver); |
| 9539 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9540 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9541 | TransportSecurityState::STSState static_sts_state; |
| 9542 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9543 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9544 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9545 | context.set_transport_security_state(&transport_security_state); |
| 9546 | context.Init(); |
| 9547 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9548 | TransportSecurityState::STSState dynamic_sts_state; |
| 9549 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9550 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9551 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 9552 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9553 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9554 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9555 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9556 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9557 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9558 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9559 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9560 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9561 | r->Start(); |
| 9562 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9563 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9564 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9565 | |
| 9566 | EXPECT_EQ(1, d.response_started_count()); |
| 9567 | EXPECT_FALSE(d.received_data_before_response()); |
| 9568 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9569 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9570 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9571 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9572 | TransportSecurityState::STSState new_static_sts_state; |
| 9573 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9574 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9575 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 9576 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9577 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9578 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9579 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9580 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9581 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9582 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9583 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9584 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9585 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9586 | static_sts_state.include_subdomains); |
| 9587 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9588 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9589 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9590 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9591 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9592 | } |
| 9593 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9594 | // Make sure HSTS preserves a POST request's method and body. |
| 9595 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9596 | static const char kData[] = "hello world"; |
| 9597 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9598 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9599 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9600 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9601 | ASSERT_TRUE(test_server.Start()); |
| 9602 | |
| 9603 | |
| 9604 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9605 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9606 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9607 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9608 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9609 | |
| 9610 | // Force https for www.somewhere.com. |
| 9611 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9612 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9613 | bool include_subdomains = false; |
| 9614 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9615 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9616 | |
| 9617 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9618 | |
| 9619 | TestURLRequestContext context(true); |
| 9620 | context.set_host_resolver(&host_resolver); |
| 9621 | context.set_transport_security_state(&transport_security_state); |
| 9622 | context.set_network_delegate(&network_delegate); |
| 9623 | context.Init(); |
| 9624 | |
| 9625 | TestDelegate d; |
| 9626 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9627 | // cause a certificate error. Ignore the error. |
| 9628 | d.set_allow_certificate_errors(true); |
| 9629 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9630 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9631 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9632 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9633 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9634 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9635 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9636 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9637 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9638 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9639 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9640 | EXPECT_EQ("https", req->url().scheme()); |
| 9641 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9642 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9643 | |
| 9644 | LoadTimingInfo load_timing_info; |
| 9645 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9646 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9647 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9648 | } |
| 9649 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9650 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9651 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9652 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9653 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9654 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9655 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9656 | ASSERT_TRUE(test_server.Start()); |
| 9657 | |
| 9658 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9659 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9660 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9661 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9662 | MockHostResolver host_resolver; |
| 9663 | |
| 9664 | TransportSecurityState transport_security_state; |
| 9665 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9666 | bool include_subdomains = false; |
| 9667 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9668 | |
| 9669 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9670 | |
| 9671 | MockCertVerifier cert_verifier; |
| 9672 | cert_verifier.set_default_result(OK); |
| 9673 | |
| 9674 | TestURLRequestContext context(true); |
| 9675 | context.set_host_resolver(&host_resolver); |
| 9676 | context.set_transport_security_state(&transport_security_state); |
| 9677 | context.set_network_delegate(&network_delegate); |
| 9678 | context.set_cert_verifier(&cert_verifier); |
| 9679 | context.Init(); |
| 9680 | |
| 9681 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9682 | test_server.host_port_pair().port())); |
| 9683 | url::Replacements<char> replacements; |
| 9684 | const char kNewScheme[] = "https"; |
| 9685 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9686 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9687 | |
| 9688 | TestDelegate d; |
| 9689 | // Quit on redirect to allow response header inspection upon redirect. |
| 9690 | d.set_quit_on_redirect(true); |
| 9691 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9692 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9693 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9694 | // Set Origin header to simulate a cross-origin request. |
| 9695 | HttpRequestHeaders request_headers; |
| 9696 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9697 | req->SetExtraRequestHeaders(request_headers); |
| 9698 | |
| 9699 | req->Start(); |
| 9700 | base::RunLoop().Run(); |
| 9701 | |
| 9702 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9703 | |
| 9704 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9705 | std::string redirect_location; |
| 9706 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 9707 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9708 | |
| 9709 | std::string received_cors_header; |
| 9710 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 9711 | &received_cors_header)); |
| 9712 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9713 | } |
| 9714 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9715 | namespace { |
| 9716 | |
| 9717 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 9718 | public: |
| 9719 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 9720 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9721 | void OnCertificateRequested(URLRequest* request, |
| 9722 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9723 | on_certificate_requested_count_++; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 9724 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9725 | } |
| 9726 | int on_certificate_requested_count() { |
| 9727 | return on_certificate_requested_count_; |
| 9728 | } |
| 9729 | private: |
| 9730 | int on_certificate_requested_count_; |
| 9731 | }; |
| 9732 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9733 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 9734 | public: |
| 9735 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 9736 | : key_(std::move(key)) {} |
| 9737 | |
| 9738 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 9739 | int sign_count() const { return sign_count_; } |
| 9740 | |
| 9741 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 9742 | return key_->GetAlgorithmPreferences(); |
| 9743 | } |
| 9744 | void Sign(uint16_t algorithm, |
| 9745 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9746 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9747 | sign_count_++; |
| 9748 | if (fail_signing_) { |
| 9749 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9750 | FROM_HERE, base::BindOnce(std::move(callback), |
| 9751 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 9752 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9753 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9754 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9755 | } |
| 9756 | } |
| 9757 | |
| 9758 | private: |
| 9759 | ~TestSSLPrivateKey() override = default; |
| 9760 | |
| 9761 | scoped_refptr<SSLPrivateKey> key_; |
| 9762 | bool fail_signing_ = false; |
| 9763 | int sign_count_ = 0; |
| 9764 | }; |
| 9765 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9766 | } // namespace |
| 9767 | |
| 9768 | // TODO(davidben): Test the rest of the code. Specifically, |
| 9769 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9770 | // - Getting a certificate request in an SSL renegotiation sending the |
| 9771 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9772 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9773 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9774 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 9775 | ssl_config.client_cert_type = |
| 9776 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9777 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9778 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9779 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9780 | ASSERT_TRUE(test_server.Start()); |
| 9781 | |
| 9782 | SSLClientAuthTestDelegate d; |
| 9783 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9784 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9785 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9786 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9787 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9788 | r->Start(); |
| 9789 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9790 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9791 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9792 | |
| 9793 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9794 | EXPECT_FALSE(d.received_data_before_response()); |
| 9795 | EXPECT_EQ(0, d.bytes_received()); |
| 9796 | |
| 9797 | // Send no certificate. |
| 9798 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 9799 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 9800 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9801 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9802 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9803 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9804 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9805 | EXPECT_EQ(1, d.response_started_count()); |
| 9806 | EXPECT_FALSE(d.received_data_before_response()); |
| 9807 | EXPECT_NE(0, d.bytes_received()); |
| 9808 | } |
| 9809 | } |
| 9810 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9811 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 9812 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9813 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9814 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9815 | ASSERT_TRUE(identity); |
| 9816 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9817 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9818 | |
| 9819 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9820 | net::SSLServerConfig ssl_config; |
| 9821 | ssl_config.client_cert_type = |
| 9822 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9823 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9824 | test_server.AddDefaultHandlers( |
| 9825 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9826 | ASSERT_TRUE(test_server.Start()); |
| 9827 | |
| 9828 | { |
| 9829 | SSLClientAuthTestDelegate d; |
| 9830 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9831 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9832 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9833 | |
| 9834 | r->Start(); |
| 9835 | EXPECT_TRUE(r->is_pending()); |
| 9836 | |
| 9837 | base::RunLoop().Run(); |
| 9838 | |
| 9839 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9840 | EXPECT_FALSE(d.received_data_before_response()); |
| 9841 | EXPECT_EQ(0, d.bytes_received()); |
| 9842 | |
| 9843 | // Send a certificate. |
| 9844 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 9845 | |
| 9846 | base::RunLoop().Run(); |
| 9847 | |
| 9848 | EXPECT_EQ(OK, d.request_status()); |
| 9849 | EXPECT_EQ(1, d.response_started_count()); |
| 9850 | EXPECT_FALSE(d.received_data_before_response()); |
| 9851 | EXPECT_NE(0, d.bytes_received()); |
| 9852 | |
| 9853 | // The private key should have been used. |
| 9854 | EXPECT_EQ(1, private_key->sign_count()); |
| 9855 | } |
| 9856 | |
| 9857 | // Close all connections and clear the session cache to force a new handshake. |
| 9858 | default_context_.http_transaction_factory() |
| 9859 | ->GetSession() |
| 9860 | ->CloseAllConnections(); |
| 9861 | SSLClientSocket::ClearSessionCache(); |
| 9862 | |
| 9863 | // Connecting again should not call OnCertificateRequested. The identity is |
| 9864 | // taken from the client auth cache. |
| 9865 | { |
| 9866 | SSLClientAuthTestDelegate d; |
| 9867 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9868 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9869 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9870 | |
| 9871 | r->Start(); |
| 9872 | EXPECT_TRUE(r->is_pending()); |
| 9873 | |
| 9874 | base::RunLoop().Run(); |
| 9875 | |
| 9876 | EXPECT_EQ(OK, d.request_status()); |
| 9877 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 9878 | EXPECT_FALSE(d.received_data_before_response()); |
| 9879 | EXPECT_EQ(1, d.response_started_count()); |
| 9880 | EXPECT_FALSE(d.received_data_before_response()); |
| 9881 | EXPECT_NE(0, d.bytes_received()); |
| 9882 | |
| 9883 | // The private key should have been used. |
| 9884 | EXPECT_EQ(2, private_key->sign_count()); |
| 9885 | } |
| 9886 | } |
| 9887 | |
| 9888 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 9889 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 9890 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9891 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9892 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9893 | ASSERT_TRUE(identity); |
| 9894 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9895 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9896 | private_key->set_fail_signing(true); |
| 9897 | |
| 9898 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9899 | net::SSLServerConfig ssl_config; |
| 9900 | ssl_config.client_cert_type = |
| 9901 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9902 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9903 | test_server.AddDefaultHandlers( |
| 9904 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9905 | ASSERT_TRUE(test_server.Start()); |
| 9906 | |
| 9907 | { |
| 9908 | SSLClientAuthTestDelegate d; |
| 9909 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9910 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9911 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9912 | |
| 9913 | r->Start(); |
| 9914 | EXPECT_TRUE(r->is_pending()); |
| 9915 | base::RunLoop().Run(); |
| 9916 | |
| 9917 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9918 | EXPECT_FALSE(d.received_data_before_response()); |
| 9919 | EXPECT_EQ(0, d.bytes_received()); |
| 9920 | |
| 9921 | // Send a certificate. |
| 9922 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 9923 | base::RunLoop().Run(); |
| 9924 | |
| 9925 | // The private key cannot sign anything, so we report an error. |
| 9926 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 9927 | EXPECT_EQ(1, d.response_started_count()); |
| 9928 | EXPECT_FALSE(d.received_data_before_response()); |
| 9929 | EXPECT_EQ(0, d.bytes_received()); |
| 9930 | |
| 9931 | // The private key should have been used. |
| 9932 | EXPECT_EQ(1, private_key->sign_count()); |
| 9933 | } |
| 9934 | |
| 9935 | // Close all connections and clear the session cache to force a new handshake. |
| 9936 | default_context_.http_transaction_factory() |
| 9937 | ->GetSession() |
| 9938 | ->CloseAllConnections(); |
| 9939 | SSLClientSocket::ClearSessionCache(); |
| 9940 | |
| 9941 | // The bad identity should have been evicted from the cache, so connecting |
| 9942 | // again should call OnCertificateRequested again. |
| 9943 | { |
| 9944 | SSLClientAuthTestDelegate d; |
| 9945 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9946 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9947 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9948 | |
| 9949 | r->Start(); |
| 9950 | EXPECT_TRUE(r->is_pending()); |
| 9951 | |
| 9952 | base::RunLoop().Run(); |
| 9953 | |
| 9954 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9955 | EXPECT_FALSE(d.received_data_before_response()); |
| 9956 | EXPECT_EQ(0, d.bytes_received()); |
| 9957 | |
| 9958 | // There should have been no additional uses of the private key. |
| 9959 | EXPECT_EQ(1, private_key->sign_count()); |
| 9960 | } |
| 9961 | } |
| 9962 | |
| 9963 | // Test that cached private keys that fail to sign anything trigger a |
| 9964 | // retry. This is so we handle unplugged smartcards |
| 9965 | // gracefully. https://crbug.com/813022. |
| 9966 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 9967 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9968 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9969 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9970 | ASSERT_TRUE(identity); |
| 9971 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9972 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9973 | |
| 9974 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9975 | net::SSLServerConfig ssl_config; |
| 9976 | ssl_config.client_cert_type = |
| 9977 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9978 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9979 | test_server.AddDefaultHandlers( |
| 9980 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9981 | ASSERT_TRUE(test_server.Start()); |
| 9982 | |
| 9983 | // Connect with a client certificate to put it in the client auth cache. |
| 9984 | { |
| 9985 | SSLClientAuthTestDelegate d; |
| 9986 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9987 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9988 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9989 | |
| 9990 | r->Start(); |
| 9991 | EXPECT_TRUE(r->is_pending()); |
| 9992 | |
| 9993 | base::RunLoop().Run(); |
| 9994 | |
| 9995 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9996 | EXPECT_FALSE(d.received_data_before_response()); |
| 9997 | EXPECT_EQ(0, d.bytes_received()); |
| 9998 | |
| 9999 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10000 | base::RunLoop().Run(); |
| 10001 | |
| 10002 | EXPECT_EQ(OK, d.request_status()); |
| 10003 | EXPECT_EQ(1, d.response_started_count()); |
| 10004 | EXPECT_FALSE(d.received_data_before_response()); |
| 10005 | EXPECT_NE(0, d.bytes_received()); |
| 10006 | |
| 10007 | // The private key should have been used. |
| 10008 | EXPECT_EQ(1, private_key->sign_count()); |
| 10009 | } |
| 10010 | |
| 10011 | // Close all connections and clear the session cache to force a new handshake. |
| 10012 | default_context_.http_transaction_factory() |
| 10013 | ->GetSession() |
| 10014 | ->CloseAllConnections(); |
| 10015 | SSLClientSocket::ClearSessionCache(); |
| 10016 | |
| 10017 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10018 | // notice the failure, and then request a new identity via |
| 10019 | // OnCertificateRequested. |
| 10020 | private_key->set_fail_signing(true); |
| 10021 | |
| 10022 | { |
| 10023 | SSLClientAuthTestDelegate d; |
| 10024 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10025 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10026 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10027 | |
| 10028 | r->Start(); |
| 10029 | EXPECT_TRUE(r->is_pending()); |
| 10030 | |
| 10031 | base::RunLoop().Run(); |
| 10032 | |
| 10033 | // There was an additional signing call on the private key (the one which |
| 10034 | // failed). |
| 10035 | EXPECT_EQ(2, private_key->sign_count()); |
| 10036 | |
| 10037 | // That caused another OnCertificateRequested call. |
| 10038 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10039 | EXPECT_FALSE(d.received_data_before_response()); |
| 10040 | EXPECT_EQ(0, d.bytes_received()); |
| 10041 | } |
| 10042 | } |
| 10043 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10044 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10045 | // Test that we attempt a session resume when making two connections to the |
| 10046 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10047 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10048 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10049 | SpawnedTestServer test_server( |
| 10050 | SpawnedTestServer::TYPE_HTTPS, |
| 10051 | ssl_options, |
| 10052 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10053 | ASSERT_TRUE(test_server.Start()); |
| 10054 | |
| 10055 | SSLClientSocket::ClearSessionCache(); |
| 10056 | |
| 10057 | { |
| 10058 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10059 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10060 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10061 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10063 | r->Start(); |
| 10064 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10065 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10066 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10067 | |
| 10068 | EXPECT_EQ(1, d.response_started_count()); |
| 10069 | } |
| 10070 | |
| 10071 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10072 | CloseAllConnections(); |
| 10073 | |
| 10074 | { |
| 10075 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10076 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10077 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10078 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10079 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10080 | r->Start(); |
| 10081 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10082 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10083 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10084 | |
| 10085 | // The response will look like; |
| 10086 | // insert abc |
| 10087 | // lookup abc |
| 10088 | // insert xyz |
| 10089 | // |
| 10090 | // With a newline at the end which makes the split think that there are |
| 10091 | // four lines. |
| 10092 | |
| 10093 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10094 | std::vector<std::string> lines = base::SplitString( |
| 10095 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10096 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 10097 | |
| 10098 | std::string session_id; |
| 10099 | |
| 10100 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10101 | std::vector<std::string> parts = base::SplitString( |
| 10102 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10103 | ASSERT_EQ(2u, parts.size()); |
| 10104 | if (i == 0) { |
| 10105 | EXPECT_EQ("insert", parts[0]); |
| 10106 | session_id = parts[1]; |
| 10107 | } else { |
| 10108 | EXPECT_EQ("lookup", parts[0]); |
| 10109 | EXPECT_EQ(session_id, parts[1]); |
| 10110 | } |
| 10111 | } |
| 10112 | } |
| 10113 | } |
| 10114 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10115 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 10116 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 10117 | // that exactly two different sessions were inserted, with no lookups etc. |
| 10118 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10119 | std::vector<std::string> lines = base::SplitString( |
| 10120 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10121 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 10122 | |
| 10123 | std::string session_id; |
| 10124 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10125 | std::vector<std::string> parts = base::SplitString( |
| 10126 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10127 | ASSERT_EQ(2u, parts.size()); |
| 10128 | EXPECT_EQ("insert", parts[0]); |
| 10129 | if (i == 0) { |
| 10130 | session_id = parts[1]; |
| 10131 | } else { |
| 10132 | EXPECT_NE(session_id, parts[1]); |
| 10133 | } |
| 10134 | } |
| 10135 | } |
| 10136 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10137 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10138 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10139 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10140 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10141 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10142 | SpawnedTestServer test_server( |
| 10143 | SpawnedTestServer::TYPE_HTTPS, |
| 10144 | ssl_options, |
| 10145 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10146 | ASSERT_TRUE(test_server.Start()); |
| 10147 | |
| 10148 | SSLClientSocket::ClearSessionCache(); |
| 10149 | |
| 10150 | { |
| 10151 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10152 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10153 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10154 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10156 | r->Start(); |
| 10157 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10158 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10159 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10160 | |
| 10161 | EXPECT_EQ(1, d.response_started_count()); |
| 10162 | } |
| 10163 | |
| 10164 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10165 | HttpNetworkSession::Context session_context; |
| 10166 | session_context.host_resolver = default_context_.host_resolver(); |
| 10167 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10168 | session_context.transport_security_state = |
| 10169 | default_context_.transport_security_state(); |
| 10170 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10171 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10172 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10173 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10174 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10175 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10176 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10177 | session_context.http_server_properties = |
| 10178 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10179 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10180 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10181 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10182 | std::unique_ptr<HttpCache> cache( |
| 10183 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10184 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10185 | |
| 10186 | default_context_.set_http_transaction_factory(cache.get()); |
| 10187 | |
| 10188 | { |
| 10189 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10190 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10191 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10192 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10193 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10194 | r->Start(); |
| 10195 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10196 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10197 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10198 | |
| 10199 | // The response will look like; |
| 10200 | // insert abc |
| 10201 | // insert xyz |
| 10202 | // |
| 10203 | // With a newline at the end which makes the split think that there are |
| 10204 | // three lines. |
| 10205 | |
| 10206 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10207 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10208 | } |
| 10209 | } |
| 10210 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10211 | class HTTPSFallbackTest : public testing::Test { |
| 10212 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10213 | HTTPSFallbackTest() : context_(true) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10214 | ssl_config_service_ = new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 10215 | false /* online revocation checking */, |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10216 | false /* require rev. checking for local anchors */, |
| 10217 | false /* token binding enabled */); |
| 10218 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10219 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10220 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10221 | |
| 10222 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10223 | TestSSLConfigService* ssl_config_service() { |
| 10224 | return ssl_config_service_.get(); |
| 10225 | } |
| 10226 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10227 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10228 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10229 | context_.Init(); |
| 10230 | delegate_.set_allow_certificate_errors(true); |
| 10231 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10232 | SpawnedTestServer test_server( |
| 10233 | SpawnedTestServer::TYPE_HTTPS, |
| 10234 | ssl_options, |
| 10235 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10236 | ASSERT_TRUE(test_server.Start()); |
| 10237 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10238 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10239 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10240 | request_->Start(); |
| 10241 | |
| 10242 | base::RunLoop().Run(); |
| 10243 | } |
| 10244 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10245 | void ExpectConnection(int version) { |
| 10246 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10247 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10248 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10249 | request_->ssl_info().connection_status)); |
| 10250 | } |
| 10251 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10252 | void ExpectFailure(int error) { |
| 10253 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10254 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10255 | } |
| 10256 | |
| 10257 | private: |
| 10258 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10259 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10260 | std::unique_ptr<URLRequest> request_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10261 | scoped_refptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10262 | }; |
| 10263 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10264 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10265 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10266 | SpawnedTestServer::SSLOptions ssl_options( |
| 10267 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10268 | ssl_options.tls_intolerant = |
| 10269 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10270 | |
| 10271 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10272 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10273 | } |
| 10274 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10275 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10276 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10277 | SpawnedTestServer::SSLOptions ssl_options( |
| 10278 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10279 | ssl_options.tls_intolerant = |
| 10280 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10281 | |
| 10282 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10283 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10284 | } |
| 10285 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10286 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10287 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10288 | SpawnedTestServer::SSLOptions ssl_options( |
| 10289 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10290 | ssl_options.tls_intolerant = |
| 10291 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10292 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10293 | |
| 10294 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10295 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10296 | } |
| 10297 | |
| 10298 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10299 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10300 | SpawnedTestServer::SSLOptions ssl_options( |
| 10301 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10302 | ssl_options.tls_intolerant = |
| 10303 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10304 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10305 | |
| 10306 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10307 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10308 | } |
| 10309 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10310 | class HTTPSSessionTest : public testing::Test { |
| 10311 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10312 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10313 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10314 | |
| 10315 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10316 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10317 | default_context_.Init(); |
| 10318 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10319 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10320 | |
| 10321 | protected: |
| 10322 | MockCertVerifier cert_verifier_; |
| 10323 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10324 | TestURLRequestContext default_context_; |
| 10325 | }; |
| 10326 | |
| 10327 | // Tests that session resumption is not attempted if an invalid certificate |
| 10328 | // is presented. |
| 10329 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10330 | SpawnedTestServer::SSLOptions ssl_options; |
| 10331 | ssl_options.record_resume = true; |
| 10332 | SpawnedTestServer test_server( |
| 10333 | SpawnedTestServer::TYPE_HTTPS, |
| 10334 | ssl_options, |
| 10335 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10336 | ASSERT_TRUE(test_server.Start()); |
| 10337 | |
| 10338 | SSLClientSocket::ClearSessionCache(); |
| 10339 | |
| 10340 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10341 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10342 | { |
| 10343 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10344 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10345 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10346 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10347 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10348 | r->Start(); |
| 10349 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10350 | |
| 10351 | base::RunLoop().Run(); |
| 10352 | |
| 10353 | EXPECT_EQ(1, d.response_started_count()); |
| 10354 | } |
| 10355 | |
| 10356 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10357 | CloseAllConnections(); |
| 10358 | |
| 10359 | // Now change the certificate to be acceptable (so that the response is |
| 10360 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10361 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10362 | { |
| 10363 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10364 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10365 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10366 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10367 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10368 | r->Start(); |
| 10369 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10370 | |
| 10371 | base::RunLoop().Run(); |
| 10372 | |
| 10373 | // The response will look like; |
| 10374 | // insert abc |
| 10375 | // insert xyz |
| 10376 | // |
| 10377 | // With a newline at the end which makes the split think that there are |
| 10378 | // three lines. |
| 10379 | // |
| 10380 | // If a session was presented (eg: a bug), then the response would look |
| 10381 | // like; |
| 10382 | // insert abc |
| 10383 | // lookup abc |
| 10384 | // insert xyz |
| 10385 | |
| 10386 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10387 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10388 | } |
| 10389 | } |
| 10390 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10391 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10392 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10393 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10394 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10395 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10396 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10397 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10398 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10399 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10400 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10401 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10402 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10403 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10404 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10405 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10406 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10407 | // This is the policy OID contained in the certificates that testserver |
| 10408 | // generates. |
| 10409 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10410 | |
| 10411 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10412 | public: |
| 10413 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10414 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10415 | ev_test_policy_( |
| 10416 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10417 | kOCSPTestCertFingerprint, |
| 10418 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10419 | } |
| 10420 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10421 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10422 | context_.SetCTPolicyEnforcer( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10423 | std::make_unique<AllowAnyCertCTPolicyEnforcer>()); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10424 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10425 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10426 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10427 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10428 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10429 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10430 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10431 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10432 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10433 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10434 | #endif |
| 10435 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10436 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10437 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10438 | #endif |
| 10439 | } |
| 10440 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10441 | void DoConnectionWithDelegate( |
| 10442 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10443 | TestDelegate* delegate, |
| 10444 | SSLInfo* out_ssl_info) { |
| 10445 | // Always overwrite |out_ssl_info|. |
| 10446 | out_ssl_info->Reset(); |
| 10447 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10448 | SpawnedTestServer test_server( |
| 10449 | SpawnedTestServer::TYPE_HTTPS, |
| 10450 | ssl_options, |
| 10451 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10452 | ASSERT_TRUE(test_server.Start()); |
| 10453 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10454 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10455 | std::unique_ptr<URLRequest> r( |
| 10456 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10457 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10458 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10459 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10460 | base::RunLoop().Run(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10461 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10462 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10463 | *out_ssl_info = r->ssl_info(); |
| 10464 | } |
| 10465 | |
| 10466 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10467 | CertStatus* out_cert_status) { |
| 10468 | // Always overwrite |out_cert_status|. |
| 10469 | *out_cert_status = 0; |
| 10470 | |
| 10471 | TestDelegate d; |
| 10472 | SSLInfo ssl_info; |
| 10473 | ASSERT_NO_FATAL_FAILURE( |
| 10474 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10475 | |
| 10476 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10477 | } |
| 10478 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10479 | ~HTTPSOCSPTest() override { |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10480 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10481 | ShutdownGlobalCertNetFetcher(); |
| 10482 | #endif |
| 10483 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10484 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 10485 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10486 | #endif |
| 10487 | } |
| 10488 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10489 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10490 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 10491 | public: |
| 10492 | AllowAnyCertCTPolicyEnforcer() = default; |
| 10493 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 10494 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 10495 | ct::CTPolicyCompliance CheckCompliance( |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10496 | X509Certificate* cert, |
| 10497 | const SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10498 | const NetLogWithSource& net_log) override { |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 10499 | return ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS; |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10500 | } |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10501 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10502 | // SetupContext configures the URLRequestContext that will be used for making |
| 10503 | // connetions to testserver. This can be overridden in test subclasses for |
| 10504 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10505 | virtual void SetupContext() { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 10506 | context_.set_ssl_config_service( |
| 10507 | new TestSSLConfigService(true /* online revocation checking */, |
| 10508 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 10509 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 10510 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10511 | } |
| 10512 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10513 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10514 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10515 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10516 | }; |
| 10517 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10518 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10519 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10520 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10521 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10522 | // TODO(eroman): Should this also be the return value for |
| 10523 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10524 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10525 | #else |
| 10526 | return 0; |
| 10527 | #endif |
| 10528 | } |
| 10529 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10530 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10531 | // operating system supports revocation checking and can distinguish between |
| 10532 | // situations where a given certificate lacks any revocation information (eg: |
| 10533 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10534 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10535 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10536 | // skipped. |
| 10537 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10538 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10539 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10540 | return true; |
| 10541 | #else |
| 10542 | return false; |
| 10543 | #endif |
| 10544 | } |
| 10545 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10546 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10547 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10548 | // several tests are effected because our testing EV certificate won't be |
| 10549 | // recognised as EV. |
| 10550 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10551 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10552 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10553 | #else |
| 10554 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10555 | #endif |
| 10556 | } |
| 10557 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10558 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10559 | // check failure as a result of checking a test EV cert, which will not |
| 10560 | // actually trigger an online revocation check on some platforms. |
| 10561 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10562 | if (SystemUsesChromiumEVMetadata()) { |
| 10563 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10564 | } else { |
| 10565 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10566 | // be enabled, and thus there will not be a revocation check to fail. |
| 10567 | return 0u; |
| 10568 | } |
| 10569 | } |
| 10570 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10571 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 10572 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10573 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 10574 | return false; |
| 10575 | #else |
| 10576 | return true; |
| 10577 | #endif |
| 10578 | } |
| 10579 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10580 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10581 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 10582 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10583 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10584 | #else |
| 10585 | return false; |
| 10586 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10587 | } |
| 10588 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10589 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10590 | if (!SystemSupportsOCSP()) { |
| 10591 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10592 | return; |
| 10593 | } |
| 10594 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10595 | SpawnedTestServer::SSLOptions ssl_options( |
| 10596 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10597 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10598 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10599 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10600 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10601 | |
| 10602 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10603 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10604 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10605 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10606 | |
| 10607 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10608 | } |
| 10609 | |
| 10610 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10611 | if (!SystemSupportsOCSP()) { |
| 10612 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10613 | return; |
| 10614 | } |
| 10615 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10616 | SpawnedTestServer::SSLOptions ssl_options( |
| 10617 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10618 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10619 | |
| 10620 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10621 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10622 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10623 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10624 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10625 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10626 | } |
| 10627 | |
| 10628 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10629 | if (!SystemSupportsOCSP()) { |
| 10630 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10631 | return; |
| 10632 | } |
| 10633 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10634 | SpawnedTestServer::SSLOptions ssl_options( |
| 10635 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10636 | ssl_options.ocsp_status = |
| 10637 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10638 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10639 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10640 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10641 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10642 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10643 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 10644 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 10645 | // doesn't consider this a candidate for soft-fail (only considers |
| 10646 | // network-level failures as skippable). |
| 10647 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10648 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10649 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10650 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10651 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10652 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10653 | |
| 10654 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10655 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10656 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10657 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10658 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10659 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10660 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10661 | LOG(WARNING) |
| 10662 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10663 | return; |
| 10664 | } |
| 10665 | |
| 10666 | SpawnedTestServer::SSLOptions ssl_options( |
| 10667 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10668 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10669 | ssl_options.staple_ocsp_response = true; |
| 10670 | ssl_options.ocsp_server_unavailable = true; |
| 10671 | |
| 10672 | CertStatus cert_status; |
| 10673 | DoConnection(ssl_options, &cert_status); |
| 10674 | |
| 10675 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10676 | |
| 10677 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10678 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10679 | |
| 10680 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10681 | } |
| 10682 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10683 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 10684 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10685 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 10686 | #else |
| 10687 | #define MAYBE_RevokedStapled RevokedStapled |
| 10688 | #endif |
| 10689 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10690 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10691 | LOG(WARNING) |
| 10692 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10693 | return; |
| 10694 | } |
| 10695 | |
| 10696 | SpawnedTestServer::SSLOptions ssl_options( |
| 10697 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10698 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10699 | ssl_options.staple_ocsp_response = true; |
| 10700 | ssl_options.ocsp_server_unavailable = true; |
| 10701 | |
| 10702 | CertStatus cert_status; |
| 10703 | DoConnection(ssl_options, &cert_status); |
| 10704 | |
| 10705 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10706 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10707 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10708 | } |
| 10709 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10710 | TEST_F(HTTPSOCSPTest, ExpectStapleReportSentOnMissing) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10711 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10712 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10713 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10714 | https_test_server.SetSSLConfig( |
| 10715 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10716 | https_test_server.ServeFilesFromSourceDirectory( |
| 10717 | base::FilePath(kTestFilePath)); |
| 10718 | ASSERT_TRUE(https_test_server.Start()); |
| 10719 | |
| 10720 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10721 | // but not provide any OCSP information. |
| 10722 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10723 | ASSERT_TRUE(cert); |
| 10724 | MockCertVerifier cert_verifier; |
| 10725 | CertVerifyResult verify_result; |
| 10726 | verify_result.verified_cert = cert; |
| 10727 | verify_result.is_issued_by_known_root = true; |
| 10728 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10729 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10730 | |
| 10731 | // Catch the Expect-Staple report. |
| 10732 | TransportSecurityState transport_security_state; |
| 10733 | MockCertificateReportSender mock_report_sender; |
| 10734 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10735 | |
| 10736 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10737 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10738 | MockHostResolver host_resolver; |
| 10739 | TestNetworkDelegate network_delegate; |
| 10740 | TestURLRequestContext context(true); |
| 10741 | context.set_host_resolver(&host_resolver); |
| 10742 | context.set_transport_security_state(&transport_security_state); |
| 10743 | context.set_network_delegate(&network_delegate); |
| 10744 | context.set_cert_verifier(&cert_verifier); |
| 10745 | context.Init(); |
| 10746 | |
| 10747 | // Now send a request to trigger the violation. |
| 10748 | TestDelegate d; |
| 10749 | GURL url = https_test_server.GetURL("/"); |
| 10750 | GURL::Replacements replace_host; |
| 10751 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10752 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10753 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10754 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10755 | violating_request->Start(); |
| 10756 | base::RunLoop().Run(); |
| 10757 | |
| 10758 | // Confirm a report was sent. |
| 10759 | EXPECT_FALSE(mock_report_sender.latest_report().empty()); |
| 10760 | EXPECT_EQ(GURL(kExpectStapleReportURI), |
| 10761 | mock_report_sender.latest_report_uri()); |
| 10762 | } |
| 10763 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10764 | // Tests that Expect-Staple reports are not sent for connections on which there |
| 10765 | // is a certificate error. |
| 10766 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnMissingWithCertError) { |
| 10767 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10768 | https_test_server.SetSSLConfig( |
| 10769 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10770 | https_test_server.ServeFilesFromSourceDirectory( |
| 10771 | base::FilePath(kTestFilePath)); |
| 10772 | ASSERT_TRUE(https_test_server.Start()); |
| 10773 | |
| 10774 | // Set up a MockCertVerifier to report an error for the certificate |
| 10775 | // and indicate that there was no stapled OCSP response. |
| 10776 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10777 | ASSERT_TRUE(cert); |
| 10778 | MockCertVerifier cert_verifier; |
| 10779 | CertVerifyResult verify_result; |
| 10780 | verify_result.cert_status = CERT_STATUS_DATE_INVALID; |
| 10781 | verify_result.verified_cert = cert; |
| 10782 | verify_result.is_issued_by_known_root = true; |
| 10783 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 10784 | cert_verifier.AddResultForCert(cert.get(), verify_result, |
| 10785 | ERR_CERT_DATE_INVALID); |
| 10786 | |
| 10787 | // Set up a mock report sender so that the test can check that an |
| 10788 | // Expect-Staple report is not sent. |
| 10789 | TransportSecurityState transport_security_state; |
| 10790 | MockCertificateReportSender mock_report_sender; |
| 10791 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10792 | |
| 10793 | TestNetworkDelegate network_delegate; |
| 10794 | TestURLRequestContext context(true); |
| 10795 | |
| 10796 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10797 | MockHostResolver host_resolver; |
| 10798 | context.set_host_resolver(&host_resolver); |
| 10799 | |
| 10800 | context.set_transport_security_state(&transport_security_state); |
| 10801 | context.set_network_delegate(&network_delegate); |
| 10802 | context.set_cert_verifier(&cert_verifier); |
| 10803 | context.Init(); |
| 10804 | |
| 10805 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10806 | // |verify_result| used with the |cert_verifier| will indicate a certificate |
| 10807 | // error, an Expect-Staple report should not be sent. |
| 10808 | TestDelegate d; |
| 10809 | GURL url = https_test_server.GetURL("/"); |
| 10810 | GURL::Replacements replace_host; |
| 10811 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10812 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10813 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 10814 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10815 | violating_request->Start(); |
| 10816 | base::RunLoop().Run(); |
| 10817 | |
| 10818 | // Confirm a report was not sent. |
| 10819 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10820 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10821 | } |
| 10822 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10823 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnValid) { |
| 10824 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10825 | https_test_server.SetSSLConfig( |
| 10826 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10827 | https_test_server.ServeFilesFromSourceDirectory( |
| 10828 | base::FilePath(kTestFilePath)); |
| 10829 | ASSERT_TRUE(https_test_server.Start()); |
| 10830 | |
| 10831 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10832 | // and provide GOOD revocation status. |
| 10833 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10834 | ASSERT_TRUE(cert); |
| 10835 | MockCertVerifier cert_verifier; |
| 10836 | CertVerifyResult verify_result; |
| 10837 | verify_result.verified_cert = cert; |
| 10838 | verify_result.is_issued_by_known_root = true; |
| 10839 | verify_result.ocsp_result.response_status = OCSPVerifyResult::PROVIDED; |
| 10840 | verify_result.ocsp_result.revocation_status = OCSPRevocationStatus::GOOD; |
| 10841 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10842 | |
| 10843 | // Catch the Expect-Staple report. |
| 10844 | TransportSecurityState transport_security_state; |
| 10845 | MockCertificateReportSender mock_report_sender; |
| 10846 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10847 | |
| 10848 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 10849 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 10850 | MockHostResolver host_resolver; |
| 10851 | TestNetworkDelegate network_delegate; |
| 10852 | TestURLRequestContext context(true); |
| 10853 | context.set_host_resolver(&host_resolver); |
| 10854 | context.set_transport_security_state(&transport_security_state); |
| 10855 | context.set_network_delegate(&network_delegate); |
| 10856 | context.set_cert_verifier(&cert_verifier); |
| 10857 | context.Init(); |
| 10858 | |
| 10859 | // This request should not not trigger an Expect-Staple violation. |
| 10860 | TestDelegate d; |
| 10861 | GURL url = https_test_server.GetURL("/"); |
| 10862 | GURL::Replacements replace_host; |
| 10863 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10864 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10865 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10866 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 10867 | ok_request->Start(); |
| 10868 | base::RunLoop().Run(); |
| 10869 | |
| 10870 | // Check that no report was sent. |
| 10871 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10872 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10873 | } |
| 10874 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10875 | // Tests that an Expect-Staple report is not sent when OCSP details are not |
| 10876 | // checked on the connection. |
| 10877 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnNotChecked) { |
| 10878 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10879 | https_test_server.SetSSLConfig( |
| 10880 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 10881 | https_test_server.ServeFilesFromSourceDirectory( |
| 10882 | base::FilePath(kTestFilePath)); |
| 10883 | ASSERT_TRUE(https_test_server.Start()); |
| 10884 | |
| 10885 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 10886 | // and set |ocsp_result| to indicate that OCSP stapling details were not |
| 10887 | // checked on the connection. |
| 10888 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 10889 | ASSERT_TRUE(cert); |
| 10890 | MockCertVerifier cert_verifier; |
| 10891 | CertVerifyResult verify_result; |
| 10892 | verify_result.verified_cert = cert; |
| 10893 | verify_result.is_issued_by_known_root = true; |
| 10894 | verify_result.ocsp_result.response_status = OCSPVerifyResult::NOT_CHECKED; |
| 10895 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 10896 | |
| 10897 | // Set up a mock report sender so that the test can check that an |
| 10898 | // Expect-Staple report is not sent. |
| 10899 | TransportSecurityState transport_security_state; |
| 10900 | MockCertificateReportSender mock_report_sender; |
| 10901 | transport_security_state.SetReportSender(&mock_report_sender); |
| 10902 | |
| 10903 | TestNetworkDelegate network_delegate; |
| 10904 | TestURLRequestContext context(true); |
| 10905 | |
| 10906 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 10907 | MockHostResolver host_resolver; |
| 10908 | context.set_host_resolver(&host_resolver); |
| 10909 | |
| 10910 | context.set_transport_security_state(&transport_security_state); |
| 10911 | context.set_network_delegate(&network_delegate); |
| 10912 | context.set_cert_verifier(&cert_verifier); |
| 10913 | context.Init(); |
| 10914 | |
| 10915 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 10916 | // |verify_result| used with the |cert_verifier| will indicate that OCSP |
| 10917 | // stapling details were not checked on the connection, an Expect-Staple |
| 10918 | // report should not be sent. |
| 10919 | TestDelegate d; |
| 10920 | GURL url = https_test_server.GetURL("/"); |
| 10921 | GURL::Replacements replace_host; |
| 10922 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 10923 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10924 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 10925 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 10926 | ok_request->Start(); |
| 10927 | base::RunLoop().Run(); |
| 10928 | |
| 10929 | // Check that no report was sent. |
| 10930 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 10931 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 10932 | } |
| 10933 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10934 | static const struct OCSPVerifyTestData { |
| 10935 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 10936 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 10937 | OCSPVerifyResult::ResponseStatus response_status; |
| 10938 | bool has_revocation_status; |
| 10939 | OCSPRevocationStatus cert_status; |
| 10940 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10941 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10942 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10943 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10944 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10945 | OCSPVerifyResult::PROVIDED, |
| 10946 | true, |
| 10947 | OCSPRevocationStatus::GOOD}, |
| 10948 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10949 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10950 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10951 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10952 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10953 | OCSPVerifyResult::INVALID_DATE, |
| 10954 | false, |
| 10955 | OCSPRevocationStatus::UNKNOWN}, |
| 10956 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10957 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10958 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10959 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10960 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10961 | OCSPVerifyResult::INVALID_DATE, |
| 10962 | false, |
| 10963 | OCSPRevocationStatus::UNKNOWN}, |
| 10964 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10965 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10966 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10967 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10968 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10969 | OCSPVerifyResult::INVALID_DATE, |
| 10970 | false, |
| 10971 | OCSPRevocationStatus::UNKNOWN}, |
| 10972 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10973 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10974 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10975 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10976 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10977 | OCSPVerifyResult::INVALID_DATE, |
| 10978 | false, |
| 10979 | OCSPRevocationStatus::UNKNOWN}, |
| 10980 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10981 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10982 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 10983 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10984 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10985 | OCSPVerifyResult::ERROR_RESPONSE, |
| 10986 | false, |
| 10987 | OCSPRevocationStatus::UNKNOWN}, |
| 10988 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10989 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10990 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 10991 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10992 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10993 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 10994 | false, |
| 10995 | OCSPRevocationStatus::UNKNOWN}, |
| 10996 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10997 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10998 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 10999 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11000 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11001 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11002 | false, |
| 11003 | OCSPRevocationStatus::UNKNOWN}, |
| 11004 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11005 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11006 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11007 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11008 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11009 | OCSPVerifyResult::INVALID_DATE, |
| 11010 | false, |
| 11011 | OCSPRevocationStatus::UNKNOWN}, |
| 11012 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11013 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11014 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11015 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11016 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11017 | OCSPVerifyResult::PROVIDED, |
| 11018 | true, |
| 11019 | OCSPRevocationStatus::UNKNOWN}, |
| 11020 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11021 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11022 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11023 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11024 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11025 | OCSPVerifyResult::INVALID_DATE, |
| 11026 | false, |
| 11027 | OCSPRevocationStatus::UNKNOWN}, |
| 11028 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11029 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11030 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11031 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11032 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11033 | OCSPVerifyResult::INVALID_DATE, |
| 11034 | false, |
| 11035 | OCSPRevocationStatus::UNKNOWN}, |
| 11036 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11037 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11038 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11039 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11040 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11041 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11042 | false, |
| 11043 | OCSPRevocationStatus::UNKNOWN}, |
| 11044 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11045 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11046 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11047 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11048 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11049 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11050 | false, |
| 11051 | OCSPRevocationStatus::UNKNOWN}, |
| 11052 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11053 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11054 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11055 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11056 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11057 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11058 | false, |
| 11059 | OCSPRevocationStatus::UNKNOWN}, |
| 11060 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11061 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11062 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11063 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11064 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11065 | OCSPVerifyResult::PROVIDED, |
| 11066 | true, |
| 11067 | OCSPRevocationStatus::GOOD}, |
| 11068 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11069 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11070 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11071 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11072 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11073 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11074 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11075 | OCSPVerifyResult::PROVIDED, |
| 11076 | true, |
| 11077 | OCSPRevocationStatus::GOOD}, |
| 11078 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11079 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11080 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11081 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11082 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11083 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11084 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11085 | OCSPVerifyResult::PROVIDED, |
| 11086 | true, |
| 11087 | OCSPRevocationStatus::GOOD}, |
| 11088 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11089 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11090 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11091 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11092 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11093 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11094 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11095 | OCSPVerifyResult::PROVIDED, |
| 11096 | true, |
| 11097 | OCSPRevocationStatus::GOOD}, |
| 11098 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11099 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11100 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11101 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11102 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11103 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11104 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11105 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11106 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11107 | OCSPVerifyResult::INVALID_DATE, |
| 11108 | false, |
| 11109 | OCSPRevocationStatus::UNKNOWN}, |
| 11110 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11111 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11112 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11113 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11114 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11115 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11116 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11117 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11118 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11119 | OCSPVerifyResult::PROVIDED, |
| 11120 | true, |
| 11121 | OCSPRevocationStatus::REVOKED}, |
| 11122 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11123 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11124 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11125 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11126 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11127 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11128 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11129 | OCSPVerifyResult::PROVIDED, |
| 11130 | true, |
| 11131 | OCSPRevocationStatus::UNKNOWN}, |
| 11132 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11133 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11134 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11135 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11136 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11137 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11138 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11139 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11140 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11141 | OCSPVerifyResult::PROVIDED, |
| 11142 | true, |
| 11143 | OCSPRevocationStatus::UNKNOWN}, |
| 11144 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11145 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11146 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11147 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11148 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11149 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11150 | false, |
| 11151 | OCSPRevocationStatus::UNKNOWN}, |
| 11152 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11153 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11154 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11155 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11156 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11157 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11158 | false, |
| 11159 | OCSPRevocationStatus::UNKNOWN}, |
| 11160 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11161 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11162 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11163 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11164 | #if !defined(USE_NSS_CERTS) |
| 11165 | // 25 |
| 11166 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11167 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11168 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11169 | OCSPVerifyResult::PROVIDED, |
| 11170 | true, |
| 11171 | OCSPRevocationStatus::REVOKED}, |
| 11172 | |
| 11173 | // 26 |
| 11174 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11175 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11176 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11177 | OCSPVerifyResult::INVALID_DATE, |
| 11178 | false, |
| 11179 | OCSPRevocationStatus::UNKNOWN}, |
| 11180 | |
| 11181 | // 27 |
| 11182 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11183 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11184 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11185 | OCSPVerifyResult::INVALID_DATE, |
| 11186 | false, |
| 11187 | OCSPRevocationStatus::UNKNOWN}, |
| 11188 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11189 | }; |
| 11190 | |
| 11191 | class HTTPSOCSPVerifyTest |
| 11192 | : public HTTPSOCSPTest, |
| 11193 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11194 | |
| 11195 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 11196 | SpawnedTestServer::SSLOptions ssl_options( |
| 11197 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11198 | OCSPVerifyTestData test = GetParam(); |
| 11199 | |
| 11200 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11201 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11202 | ssl_options.staple_ocsp_response = true; |
| 11203 | |
| 11204 | SSLInfo ssl_info; |
| 11205 | OCSPErrorTestDelegate delegate; |
| 11206 | ASSERT_NO_FATAL_FAILURE( |
| 11207 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11208 | |
| 11209 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11210 | // URLRequest caches certificate errors. |
| 11211 | if (delegate.have_certificate_errors()) { |
| 11212 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11213 | ssl_info = delegate.ssl_info(); |
| 11214 | } |
| 11215 | |
| 11216 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11217 | |
| 11218 | if (test.has_revocation_status) |
| 11219 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11220 | } |
| 11221 | |
| 11222 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 11223 | HTTPSOCSPVerifyTest, |
| 11224 | testing::ValuesIn(kOCSPVerifyData)); |
| 11225 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11226 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11227 | public: |
| 11228 | void SetupContext() override { |
| 11229 | context_.set_ssl_config_service(new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11230 | false /* online revocation checking */, |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11231 | false /* require rev. checking for local anchors */, |
| 11232 | false /* token binding enabled */)); |
| 11233 | } |
| 11234 | }; |
| 11235 | |
| 11236 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11237 | SpawnedTestServer::SSLOptions ssl_options( |
| 11238 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11239 | SpawnedTestServer test_server( |
| 11240 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11241 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11242 | ASSERT_TRUE(test_server.Start()); |
| 11243 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11244 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11245 | // checking. |
| 11246 | ev_test_policy_.reset(); |
| 11247 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11248 | TestDelegate d; |
| 11249 | d.set_allow_certificate_errors(true); |
| 11250 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11251 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11252 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11253 | |
| 11254 | r->Start(); |
| 11255 | EXPECT_TRUE(r->is_pending()); |
| 11256 | |
| 11257 | base::RunLoop().Run(); |
| 11258 | |
| 11259 | EXPECT_EQ(1, d.response_started_count()); |
| 11260 | |
| 11261 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11262 | EXPECT_EQ(OK, d.request_status()); |
| 11263 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11264 | ASSERT_TRUE(r->ssl_info().cert); |
| 11265 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11266 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11267 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11268 | } |
| 11269 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11270 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11271 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11272 | void SetupContext() override { |
| 11273 | context_.set_ssl_config_service(new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11274 | false /* online revocation checking */, |
| 11275 | true /* require rev. checking for local anchors */, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11276 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11277 | } |
| 11278 | }; |
| 11279 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11280 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11281 | if (!SystemSupportsOCSP()) { |
| 11282 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11283 | return; |
| 11284 | } |
| 11285 | |
| 11286 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11287 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11288 | << "revocation checking"; |
| 11289 | return; |
| 11290 | } |
| 11291 | |
| 11292 | SpawnedTestServer::SSLOptions ssl_options( |
| 11293 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11294 | ssl_options.ocsp_status = |
| 11295 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11296 | |
| 11297 | CertStatus cert_status; |
| 11298 | DoConnection(ssl_options, &cert_status); |
| 11299 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11300 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11301 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11302 | // affirmatively revoked? |
| 11303 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11304 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11305 | #else |
| 11306 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11307 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11308 | |
| 11309 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11310 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11311 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11312 | } |
| 11313 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11314 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11315 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11316 | void SetupContext() override { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11317 | context_.set_ssl_config_service( |
| 11318 | new TestSSLConfigService(false /* online revocation checking */, |
| 11319 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11320 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11321 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11322 | } |
| 11323 | }; |
| 11324 | |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11325 | // Helper class to set the global CRLSet, and on destruction restore the |
| 11326 | // previously set one. |
| 11327 | class ScopedSetCRLSet { |
| 11328 | public: |
| 11329 | ScopedSetCRLSet(scoped_refptr<CRLSet> crl_set) { |
| 11330 | prev_crl_set_ = SSLConfigService::GetCRLSet(); |
| 11331 | SSLConfigService::SetCRLSetForTesting(std::move(crl_set)); |
| 11332 | } |
| 11333 | |
| 11334 | ~ScopedSetCRLSet() { |
| 11335 | SSLConfigService::SetCRLSetForTesting(std::move(prev_crl_set_)); |
| 11336 | } |
| 11337 | |
| 11338 | private: |
| 11339 | scoped_refptr<CRLSet> prev_crl_set_; |
| 11340 | }; |
| 11341 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11342 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11343 | if (!SystemSupportsOCSP()) { |
| 11344 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11345 | return; |
| 11346 | } |
| 11347 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11348 | SpawnedTestServer::SSLOptions ssl_options( |
| 11349 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11350 | ssl_options.ocsp_status = |
| 11351 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11352 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11353 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11354 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11355 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11356 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11357 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11358 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11359 | |
| 11360 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11361 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11362 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11363 | } |
| 11364 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11365 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11366 | if (!SystemSupportsOCSP()) { |
| 11367 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11368 | return; |
| 11369 | } |
| 11370 | |
| 11371 | SpawnedTestServer::SSLOptions ssl_options( |
| 11372 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11373 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11374 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11375 | |
| 11376 | CertStatus cert_status; |
| 11377 | DoConnection(ssl_options, &cert_status); |
| 11378 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11379 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11380 | // possible to determine whether the check failed because of actual |
| 11381 | // revocation or because there was an OCSP failure. |
| 11382 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11383 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11384 | #else |
| 11385 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11386 | #endif |
| 11387 | |
| 11388 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11389 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11390 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11391 | } |
| 11392 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11393 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11394 | if (!SystemSupportsOCSP()) { |
| 11395 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11396 | return; |
| 11397 | } |
| 11398 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11399 | SpawnedTestServer::SSLOptions ssl_options( |
| 11400 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11401 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11402 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11403 | |
| 11404 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11405 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11406 | |
| 11407 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11408 | |
| 11409 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11410 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11411 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11412 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11413 | } |
| 11414 | |
| 11415 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11416 | if (!SystemSupportsOCSP()) { |
| 11417 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11418 | return; |
| 11419 | } |
| 11420 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11421 | SpawnedTestServer::SSLOptions ssl_options( |
| 11422 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11423 | ssl_options.ocsp_status = |
| 11424 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11425 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11426 | |
| 11427 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11428 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11429 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11430 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11431 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11432 | |
| 11433 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11434 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11435 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11436 | } |
| 11437 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11438 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11439 | if (!SystemSupportsOCSP()) { |
| 11440 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11441 | return; |
| 11442 | } |
| 11443 | |
| 11444 | SpawnedTestServer::SSLOptions ssl_options( |
| 11445 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11446 | ssl_options.ocsp_status = |
| 11447 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11448 | ScopedSetCRLSet set_crlset( |
| 11449 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {})); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11450 | |
| 11451 | CertStatus cert_status; |
| 11452 | DoConnection(ssl_options, &cert_status); |
| 11453 | |
| 11454 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11455 | // revocation check for EV. |
| 11456 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11457 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11458 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11459 | EXPECT_FALSE( |
| 11460 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11461 | } |
| 11462 | |
| 11463 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11464 | if (!SystemSupportsOCSP()) { |
| 11465 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11466 | return; |
| 11467 | } |
| 11468 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11469 | SpawnedTestServer::SSLOptions ssl_options( |
| 11470 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11471 | ssl_options.ocsp_status = |
| 11472 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11473 | ScopedSetCRLSet set_crlset(CRLSet::EmptyCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11474 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11475 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11476 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11477 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11478 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11479 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11480 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11481 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11482 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11483 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11484 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11485 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11486 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11487 | } |
| 11488 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11489 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11490 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11491 | void SetupContext() override { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11492 | context_.set_ssl_config_service( |
| 11493 | new TestSSLConfigService(false /* online revocation checking */, |
| 11494 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11495 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11496 | false /* token binding enabled */)); |
| 11497 | } |
| 11498 | |
| 11499 | void SetUp() override { |
| 11500 | HTTPSOCSPTest::SetUp(); |
| 11501 | |
| 11502 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11503 | // checking (as per the user preference). |
| 11504 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11505 | } |
| 11506 | }; |
| 11507 | |
| 11508 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11509 | SpawnedTestServer::SSLOptions ssl_options( |
| 11510 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11511 | ssl_options.ocsp_status = |
| 11512 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11513 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11514 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11515 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11516 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11517 | |
| 11518 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11519 | // we don't fall back to online revocation checks. |
| 11520 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11521 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11522 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11523 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11524 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11525 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 11526 | // Test that when online revocation checking is disabled, and the leaf |
| 11527 | // certificate is not EV, that no revocation checking actually happens. |
| 11528 | if (!SystemSupportsOCSP()) { |
| 11529 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11530 | return; |
| 11531 | } |
| 11532 | |
| 11533 | SpawnedTestServer::SSLOptions ssl_options( |
| 11534 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11535 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11536 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
| 11537 | |
| 11538 | CertStatus cert_status; |
| 11539 | DoConnection(ssl_options, &cert_status); |
| 11540 | |
| 11541 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11542 | |
| 11543 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11544 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11545 | } |
| 11546 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11547 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11548 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11549 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11550 | return; |
| 11551 | #endif |
| 11552 | |
| 11553 | SpawnedTestServer::SSLOptions ssl_options( |
| 11554 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11555 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11556 | ssl_options.cert_serial = 10; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11557 | ScopedSetCRLSet set_crlset( |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11558 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {})); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11559 | |
| 11560 | CertStatus cert_status = 0; |
| 11561 | DoConnection(ssl_options, &cert_status); |
| 11562 | |
| 11563 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11564 | // reflected without online revocation checking. |
| 11565 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11566 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11567 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11568 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11569 | |
| 11570 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 11571 | #if defined(OS_ANDROID) |
| 11572 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11573 | return; |
| 11574 | #endif |
| 11575 | |
| 11576 | SpawnedTestServer::SSLOptions ssl_options( |
| 11577 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11578 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11579 | static const char kCommonName[] = "Test CN"; |
| 11580 | ssl_options.cert_common_name = kCommonName; |
| 11581 | |
| 11582 | { |
| 11583 | ScopedSetCRLSet set_crlset( |
| 11584 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {})); |
| 11585 | |
| 11586 | CertStatus cert_status = 0; |
| 11587 | DoConnection(ssl_options, &cert_status); |
| 11588 | |
| 11589 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11590 | // reflected without online revocation checking. |
| 11591 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11592 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame^] | 11593 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11594 | } |
| 11595 | |
| 11596 | const uint8_t kTestServerSPKISHA256[32] = { |
| 11597 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 11598 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 11599 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 11600 | }; |
| 11601 | const std::string spki_hash( |
| 11602 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 11603 | sizeof(kTestServerSPKISHA256)); |
| 11604 | |
| 11605 | { |
| 11606 | ScopedSetCRLSet set_crlset( |
| 11607 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash})); |
| 11608 | |
| 11609 | CertStatus cert_status = 0; |
| 11610 | DoConnection(ssl_options, &cert_status); |
| 11611 | |
| 11612 | // When the correct SPKI hash is specified, the connection should succeed |
| 11613 | // even though the subject is listed in the CRLSet. |
| 11614 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11615 | } |
| 11616 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11617 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11618 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11619 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11620 | !defined(OS_FUCHSIA) |
| 11621 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11622 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11623 | // connections reliably. FTP tests are disabled on platforms that use |
| 11624 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11625 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11626 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11627 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11628 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11629 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11630 | // Can't use |default_context_|'s HostResolver to set up the |
| 11631 | // FTPTransactionFactory because it hasn't been created yet. |
| 11632 | default_context_.set_host_resolver(&host_resolver_); |
| 11633 | } |
| 11634 | |
| 11635 | // URLRequestTest interface: |
| 11636 | void SetUpFactory() override { |
| 11637 | // Add FTP support to the default URLRequestContext. |
| 11638 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11639 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11640 | } |
| 11641 | |
| 11642 | std::string GetTestFileContents() { |
| 11643 | base::FilePath path; |
| 11644 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 11645 | path = path.Append(kTestFilePath); |
| 11646 | path = path.AppendASCII(kFtpTestFile); |
| 11647 | std::string contents; |
| 11648 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11649 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11650 | } |
| 11651 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11652 | protected: |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11653 | // Note that this is destroyed before the FtpProtocolHandler that references |
| 11654 | // it, which is owned by the parent class. Since no requests are made during |
| 11655 | // teardown, this works, though it's not great. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11656 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11657 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11658 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11659 | }; |
| 11660 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11661 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11662 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11663 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11664 | |
| 11665 | TestDelegate d; |
| 11666 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11667 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11668 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11669 | r->Start(); |
| 11670 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11671 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11672 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11673 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11674 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11675 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11676 | } |
| 11677 | } |
| 11678 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11679 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11680 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11681 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11682 | TestDelegate d; |
| 11683 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11684 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11685 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11686 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11687 | r->Start(); |
| 11688 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11689 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11690 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11691 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11692 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11693 | EXPECT_EQ(1, d.response_started_count()); |
| 11694 | EXPECT_FALSE(d.received_data_before_response()); |
| 11695 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11696 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11697 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11698 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11699 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11700 | } |
| 11701 | } |
| 11702 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11703 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11704 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11705 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11706 | TestDelegate d; |
| 11707 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11708 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11709 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11710 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11711 | r->Start(); |
| 11712 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11713 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11714 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11715 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11716 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11717 | EXPECT_EQ(1, d.response_started_count()); |
| 11718 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11719 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11720 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11721 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11722 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11723 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11724 | } |
| 11725 | } |
| 11726 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11727 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11728 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11729 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11730 | TestDelegate d; |
| 11731 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11732 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11733 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11734 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11735 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11736 | r->Start(); |
| 11737 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11738 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11739 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11740 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11741 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11742 | EXPECT_EQ(1, d.response_started_count()); |
| 11743 | EXPECT_FALSE(d.received_data_before_response()); |
| 11744 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11745 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11746 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11747 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11748 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11749 | |
| 11750 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11751 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11752 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11753 | } |
| 11754 | } |
| 11755 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11756 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11757 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11758 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11759 | TestDelegate d; |
| 11760 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11761 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11762 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11763 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11764 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11765 | r->Start(); |
| 11766 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11767 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11768 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11769 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11770 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11771 | EXPECT_EQ(1, d.response_started_count()); |
| 11772 | EXPECT_FALSE(d.received_data_before_response()); |
| 11773 | EXPECT_EQ(d.bytes_received(), 0); |
| 11774 | } |
| 11775 | } |
| 11776 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11777 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11778 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11779 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11780 | TestDelegate d; |
| 11781 | // Set correct login credentials. The delegate will be asked for them when |
| 11782 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11783 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11784 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11785 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11786 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11787 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11788 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11789 | r->Start(); |
| 11790 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11791 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11792 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11793 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11794 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11795 | EXPECT_EQ(1, d.response_started_count()); |
| 11796 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11797 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11798 | } |
| 11799 | } |
| 11800 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11801 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11802 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11803 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11804 | TestDelegate d; |
| 11805 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11806 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11807 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11808 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11809 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11810 | r->Start(); |
| 11811 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11812 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11813 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11814 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11815 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11816 | EXPECT_EQ(1, d.response_started_count()); |
| 11817 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11818 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11819 | } |
| 11820 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11821 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11822 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11823 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11824 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11825 | TestDelegate d; |
| 11826 | // Set correct login credentials. The delegate will be asked for them when |
| 11827 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11828 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11829 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11830 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11831 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11832 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11833 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11834 | r->Start(); |
| 11835 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11836 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11837 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11838 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11839 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11840 | EXPECT_EQ(1, d.response_started_count()); |
| 11841 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11842 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11843 | } |
| 11844 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11845 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11846 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11847 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11848 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11849 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11850 | { |
| 11851 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11852 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11853 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11854 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11855 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11856 | r->Start(); |
| 11857 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11858 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11859 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11860 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11861 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11862 | EXPECT_EQ(1, d->response_started_count()); |
| 11863 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11864 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11865 | } |
| 11866 | |
| 11867 | d.reset(new TestDelegate); |
| 11868 | { |
| 11869 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11870 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11871 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11872 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11873 | r->Start(); |
| 11874 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11875 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11876 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11877 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11878 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11879 | EXPECT_EQ(1, d->response_started_count()); |
| 11880 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11881 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11882 | } |
| 11883 | } |
| 11884 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11885 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11886 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11887 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11888 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11889 | // Set correct login credentials. The delegate will be asked for them when |
| 11890 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11891 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11892 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11893 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11894 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11895 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11896 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11897 | r->Start(); |
| 11898 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11899 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11900 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11901 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11902 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11903 | EXPECT_EQ(1, d->response_started_count()); |
| 11904 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11905 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11906 | } |
| 11907 | |
| 11908 | // Use a new delegate without explicit credentials. The cached ones should be |
| 11909 | // used. |
| 11910 | d.reset(new TestDelegate); |
| 11911 | { |
| 11912 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 11913 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11914 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11915 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11916 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11917 | r->Start(); |
| 11918 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11919 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 11920 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11921 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11922 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11923 | EXPECT_EQ(1, d->response_started_count()); |
| 11924 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11925 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11926 | } |
| 11927 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11928 | |
| 11929 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 11930 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11931 | |
| 11932 | TestDelegate d; |
| 11933 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11934 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 11935 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11936 | req->Start(); |
| 11937 | base::RunLoop().Run(); |
| 11938 | |
| 11939 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 11940 | } |
| 11941 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11942 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11943 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11944 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 11945 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11946 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 11947 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11948 | |
| 11949 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11950 | |
| 11951 | req->Start(); |
| 11952 | base::RunLoop().Run(); |
| 11953 | |
| 11954 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 11955 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11956 | } |
| 11957 | |
| 11958 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 11959 | MockHostResolver host_resolver; |
| 11960 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 11961 | TestURLRequestContext context(true); |
| 11962 | context.set_network_delegate(&network_delegate); |
| 11963 | context.set_host_resolver(&host_resolver); |
| 11964 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 11965 | context.Init(); |
| 11966 | |
| 11967 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11968 | std::unique_ptr<URLRequest> req( |
| 11969 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 11970 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11971 | |
| 11972 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11973 | |
| 11974 | req->Start(); |
| 11975 | base::RunLoop().Run(); |
| 11976 | EXPECT_TRUE(req->response_info().network_accessed); |
| 11977 | } |
| 11978 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11979 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11980 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11981 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11982 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11983 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11984 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 11985 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11986 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11987 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11988 | req.get(), &default_network_delegate_, |
| 11989 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11990 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 11991 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11992 | |
| 11993 | req->Start(); |
| 11994 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11995 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11996 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11997 | EXPECT_EQ(0, d.received_redirect_count()); |
| 11998 | } |
| 11999 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12000 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12001 | ASSERT_TRUE(http_test_server()->Start()); |
| 12002 | TestURLRequestContext context; |
| 12003 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12004 | TestDelegate delegate; |
| 12005 | HttpRequestHeaders extra_headers; |
| 12006 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12007 | |
| 12008 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12009 | HttpRawRequestHeaders raw_req_headers; |
| 12010 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12011 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12012 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12013 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12014 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12015 | r->SetRequestHeadersCallback(base::Bind( |
| 12016 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12017 | r->SetResponseHeadersCallback(base::Bind( |
| 12018 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12019 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12020 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12021 | r->Start(); |
| 12022 | while (!delegate.response_started_count()) |
| 12023 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12024 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12025 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12026 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12027 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12028 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12029 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12030 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12031 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12032 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12033 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12034 | } |
| 12035 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12036 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12037 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12038 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12039 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12040 | FAIL() << "Callback should not be called unless request is sent"; |
| 12041 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12042 | r->SetResponseHeadersCallback( |
| 12043 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12044 | FAIL() << "Callback should not be called unless request is sent"; |
| 12045 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12046 | r->Start(); |
| 12047 | base::RunLoop().Run(); |
| 12048 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12049 | } |
| 12050 | } |
| 12051 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12052 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12053 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12054 | HttpRawRequestHeaders raw_req_headers; |
| 12055 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12056 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12057 | TestURLRequestContext context; |
| 12058 | TestDelegate delegate; |
| 12059 | HttpRequestHeaders extra_headers; |
| 12060 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12061 | delegate.set_quit_on_redirect(true); |
| 12062 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
| 12063 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12064 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12065 | r->SetExtraRequestHeaders(extra_headers); |
| 12066 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12067 | base::Unretained(&raw_req_headers))); |
| 12068 | r->SetResponseHeadersCallback(base::Bind( |
| 12069 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12070 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12071 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12072 | r->Start(); |
| 12073 | base::RunLoop().Run(); |
| 12074 | |
| 12075 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12076 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12077 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12078 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12079 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12080 | EXPECT_EQ("gzip, deflate", value); |
| 12081 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12082 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12083 | raw_req_headers.request_line()); |
| 12084 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12085 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12086 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12087 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12088 | raw_req_headers = HttpRawRequestHeaders(); |
| 12089 | raw_resp_headers = nullptr; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12090 | r->FollowDeferredRedirect(); |
| 12091 | base::RunLoop().Run(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12092 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12093 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12094 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12095 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12096 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12097 | raw_req_headers.request_line()); |
| 12098 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12099 | } |
| 12100 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12101 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12102 | TestDelegate request_delegate; |
| 12103 | |
| 12104 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12105 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12106 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12107 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12108 | FAIL() << "Callback should not be called unless request is sent"; |
| 12109 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12110 | r->SetResponseHeadersCallback( |
| 12111 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12112 | FAIL() << "Callback should not be called unless request is sent"; |
| 12113 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12114 | r->Start(); |
| 12115 | base::RunLoop().Run(); |
| 12116 | EXPECT_FALSE(r->is_pending()); |
| 12117 | } |
| 12118 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12119 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12120 | ASSERT_TRUE(http_test_server()->Start()); |
| 12121 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12122 | |
| 12123 | TestURLRequestContext context; |
| 12124 | TestDelegate delegate; |
| 12125 | |
| 12126 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12127 | HttpRequestHeaders extra_headers; |
| 12128 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12129 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12130 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12131 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12132 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12133 | using RespHeadersVector = |
| 12134 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12135 | RespHeadersVector raw_resp_headers; |
| 12136 | |
| 12137 | auto req_headers_callback = base::Bind( |
| 12138 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12139 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12140 | }, |
| 12141 | &raw_req_headers); |
| 12142 | auto resp_headers_callback = base::Bind( |
| 12143 | [](RespHeadersVector* vec, |
| 12144 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12145 | vec->push_back(headers); |
| 12146 | }, |
| 12147 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12148 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12149 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12150 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12151 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12152 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12153 | r->Start(); |
| 12154 | base::RunLoop().Run(); |
| 12155 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12156 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12157 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12158 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12159 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12160 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12161 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12162 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12163 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12164 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12165 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12166 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12167 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12168 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12169 | |
| 12170 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12171 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12172 | r2->SetExtraRequestHeaders(extra_headers); |
| 12173 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12174 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12175 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12176 | r2->Start(); |
| 12177 | base::RunLoop().Run(); |
| 12178 | EXPECT_FALSE(r2->is_pending()); |
| 12179 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12180 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12181 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12182 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12183 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12184 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12185 | } |
| 12186 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12187 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12188 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12189 | TestDelegate d; |
| 12190 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12191 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12192 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12193 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12194 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12195 | r->SetResponseHeadersCallback( |
| 12196 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12197 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12198 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12199 | r->Start(); |
| 12200 | base::RunLoop().Run(); |
| 12201 | EXPECT_FALSE(r->is_pending()); |
| 12202 | } |
| 12203 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12204 | // Test that URLRequests get properly tagged. |
| 12205 | #if defined(OS_ANDROID) |
| 12206 | TEST_F(URLRequestTestHTTP, TestTagging) { |
| 12207 | ASSERT_TRUE(http_test_server()->Start()); |
| 12208 | |
| 12209 | // The tag under which the system reports untagged traffic. |
| 12210 | static const int32_t UNTAGGED_TAG = 0; |
| 12211 | |
| 12212 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 12213 | |
| 12214 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 12215 | TestDelegate delegate; |
| 12216 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 12217 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12218 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12219 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12220 | req->Start(); |
| 12221 | base::RunLoop().Run(); |
| 12222 | |
| 12223 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12224 | |
| 12225 | int32_t tag_val1 = 0x12345678; |
| 12226 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12227 | old_traffic = GetTaggedBytes(tag_val1); |
| 12228 | |
| 12229 | // Test specific tag value. |
| 12230 | req = default_context_.CreateRequest(http_test_server()->GetURL("/"), |
| 12231 | DEFAULT_PRIORITY, &delegate, |
| 12232 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12233 | req->set_socket_tag(tag1); |
| 12234 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12235 | req->Start(); |
| 12236 | base::RunLoop().Run(); |
| 12237 | |
| 12238 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12239 | } |
| 12240 | #endif |
| 12241 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12242 | } // namespace net |