[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5 | #include <memory> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 6 | #include <utility> |
| 7 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8 | #include "base/memory/ptr_util.h" |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 9 | #include "base/run_loop.h" |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 12 | #if defined(OS_WIN) |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 13 | #include <windows.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 14 | #include <shlobj.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 15 | #endif |
| 16 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 20 | #include <limits> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 22 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 23 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 24 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 26 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 27 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 28 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 29 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 30 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 31 | #include "base/macros.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 32 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 33 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "base/path_service.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 35 | #include "base/power_monitor/power_monitor.h" |
| 36 | #include "base/power_monitor/power_monitor_source.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 37 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 38 | #include "base/single_thread_task_runner.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 39 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 40 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 41 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 42 | #include "base/strings/string_util.h" |
| 43 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 44 | #include "base/strings/utf_string_conversions.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 45 | #include "base/test/histogram_tester.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 46 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 47 | #include "base/values.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 48 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 49 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 50 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 52 | #include "net/base/load_timing_info.h" |
| 53 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 54 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | #include "net/base/net_module.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 56 | #include "net/base/request_priority.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 57 | #include "net/base/upload_bytes_element_reader.h" |
| 58 | #include "net/base/upload_data_stream.h" |
| 59 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 60 | #include "net/base/url_util.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 61 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 62 | #include "net/cert/ct_policy_status.h" |
| 63 | #include "net/cert/ct_verifier.h" |
| 64 | #include "net/cert/ct_verify_result.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 65 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 66 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 67 | #include "net/cert/test_root_certs.h" |
eroman | ed744f3 | 2015-04-09 06:35:49 | [diff] [blame] | 68 | #include "net/cert_net/nss_ocsp.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 69 | #include "net/cookies/cookie_monster.h" |
| 70 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 71 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 72 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 73 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 74 | #include "net/http/http_cache.h" |
| 75 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 76 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 77 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 78 | #include "net/http/http_response_headers.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 79 | #include "net/http/http_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 80 | #include "net/log/net_log.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 81 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 82 | #include "net/log/test_net_log_entry.h" |
| 83 | #include "net/log/test_net_log_util.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 84 | #include "net/nqe/external_estimate_provider.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 85 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 86 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 87 | #include "net/ssl/channel_id_service.h" |
| 88 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 89 | #include "net/ssl/ssl_connection_status_flags.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 90 | #include "net/ssl/ssl_server_config.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 91 | #include "net/ssl/token_binding.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 92 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 93 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 94 | #include "net/test/embedded_test_server/http_request.h" |
| 95 | #include "net/test/embedded_test_server/http_response.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 96 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 97 | #include "net/test/test_data_directory.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 98 | #include "net/test/url_request/url_request_failed_job.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 99 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 100 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 101 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 102 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 103 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 104 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 105 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 106 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 107 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 108 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 109 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 110 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 111 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 113 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 114 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 115 | #include "net/url_request/file_protocol_handler.h" |
| 116 | #include "net/url_request/url_request_file_dir_job.h" |
| 117 | #endif |
| 118 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 119 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 120 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 121 | #include "net/url_request/ftp_protocol_handler.h" |
| 122 | #endif |
| 123 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 124 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 125 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 126 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 127 | #endif |
| 128 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 129 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 130 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 131 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 132 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 133 | namespace net { |
| 134 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 135 | namespace { |
| 136 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 137 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 138 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 139 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 140 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 141 | const base::FilePath::CharType kTestFilePath[] = |
| 142 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 143 | |
| 144 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 145 | // Test file used in most FTP tests. |
| 146 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 147 | #endif |
| 148 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 149 | // Tests load timing information in the case a fresh connection was used, with |
| 150 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 151 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 152 | int connect_timing_flags) { |
| 153 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 154 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 155 | |
| 156 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 157 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 158 | |
| 159 | EXPECT_LE(load_timing_info.request_start, |
| 160 | load_timing_info.connect_timing.connect_start); |
| 161 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 162 | connect_timing_flags); |
| 163 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 164 | load_timing_info.send_start); |
| 165 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 166 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 167 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 168 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 169 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 170 | } |
| 171 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 172 | // Same as above, but with proxy times. |
| 173 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 174 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 175 | int connect_timing_flags) { |
| 176 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 177 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 178 | |
| 179 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 180 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 181 | |
| 182 | EXPECT_LE(load_timing_info.request_start, |
| 183 | load_timing_info.proxy_resolve_start); |
| 184 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 185 | load_timing_info.proxy_resolve_end); |
| 186 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 187 | load_timing_info.connect_timing.connect_start); |
| 188 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 189 | connect_timing_flags); |
| 190 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 191 | load_timing_info.send_start); |
| 192 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 193 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 194 | } |
| 195 | |
| 196 | // Same as above, but with a reused socket and proxy times. |
| 197 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 198 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 199 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 200 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 201 | |
| 202 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 203 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 204 | |
| 205 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 206 | |
| 207 | EXPECT_LE(load_timing_info.request_start, |
| 208 | load_timing_info.proxy_resolve_start); |
| 209 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 210 | load_timing_info.proxy_resolve_end); |
| 211 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 212 | load_timing_info.send_start); |
| 213 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 214 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 215 | } |
| 216 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 217 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 218 | // Tests load timing information in the case of a cache hit, when no cache |
| 219 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 220 | base::StringPiece TestNetResourceProvider(int key) { |
| 221 | return "header"; |
| 222 | } |
| 223 | |
| 224 | void FillBuffer(char* buffer, size_t len) { |
| 225 | static bool called = false; |
| 226 | if (!called) { |
| 227 | called = true; |
| 228 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 229 | srand(seed); |
| 230 | } |
| 231 | |
| 232 | for (size_t i = 0; i < len; i++) { |
| 233 | buffer[i] = static_cast<char>(rand()); |
| 234 | if (!buffer[i]) |
| 235 | buffer[i] = 'g'; |
| 236 | } |
| 237 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 238 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 239 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 240 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 241 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 242 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 243 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 244 | |
| 245 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 246 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 247 | |
| 248 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 249 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 250 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 251 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 252 | |
| 253 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 254 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 255 | } |
| 256 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 257 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 258 | // Tests load timing in the case that there is no HTTP response. This can be |
| 259 | // used to test in the case of errors or non-HTTP requests. |
| 260 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 261 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 262 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 263 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 264 | |
| 265 | // Only the request times should be non-null. |
| 266 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 267 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 268 | |
| 269 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 270 | |
| 271 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 272 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 273 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 274 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 275 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 276 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 277 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 278 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 279 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 280 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 281 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 282 | public: |
| 283 | TestPowerMonitorSource() {} |
| 284 | ~TestPowerMonitorSource() override {} |
| 285 | |
| 286 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 287 | |
| 288 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 289 | |
| 290 | bool IsOnBatteryPowerImpl() override { return false; } |
| 291 | |
| 292 | private: |
| 293 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 294 | }; |
| 295 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 296 | // Job that allows monitoring of its priority. |
| 297 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 298 | public: |
| 299 | // The latest priority of the job is always written to |request_priority_|. |
| 300 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 301 | NetworkDelegate* network_delegate, |
| 302 | RequestPriority* request_priority) |
| 303 | : URLRequestTestJob(request, network_delegate), |
| 304 | request_priority_(request_priority) { |
| 305 | *request_priority_ = DEFAULT_PRIORITY; |
| 306 | } |
| 307 | |
| 308 | void SetPriority(RequestPriority priority) override { |
| 309 | *request_priority_ = priority; |
| 310 | URLRequestTestJob::SetPriority(priority); |
| 311 | } |
| 312 | |
| 313 | private: |
| 314 | RequestPriority* const request_priority_; |
| 315 | }; |
| 316 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 317 | // Do a case-insensitive search through |haystack| for |needle|. |
| 318 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 319 | std::string::const_iterator it = std::search( |
| 320 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 321 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 322 | return it != haystack.end(); |
| 323 | } |
| 324 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 325 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 326 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 327 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 328 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 331 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 332 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 333 | // -1 means unknown. 0 means no encryption. |
| 334 | EXPECT_GT(ssl_info.security_bits, 0); |
| 335 | |
| 336 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 337 | uint16_t cipher_suite = |
| 338 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 339 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 342 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 343 | const GURL& host_url) { |
| 344 | std::string sent_value; |
| 345 | |
| 346 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 347 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 348 | |
| 349 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 350 | EXPECT_EQ("keep-alive", sent_value); |
| 351 | } |
| 352 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 353 | // A network delegate that allows the user to choose a subset of request stages |
| 354 | // to block in. When blocking, the delegate can do one of the following: |
| 355 | // * synchronously return a pre-specified error code, or |
| 356 | // * asynchronously return that value via an automatically called callback, |
| 357 | // or |
| 358 | // * block and wait for the user to do a callback. |
| 359 | // Additionally, the user may also specify a redirect URL -- then each request |
| 360 | // with the current URL different from the redirect target will be redirected |
| 361 | // to that target, in the on-before-URL-request stage, independent of whether |
| 362 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 363 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 364 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 365 | // Stages in which the delegate can block. |
| 366 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 367 | NOT_BLOCKED = 0, |
| 368 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 369 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 370 | ON_HEADERS_RECEIVED = 1 << 2, |
| 371 | ON_AUTH_REQUIRED = 1 << 3 |
| 372 | }; |
| 373 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 374 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 375 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 376 | enum BlockMode { |
| 377 | SYNCHRONOUS, // No callback, returns specified return values. |
| 378 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 379 | // specified return codes. |
| 380 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 381 | // |auth_retval_| are ignored. In every blocking stage the |
| 382 | // message loop is quit. |
| 383 | }; |
| 384 | |
| 385 | // Creates a delegate which does not block at all. |
| 386 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 387 | |
| 388 | // For users to trigger a callback returning |response|. |
| 389 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 390 | // Only call if |block_mode_| == USER_CALLBACK. |
| 391 | void DoCallback(int response); |
| 392 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 393 | |
| 394 | // Setters. |
| 395 | void set_retval(int retval) { |
| 396 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 397 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 398 | ASSERT_NE(OK, retval); |
| 399 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 400 | } |
| 401 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 402 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 403 | // |auth_credentials_| will be passed with the response. |
| 404 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 405 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 406 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 407 | auth_retval_ = auth_retval; |
| 408 | } |
| 409 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 410 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 411 | } |
| 412 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 413 | void set_redirect_url(const GURL& url) { |
| 414 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 417 | void set_block_on(int block_on) { |
| 418 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 421 | // Allows the user to check in which state did we block. |
| 422 | Stage stage_blocked_for_callback() const { |
| 423 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 424 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | void RunCallback(int response, const CompletionCallback& callback); |
| 429 | void RunAuthCallback(AuthRequiredResponse response, |
| 430 | const AuthCallback& callback); |
| 431 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 432 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 433 | int OnBeforeURLRequest(URLRequest* request, |
| 434 | const CompletionCallback& callback, |
| 435 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 436 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 437 | int OnBeforeStartTransaction(URLRequest* request, |
| 438 | const CompletionCallback& callback, |
| 439 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 440 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 441 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 442 | URLRequest* request, |
| 443 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 444 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 445 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 446 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 447 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 448 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 449 | URLRequest* request, |
| 450 | const AuthChallengeInfo& auth_info, |
| 451 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 452 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 453 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 454 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 455 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 456 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 457 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 458 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 459 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 460 | |
| 461 | // Configuration parameters, can be adjusted by public methods: |
| 462 | const BlockMode block_mode_; |
| 463 | |
| 464 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 465 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 466 | int retval_; // To be returned in non-auth stages. |
| 467 | AuthRequiredResponse auth_retval_; |
| 468 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 469 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 470 | int block_on_; // Bit mask: in which stages to block. |
| 471 | |
| 472 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 473 | // callback. |
| 474 | AuthCredentials auth_credentials_; |
| 475 | AuthCredentials* target_auth_credentials_; |
| 476 | |
| 477 | // Internal variables, not set by not the user: |
| 478 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 479 | // USER_CALLBACK). |
| 480 | Stage stage_blocked_for_callback_; |
| 481 | |
| 482 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 483 | CompletionCallback callback_; |
| 484 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 485 | |
| 486 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 487 | |
| 488 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 489 | }; |
| 490 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 491 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 492 | : block_mode_(block_mode), |
| 493 | retval_(OK), |
| 494 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 495 | block_on_(0), |
| 496 | target_auth_credentials_(NULL), |
| 497 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 498 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 502 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 503 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 504 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 505 | CompletionCallback callback = callback_; |
| 506 | Reset(); |
| 507 | RunCallback(response, callback); |
| 508 | } |
| 509 | |
| 510 | void BlockingNetworkDelegate::DoAuthCallback( |
| 511 | NetworkDelegate::AuthRequiredResponse response) { |
| 512 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 513 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 514 | AuthCallback auth_callback = auth_callback_; |
| 515 | Reset(); |
| 516 | RunAuthCallback(response, auth_callback); |
| 517 | } |
| 518 | |
| 519 | void BlockingNetworkDelegate::RunCallback(int response, |
| 520 | const CompletionCallback& callback) { |
| 521 | callback.Run(response); |
| 522 | } |
| 523 | |
| 524 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 525 | const AuthCallback& callback) { |
| 526 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 527 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 528 | *target_auth_credentials_ = auth_credentials_; |
| 529 | } |
| 530 | callback.Run(response); |
| 531 | } |
| 532 | |
| 533 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 534 | URLRequest* request, |
| 535 | const CompletionCallback& callback, |
| 536 | GURL* new_url) { |
| 537 | if (redirect_url_ == request->url()) |
| 538 | return OK; // We've already seen this request and redirected elsewhere. |
| 539 | |
| 540 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 541 | |
| 542 | if (!redirect_url_.is_empty()) |
| 543 | *new_url = redirect_url_; |
| 544 | |
| 545 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 546 | } |
| 547 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 548 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 549 | URLRequest* request, |
| 550 | const CompletionCallback& callback, |
| 551 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 552 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 553 | |
| 554 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 555 | } |
| 556 | |
| 557 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 558 | URLRequest* request, |
| 559 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 560 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 561 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 562 | GURL* allowed_unsafe_redirect_url) { |
| 563 | TestNetworkDelegate::OnHeadersReceived(request, |
| 564 | callback, |
| 565 | original_response_headers, |
| 566 | override_response_headers, |
| 567 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 568 | |
| 569 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 570 | } |
| 571 | |
| 572 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 573 | URLRequest* request, |
| 574 | const AuthChallengeInfo& auth_info, |
| 575 | const AuthCallback& callback, |
| 576 | AuthCredentials* credentials) { |
| 577 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 578 | credentials); |
| 579 | // Check that the user has provided callback for the previous blocked stage. |
| 580 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 581 | |
| 582 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 583 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 584 | } |
| 585 | |
| 586 | target_auth_credentials_ = credentials; |
| 587 | |
| 588 | switch (block_mode_) { |
| 589 | case SYNCHRONOUS: |
| 590 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 591 | *target_auth_credentials_ = auth_credentials_; |
| 592 | return auth_retval_; |
| 593 | |
| 594 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 595 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 596 | FROM_HERE, |
| 597 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 598 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 599 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 600 | |
| 601 | case USER_CALLBACK: |
| 602 | auth_callback_ = callback; |
| 603 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 604 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 605 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 606 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 607 | } |
| 608 | NOTREACHED(); |
| 609 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 610 | } |
| 611 | |
| 612 | void BlockingNetworkDelegate::Reset() { |
| 613 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 614 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 615 | callback_.Reset(); |
| 616 | auth_callback_.Reset(); |
| 617 | } |
| 618 | |
| 619 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 620 | BlockingNetworkDelegate::Stage stage, |
| 621 | const CompletionCallback& callback) { |
| 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_ & stage) == 0) { |
| 626 | return OK; |
| 627 | } |
| 628 | |
| 629 | switch (block_mode_) { |
| 630 | case SYNCHRONOUS: |
| 631 | EXPECT_NE(OK, retval_); |
| 632 | return retval_; |
| 633 | |
| 634 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 635 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 636 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 637 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 638 | return ERR_IO_PENDING; |
| 639 | |
| 640 | case USER_CALLBACK: |
| 641 | callback_ = callback; |
| 642 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 643 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 644 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 645 | return ERR_IO_PENDING; |
| 646 | } |
| 647 | NOTREACHED(); |
| 648 | return 0; |
| 649 | } |
| 650 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 651 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 652 | public: |
| 653 | // Does not own |delegate|. |
| 654 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 655 | NetworkDelegate* delegate) |
| 656 | : TestURLRequestContext(true) { |
| 657 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 658 | set_network_delegate(delegate); |
| 659 | Init(); |
| 660 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 661 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 662 | }; |
| 663 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 664 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 665 | // URI and report to be sent. |
| 666 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 667 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 668 | public: |
| 669 | MockCertificateReportSender() {} |
| 670 | ~MockCertificateReportSender() override {} |
| 671 | |
| 672 | void Send(const GURL& report_uri, const std::string& report) override { |
| 673 | latest_report_uri_ = report_uri; |
| 674 | latest_report_ = report; |
| 675 | } |
| 676 | |
estark | f297912 | 2016-04-05 17:04:39 | [diff] [blame] | 677 | void SetErrorCallback( |
| 678 | const base::Callback<void(const GURL&, int)>& error_callback) override {} |
| 679 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 680 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 681 | const std::string& latest_report() { return latest_report_; } |
| 682 | |
| 683 | private: |
| 684 | GURL latest_report_uri_; |
| 685 | std::string latest_report_; |
| 686 | }; |
| 687 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 688 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 689 | public: |
| 690 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 691 | bool OnAreStrictSecureCookiesEnabled() const override { return true; } |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 692 | }; |
| 693 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 694 | } // namespace |
| 695 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 696 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 697 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 698 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 699 | URLRequestTest() : default_context_(true) { |
| 700 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 701 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 702 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 703 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 704 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 705 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 706 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 707 | // URLRequestJobs may post clean-up tasks on destruction. |
| 708 | base::RunLoop().RunUntilIdle(); |
| 709 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 710 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 711 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 712 | SetUpFactory(); |
| 713 | default_context_.set_job_factory(job_factory_.get()); |
| 714 | default_context_.Init(); |
| 715 | PlatformTest::SetUp(); |
| 716 | } |
| 717 | |
| 718 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 719 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 720 | "data", base::WrapUnique(new DataProtocolHandler)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 721 | #if !defined(DISABLE_FILE_SUPPORT) |
| 722 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 723 | "file", base::WrapUnique(new FileProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 724 | base::ThreadTaskRunnerHandle::Get()))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 725 | #endif |
| 726 | } |
| 727 | |
| 728 | TestNetworkDelegate* default_network_delegate() { |
| 729 | return &default_network_delegate_; |
| 730 | } |
| 731 | |
| 732 | const TestURLRequestContext& default_context() const { |
| 733 | return default_context_; |
| 734 | } |
| 735 | |
| 736 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 737 | // Adds the TestJobInterceptor to the default context. |
| 738 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 739 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 740 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 741 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 742 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 743 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 744 | } |
| 745 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 746 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 747 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 748 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 749 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 750 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 751 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 752 | }; |
| 753 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 754 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 755 | TestDelegate d; |
| 756 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 757 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 758 | GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 760 | r->Start(); |
| 761 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 762 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 763 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 765 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 766 | EXPECT_FALSE(d.received_data_before_response()); |
| 767 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 768 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 769 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 770 | |
| 771 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 772 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| 776 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 777 | TestDelegate d; |
| 778 | { |
| 779 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 780 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 781 | GURL("data:image/png;base64," |
| 782 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 783 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 784 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 785 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 786 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 787 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 788 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 789 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 790 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 791 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 792 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 793 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 794 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 795 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 796 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 797 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 798 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 799 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 800 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 801 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 802 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 803 | r->Start(); |
| 804 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 805 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 806 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 807 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 808 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 809 | EXPECT_FALSE(d.received_data_before_response()); |
| 810 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 811 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 812 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 813 | |
| 814 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 815 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 819 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 820 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 821 | base::FilePath app_path; |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 822 | |
| 823 | #if defined(OS_ANDROID) |
| 824 | // Android devices are not guaranteed to be able to read /proc/self/exe |
| 825 | // Use /etc/hosts instead |
| 826 | app_path = base::FilePath("/etc/hosts"); |
| 827 | #else |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 828 | PathService::Get(base::FILE_EXE, &app_path); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 829 | #endif // OS_ANDROID |
| 830 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 831 | GURL app_url = FilePathToFileURL(app_path); |
| 832 | |
| 833 | TestDelegate d; |
| 834 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 835 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 836 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 837 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 838 | r->Start(); |
| 839 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 840 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 841 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 842 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 843 | int64_t file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 844 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 845 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 846 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 847 | EXPECT_EQ(1, d.response_started_count()); |
| 848 | EXPECT_FALSE(d.received_data_before_response()); |
| 849 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 850 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 851 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 852 | |
| 853 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 854 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 858 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 859 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 860 | PathService::Get(base::FILE_EXE, &app_path); |
| 861 | GURL app_url = FilePathToFileURL(app_path); |
| 862 | |
| 863 | TestDelegate d; |
| 864 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 865 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 866 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 867 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 868 | r->Start(); |
| 869 | EXPECT_TRUE(r->is_pending()); |
| 870 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 871 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 872 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 873 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 874 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 875 | } |
| 876 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 877 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 878 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 879 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 880 | FillBuffer(buffer.get(), buffer_size); |
| 881 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 882 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 883 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 884 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 885 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 886 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 887 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 888 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 889 | |
| 890 | const size_t first_byte_position = 500; |
| 891 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 892 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 893 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 894 | buffer.get() + last_byte_position + 1); |
| 895 | |
| 896 | TestDelegate d; |
| 897 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 898 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 899 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 900 | |
| 901 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 902 | headers.SetHeader( |
| 903 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 904 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 905 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 906 | r->SetExtraRequestHeaders(headers); |
| 907 | r->Start(); |
| 908 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 909 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 910 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 911 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 912 | EXPECT_EQ(1, d.response_started_count()); |
| 913 | EXPECT_FALSE(d.received_data_before_response()); |
| 914 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 915 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 916 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 917 | } |
| 918 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 919 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 923 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 924 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | FillBuffer(buffer.get(), buffer_size); |
| 926 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 927 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 928 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 929 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 930 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 931 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 932 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 933 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 934 | |
| 935 | const size_t first_byte_position = 500; |
| 936 | const size_t last_byte_position = buffer_size - 1; |
| 937 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 938 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 939 | buffer.get() + last_byte_position + 1); |
| 940 | |
| 941 | TestDelegate d; |
| 942 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 943 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 944 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 945 | |
| 946 | HttpRequestHeaders headers; |
| 947 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 948 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 949 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 950 | r->SetExtraRequestHeaders(headers); |
| 951 | r->Start(); |
| 952 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 953 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 954 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 955 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 956 | EXPECT_EQ(1, d.response_started_count()); |
| 957 | EXPECT_FALSE(d.received_data_before_response()); |
| 958 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 959 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 960 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 961 | } |
| 962 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 963 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 967 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 968 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 969 | FillBuffer(buffer.get(), buffer_size); |
| 970 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 971 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 972 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 974 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 976 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 977 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 978 | |
| 979 | TestDelegate d; |
| 980 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 981 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 982 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 983 | |
| 984 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 985 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 986 | r->SetExtraRequestHeaders(headers); |
| 987 | r->Start(); |
| 988 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 989 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 990 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 991 | EXPECT_TRUE(d.request_failed()); |
| 992 | } |
| 993 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 994 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 995 | } |
| 996 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 997 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 998 | base::ScopedTempDir temp_dir; |
| 999 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1000 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1001 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1002 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1003 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1004 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1005 | |
| 1006 | { |
| 1007 | TestDelegate d; |
| 1008 | TestNetworkDelegate network_delegate; |
| 1009 | network_delegate.set_can_access_files(true); |
| 1010 | default_context_.set_network_delegate(&network_delegate); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1011 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1012 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1013 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1014 | base::RunLoop().Run(); |
| 1015 | EXPECT_FALSE(d.request_failed()); |
| 1016 | EXPECT_EQ(test_data, d.data_received()); |
| 1017 | } |
| 1018 | |
| 1019 | { |
| 1020 | TestDelegate d; |
| 1021 | TestNetworkDelegate network_delegate; |
| 1022 | network_delegate.set_can_access_files(false); |
| 1023 | default_context_.set_network_delegate(&network_delegate); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1024 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1025 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1026 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1027 | base::RunLoop().Run(); |
| 1028 | EXPECT_TRUE(d.request_failed()); |
| 1029 | EXPECT_EQ("", d.data_received()); |
| 1030 | } |
| 1031 | } |
| 1032 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1033 | |
| 1034 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1035 | // Put in mock resource provider. |
| 1036 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1037 | |
| 1038 | TestDelegate d; |
| 1039 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1040 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1041 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1042 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1043 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1044 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1045 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1046 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1047 | req->Start(); |
| 1048 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1049 | |
| 1050 | d.set_cancel_in_received_data_pending(true); |
| 1051 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1052 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | // Take out mock resource provider. |
| 1056 | NetModule::SetResourceProvider(NULL); |
| 1057 | } |
| 1058 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1059 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1060 | // Verify the general sanity of the the output of the file: |
| 1061 | // directory lister by checking for the output of a known existing |
| 1062 | // file. |
| 1063 | const char sentinel_name[] = "filedir-sentinel"; |
| 1064 | |
| 1065 | base::FilePath path; |
| 1066 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1067 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1068 | |
| 1069 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1070 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1071 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1072 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1073 | base::RunLoop().Run(); |
| 1074 | |
| 1075 | // Generate entry for the sentinel file. |
| 1076 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1077 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1078 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1079 | EXPECT_GT(info.size, 0); |
| 1080 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1081 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1082 | std::string(sentinel_name), |
| 1083 | false /* is_dir */, |
| 1084 | info.size, |
| 1085 | info.last_modified); |
| 1086 | |
| 1087 | ASSERT_LT(0, d.bytes_received()); |
| 1088 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1089 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1090 | // Check for the entry generated for the "sentinel" file. |
| 1091 | const std::string& data = d.data_received(); |
| 1092 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1093 | } |
| 1094 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1095 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1096 | // There is an implicit redirect when loading a file path that matches a |
| 1097 | // directory and does not end with a slash. Ensure that following such |
| 1098 | // redirects does not crash. See http://crbug.com/18686. |
| 1099 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1100 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1101 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1102 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1103 | |
| 1104 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1105 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1106 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1107 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1108 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1109 | |
| 1110 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1111 | ASSERT_LT(0, d.bytes_received()); |
| 1112 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1113 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | #if defined(OS_WIN) |
| 1117 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1118 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1119 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1120 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1121 | default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1122 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1123 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1124 | |
| 1125 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1126 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1127 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1128 | #endif // defined(OS_WIN) |
| 1129 | |
| 1130 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1131 | |
| 1132 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1133 | TestDelegate d; |
| 1134 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1135 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1136 | GURL("invalid url"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1137 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1138 | r->Start(); |
| 1139 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1140 | |
| 1141 | base::RunLoop().Run(); |
| 1142 | EXPECT_TRUE(d.request_failed()); |
| 1143 | } |
| 1144 | } |
| 1145 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1146 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1147 | TestURLRequestContext context; |
| 1148 | TestNetworkDelegate network_delegate; |
| 1149 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1150 | context.set_network_delegate(&network_delegate); |
| 1151 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1152 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1153 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1154 | req->SetReferrer("https://somewhere.com/"); |
| 1155 | |
| 1156 | req->Start(); |
| 1157 | base::RunLoop().Run(); |
| 1158 | EXPECT_TRUE(d.request_failed()); |
| 1159 | } |
| 1160 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1161 | #if defined(OS_WIN) |
| 1162 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1163 | base::FilePath app_path; |
| 1164 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1165 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1166 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1167 | |
| 1168 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1169 | |
| 1170 | base::win::ScopedCOMInitializer com_initializer; |
| 1171 | |
| 1172 | // Temporarily create a shortcut for test |
| 1173 | { |
| 1174 | base::win::ScopedComPtr<IShellLink> shell; |
| 1175 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1176 | CLSCTX_INPROC_SERVER))); |
| 1177 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1178 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1179 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1180 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1181 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1182 | } |
| 1183 | |
| 1184 | TestDelegate d; |
| 1185 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1186 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1187 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1188 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1189 | r->Start(); |
| 1190 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1191 | |
| 1192 | base::RunLoop().Run(); |
| 1193 | |
| 1194 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1195 | GetFileAttributesEx(app_path.value().c_str(), |
| 1196 | GetFileExInfoStandard, &data); |
| 1197 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1198 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1199 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1200 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1201 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1202 | DWORD read_size; |
| 1203 | BOOL result; |
| 1204 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1205 | &read_size, NULL); |
| 1206 | std::string content(buffer.get(), read_size); |
| 1207 | CloseHandle(file); |
| 1208 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1209 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1210 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1211 | EXPECT_EQ(content, d.data_received()); |
| 1212 | } |
| 1213 | |
| 1214 | // Clean the shortcut |
| 1215 | DeleteFile(lnk_path.c_str()); |
| 1216 | } |
| 1217 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1218 | |
| 1219 | // Custom URLRequestJobs for use with interceptor tests |
| 1220 | class RestartTestJob : public URLRequestTestJob { |
| 1221 | public: |
| 1222 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1223 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1224 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1225 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1226 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1227 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1228 | }; |
| 1229 | |
| 1230 | class CancelTestJob : public URLRequestTestJob { |
| 1231 | public: |
| 1232 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1233 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1234 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1235 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1236 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1237 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1238 | }; |
| 1239 | |
| 1240 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1241 | public: |
| 1242 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1243 | NetworkDelegate* network_delegate) |
| 1244 | : URLRequestTestJob(request, network_delegate, true) { |
| 1245 | } |
| 1246 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1247 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1248 | request_->Cancel(); |
| 1249 | this->NotifyRestartRequired(); |
| 1250 | } |
| 1251 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1252 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1253 | }; |
| 1254 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1255 | // An Interceptor for use with interceptor tests. |
| 1256 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1257 | public: |
| 1258 | // Static getters for canned response header and data strings. |
| 1259 | static std::string ok_data() { |
| 1260 | return URLRequestTestJob::test_data_1(); |
| 1261 | } |
| 1262 | |
| 1263 | static std::string ok_headers() { |
| 1264 | return URLRequestTestJob::test_headers(); |
| 1265 | } |
| 1266 | |
| 1267 | static std::string redirect_data() { |
| 1268 | return std::string(); |
| 1269 | } |
| 1270 | |
| 1271 | static std::string redirect_headers() { |
| 1272 | return URLRequestTestJob::test_redirect_headers(); |
| 1273 | } |
| 1274 | |
| 1275 | static std::string error_data() { |
| 1276 | return std::string("ohhh nooooo mr. bill!"); |
| 1277 | } |
| 1278 | |
| 1279 | static std::string error_headers() { |
| 1280 | return URLRequestTestJob::test_error_headers(); |
| 1281 | } |
| 1282 | |
| 1283 | MockURLRequestInterceptor() |
| 1284 | : intercept_main_request_(false), restart_main_request_(false), |
| 1285 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1286 | simulate_main_network_error_(false), |
| 1287 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1288 | intercept_final_response_(false), cancel_final_request_(false), |
| 1289 | use_url_request_http_job_(false), |
| 1290 | did_intercept_main_(false), did_restart_main_(false), |
| 1291 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1292 | did_simulate_error_main_(false), |
| 1293 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1294 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1295 | } |
| 1296 | |
| 1297 | ~MockURLRequestInterceptor() override { |
| 1298 | } |
| 1299 | |
| 1300 | // URLRequestInterceptor implementation: |
| 1301 | URLRequestJob* MaybeInterceptRequest( |
| 1302 | URLRequest* request, |
| 1303 | NetworkDelegate* network_delegate) const override { |
| 1304 | if (restart_main_request_) { |
| 1305 | restart_main_request_ = false; |
| 1306 | did_restart_main_ = true; |
| 1307 | return new RestartTestJob(request, network_delegate); |
| 1308 | } |
| 1309 | if (cancel_main_request_) { |
| 1310 | cancel_main_request_ = false; |
| 1311 | did_cancel_main_ = true; |
| 1312 | return new CancelTestJob(request, network_delegate); |
| 1313 | } |
| 1314 | if (cancel_then_restart_main_request_) { |
| 1315 | cancel_then_restart_main_request_ = false; |
| 1316 | did_cancel_then_restart_main_ = true; |
| 1317 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1318 | } |
| 1319 | if (simulate_main_network_error_) { |
| 1320 | simulate_main_network_error_ = false; |
| 1321 | did_simulate_error_main_ = true; |
| 1322 | if (use_url_request_http_job_) { |
| 1323 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1324 | } |
| 1325 | // This job will result in error since the requested URL is not one of the |
| 1326 | // URLs supported by these tests. |
| 1327 | return new URLRequestTestJob(request, network_delegate, true); |
| 1328 | } |
| 1329 | if (!intercept_main_request_) |
| 1330 | return nullptr; |
| 1331 | intercept_main_request_ = false; |
| 1332 | did_intercept_main_ = true; |
| 1333 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1334 | network_delegate, |
| 1335 | main_headers_, |
| 1336 | main_data_, |
| 1337 | true); |
| 1338 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1339 | return job; |
| 1340 | } |
| 1341 | |
| 1342 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1343 | NetworkDelegate* network_delegate, |
| 1344 | const GURL& location) const override { |
| 1345 | if (cancel_redirect_request_) { |
| 1346 | cancel_redirect_request_ = false; |
| 1347 | did_cancel_redirect_ = true; |
| 1348 | return new CancelTestJob(request, network_delegate); |
| 1349 | } |
| 1350 | if (!intercept_redirect_) |
| 1351 | return nullptr; |
| 1352 | intercept_redirect_ = false; |
| 1353 | did_intercept_redirect_ = true; |
| 1354 | if (use_url_request_http_job_) { |
| 1355 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1356 | } |
| 1357 | return new URLRequestTestJob(request, |
| 1358 | network_delegate, |
| 1359 | redirect_headers_, |
| 1360 | redirect_data_, |
| 1361 | true); |
| 1362 | } |
| 1363 | |
| 1364 | URLRequestJob* MaybeInterceptResponse( |
| 1365 | URLRequest* request, |
| 1366 | NetworkDelegate* network_delegate) const override { |
| 1367 | if (cancel_final_request_) { |
| 1368 | cancel_final_request_ = false; |
| 1369 | did_cancel_final_ = true; |
| 1370 | return new CancelTestJob(request, network_delegate); |
| 1371 | } |
| 1372 | if (!intercept_final_response_) |
| 1373 | return nullptr; |
| 1374 | intercept_final_response_ = false; |
| 1375 | did_intercept_final_ = true; |
| 1376 | if (use_url_request_http_job_) { |
| 1377 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1378 | } |
| 1379 | return new URLRequestTestJob(request, |
| 1380 | network_delegate, |
| 1381 | final_headers_, |
| 1382 | final_data_, |
| 1383 | true); |
| 1384 | } |
| 1385 | |
| 1386 | void set_intercept_main_request(bool intercept_main_request) { |
| 1387 | intercept_main_request_ = intercept_main_request; |
| 1388 | } |
| 1389 | |
| 1390 | void set_main_headers(const std::string& main_headers) { |
| 1391 | main_headers_ = main_headers; |
| 1392 | } |
| 1393 | |
| 1394 | void set_main_data(const std::string& main_data) { |
| 1395 | main_data_ = main_data; |
| 1396 | } |
| 1397 | |
| 1398 | void set_main_request_load_timing_info( |
| 1399 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1400 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1401 | } |
| 1402 | |
| 1403 | void set_restart_main_request(bool restart_main_request) { |
| 1404 | restart_main_request_ = restart_main_request; |
| 1405 | } |
| 1406 | |
| 1407 | void set_cancel_main_request(bool cancel_main_request) { |
| 1408 | cancel_main_request_ = cancel_main_request; |
| 1409 | } |
| 1410 | |
| 1411 | void set_cancel_then_restart_main_request( |
| 1412 | bool cancel_then_restart_main_request) { |
| 1413 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1414 | } |
| 1415 | |
| 1416 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1417 | simulate_main_network_error_ = simulate_main_network_error; |
| 1418 | } |
| 1419 | |
| 1420 | void set_intercept_redirect(bool intercept_redirect) { |
| 1421 | intercept_redirect_ = intercept_redirect; |
| 1422 | } |
| 1423 | |
| 1424 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1425 | redirect_headers_ = redirect_headers; |
| 1426 | } |
| 1427 | |
| 1428 | void set_redirect_data(const std::string& redirect_data) { |
| 1429 | redirect_data_ = redirect_data; |
| 1430 | } |
| 1431 | |
| 1432 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1433 | cancel_redirect_request_ = cancel_redirect_request; |
| 1434 | } |
| 1435 | |
| 1436 | void set_intercept_final_response(bool intercept_final_response) { |
| 1437 | intercept_final_response_ = intercept_final_response; |
| 1438 | } |
| 1439 | |
| 1440 | void set_final_headers(const std::string& final_headers) { |
| 1441 | final_headers_ = final_headers; |
| 1442 | } |
| 1443 | |
| 1444 | void set_final_data(const std::string& final_data) { |
| 1445 | final_data_ = final_data; |
| 1446 | } |
| 1447 | |
| 1448 | void set_cancel_final_request(bool cancel_final_request) { |
| 1449 | cancel_final_request_ = cancel_final_request; |
| 1450 | } |
| 1451 | |
| 1452 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1453 | use_url_request_http_job_ = use_url_request_http_job; |
| 1454 | } |
| 1455 | |
| 1456 | bool did_intercept_main() const { |
| 1457 | return did_intercept_main_; |
| 1458 | } |
| 1459 | |
| 1460 | bool did_restart_main() const { |
| 1461 | return did_restart_main_; |
| 1462 | } |
| 1463 | |
| 1464 | bool did_cancel_main() const { |
| 1465 | return did_cancel_main_; |
| 1466 | } |
| 1467 | |
| 1468 | bool did_cancel_then_restart_main() const { |
| 1469 | return did_cancel_then_restart_main_; |
| 1470 | } |
| 1471 | |
| 1472 | bool did_simulate_error_main() const { |
| 1473 | return did_simulate_error_main_; |
| 1474 | } |
| 1475 | |
| 1476 | bool did_intercept_redirect() const { |
| 1477 | return did_intercept_redirect_; |
| 1478 | } |
| 1479 | |
| 1480 | bool did_cancel_redirect() const { |
| 1481 | return did_cancel_redirect_; |
| 1482 | } |
| 1483 | |
| 1484 | bool did_intercept_final() const { |
| 1485 | return did_intercept_final_; |
| 1486 | } |
| 1487 | |
| 1488 | bool did_cancel_final() const { |
| 1489 | return did_cancel_final_; |
| 1490 | } |
| 1491 | |
| 1492 | private: |
| 1493 | // Indicate whether to intercept the main request, and if so specify the |
| 1494 | // response to return and the LoadTimingInfo to use. |
| 1495 | mutable bool intercept_main_request_; |
| 1496 | mutable std::string main_headers_; |
| 1497 | mutable std::string main_data_; |
| 1498 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1499 | |
| 1500 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1501 | mutable bool restart_main_request_; |
| 1502 | mutable bool cancel_main_request_; |
| 1503 | mutable bool cancel_then_restart_main_request_; |
| 1504 | mutable bool simulate_main_network_error_; |
| 1505 | |
| 1506 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1507 | // return. |
| 1508 | mutable bool intercept_redirect_; |
| 1509 | mutable std::string redirect_headers_; |
| 1510 | mutable std::string redirect_data_; |
| 1511 | |
| 1512 | // Cancel the request within MaybeInterceptRedirect. |
| 1513 | mutable bool cancel_redirect_request_; |
| 1514 | |
| 1515 | // Indicate whether to intercept the final response, and if so specify the |
| 1516 | // response to return. |
| 1517 | mutable bool intercept_final_response_; |
| 1518 | mutable std::string final_headers_; |
| 1519 | mutable std::string final_data_; |
| 1520 | |
| 1521 | // Cancel the final request within MaybeInterceptResponse. |
| 1522 | mutable bool cancel_final_request_; |
| 1523 | |
| 1524 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1525 | mutable bool use_url_request_http_job_; |
| 1526 | |
| 1527 | // These indicate if the interceptor did something or not. |
| 1528 | mutable bool did_intercept_main_; |
| 1529 | mutable bool did_restart_main_; |
| 1530 | mutable bool did_cancel_main_; |
| 1531 | mutable bool did_cancel_then_restart_main_; |
| 1532 | mutable bool did_simulate_error_main_; |
| 1533 | mutable bool did_intercept_redirect_; |
| 1534 | mutable bool did_cancel_redirect_; |
| 1535 | mutable bool did_intercept_final_; |
| 1536 | mutable bool did_cancel_final_; |
| 1537 | }; |
| 1538 | |
| 1539 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1540 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1541 | public: |
| 1542 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1543 | } |
| 1544 | |
| 1545 | ~URLRequestInterceptorTest() override { |
| 1546 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1547 | base::RunLoop().RunUntilIdle(); |
| 1548 | } |
| 1549 | |
| 1550 | void SetUpFactory() override { |
| 1551 | interceptor_ = new MockURLRequestInterceptor(); |
| 1552 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1553 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | MockURLRequestInterceptor* interceptor() const { |
| 1557 | return interceptor_; |
| 1558 | } |
| 1559 | |
| 1560 | private: |
| 1561 | MockURLRequestInterceptor* interceptor_; |
| 1562 | }; |
| 1563 | |
| 1564 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1565 | // Intercept the main request and respond with a simple response. |
| 1566 | interceptor()->set_intercept_main_request(true); |
| 1567 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1568 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1569 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1570 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1571 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1572 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1573 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1574 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1575 | req->SetUserData(nullptr, user_data0); |
| 1576 | req->SetUserData(&user_data1, user_data1); |
| 1577 | req->SetUserData(&user_data2, user_data2); |
| 1578 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1579 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1580 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1581 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1582 | // Make sure we can retrieve our specific user data. |
| 1583 | EXPECT_EQ(user_data0, req->GetUserData(nullptr)); |
| 1584 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1585 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1586 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1587 | // Check that we got one good response. |
| 1588 | EXPECT_TRUE(req->status().is_success()); |
| 1589 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1590 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1591 | EXPECT_EQ(1, d.response_started_count()); |
| 1592 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1593 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1594 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1595 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1596 | // Intercept the main request and respond with a redirect. |
| 1597 | interceptor()->set_intercept_main_request(true); |
| 1598 | interceptor()->set_main_headers( |
| 1599 | MockURLRequestInterceptor::redirect_headers()); |
| 1600 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1601 | |
| 1602 | // Intercept that redirect and respond with a final OK response. |
| 1603 | interceptor()->set_intercept_redirect(true); |
| 1604 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1605 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1606 | |
| 1607 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1608 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1609 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1610 | req->set_method("GET"); |
| 1611 | req->Start(); |
| 1612 | base::RunLoop().Run(); |
| 1613 | |
| 1614 | // Check that the interceptor got called as expected. |
| 1615 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1616 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1617 | |
| 1618 | // Check that we got one good response. |
| 1619 | EXPECT_TRUE(req->status().is_success()); |
| 1620 | if (req->status().is_success()) |
| 1621 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1622 | |
| 1623 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1624 | EXPECT_EQ(1, d.response_started_count()); |
| 1625 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1626 | } |
| 1627 | |
| 1628 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1629 | // Intercept the main request to generate a server error response. |
| 1630 | interceptor()->set_intercept_main_request(true); |
| 1631 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1632 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1633 | |
| 1634 | // Intercept that error and respond with an OK response. |
| 1635 | interceptor()->set_intercept_final_response(true); |
| 1636 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1637 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1638 | |
| 1639 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1640 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1641 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1642 | req->set_method("GET"); |
| 1643 | req->Start(); |
| 1644 | base::RunLoop().Run(); |
| 1645 | |
| 1646 | // Check that the interceptor got called as expected. |
| 1647 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1648 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1649 | |
| 1650 | // Check that we got one good response. |
| 1651 | EXPECT_TRUE(req->status().is_success()); |
| 1652 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1653 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1654 | EXPECT_EQ(1, d.response_started_count()); |
| 1655 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1656 | } |
| 1657 | |
| 1658 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1659 | // Intercept the main request to simulate a network error. |
| 1660 | interceptor()->set_simulate_main_network_error(true); |
| 1661 | |
| 1662 | // Intercept that error and respond with an OK response. |
| 1663 | interceptor()->set_intercept_final_response(true); |
| 1664 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1665 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1666 | |
| 1667 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1668 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1669 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1670 | req->set_method("GET"); |
| 1671 | req->Start(); |
| 1672 | base::RunLoop().Run(); |
| 1673 | |
| 1674 | // Check that the interceptor got called as expected. |
| 1675 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1676 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1677 | |
| 1678 | // Check that we received one good response. |
| 1679 | EXPECT_TRUE(req->status().is_success()); |
| 1680 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1681 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1682 | EXPECT_EQ(1, d.response_started_count()); |
| 1683 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1684 | } |
| 1685 | |
| 1686 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1687 | // Restart the main request. |
| 1688 | interceptor()->set_restart_main_request(true); |
| 1689 | |
| 1690 | // then intercept the new main request and respond with an OK response |
| 1691 | interceptor()->set_intercept_main_request(true); |
| 1692 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1693 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1694 | |
| 1695 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1696 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1697 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1698 | req->set_method("GET"); |
| 1699 | req->Start(); |
| 1700 | base::RunLoop().Run(); |
| 1701 | |
| 1702 | // Check that the interceptor got called as expected. |
| 1703 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1704 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1705 | |
| 1706 | // Check that we received one good response. |
| 1707 | EXPECT_TRUE(req->status().is_success()); |
| 1708 | if (req->status().is_success()) |
| 1709 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1710 | |
| 1711 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1712 | EXPECT_EQ(1, d.response_started_count()); |
| 1713 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1714 | } |
| 1715 | |
| 1716 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1717 | // Intercept the main request and cancel from within the restarted job. |
| 1718 | interceptor()->set_cancel_main_request(true); |
| 1719 | |
| 1720 | // Set up to intercept the final response and override it with an OK response. |
| 1721 | interceptor()->set_intercept_final_response(true); |
| 1722 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1723 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1724 | |
| 1725 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1726 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1727 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1728 | req->set_method("GET"); |
| 1729 | req->Start(); |
| 1730 | base::RunLoop().Run(); |
| 1731 | |
| 1732 | // Check that the interceptor got called as expected. |
| 1733 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1734 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1735 | |
| 1736 | // Check that we see a canceled request. |
| 1737 | EXPECT_FALSE(req->status().is_success()); |
| 1738 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1739 | } |
| 1740 | |
| 1741 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1742 | // Intercept the main request and respond with a redirect. |
| 1743 | interceptor()->set_intercept_main_request(true); |
| 1744 | interceptor()->set_main_headers( |
| 1745 | MockURLRequestInterceptor::redirect_headers()); |
| 1746 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1747 | |
| 1748 | // Intercept the redirect and cancel from within that job. |
| 1749 | interceptor()->set_cancel_redirect_request(true); |
| 1750 | |
| 1751 | // Set up to intercept the final response and override it with an OK response. |
| 1752 | interceptor()->set_intercept_final_response(true); |
| 1753 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1754 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1755 | |
| 1756 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1757 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1758 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1759 | req->set_method("GET"); |
| 1760 | req->Start(); |
| 1761 | base::RunLoop().Run(); |
| 1762 | |
| 1763 | // Check that the interceptor got called as expected. |
| 1764 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1765 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 1766 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1767 | |
| 1768 | // Check that we see a canceled request. |
| 1769 | EXPECT_FALSE(req->status().is_success()); |
| 1770 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1771 | } |
| 1772 | |
| 1773 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 1774 | // Intercept the main request to simulate a network error. |
| 1775 | interceptor()->set_simulate_main_network_error(true); |
| 1776 | |
| 1777 | // Set up to intercept final the response and cancel from within that job. |
| 1778 | interceptor()->set_cancel_final_request(true); |
| 1779 | |
| 1780 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1781 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1782 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1783 | req->set_method("GET"); |
| 1784 | req->Start(); |
| 1785 | base::RunLoop().Run(); |
| 1786 | |
| 1787 | // Check that the interceptor got called as expected. |
| 1788 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1789 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 1790 | |
| 1791 | // Check that we see a canceled request. |
| 1792 | EXPECT_FALSE(req->status().is_success()); |
| 1793 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1794 | } |
| 1795 | |
| 1796 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 1797 | // Intercept the main request and cancel then restart from within that job. |
| 1798 | interceptor()->set_cancel_then_restart_main_request(true); |
| 1799 | |
| 1800 | // Set up to intercept the final response and override it with an OK response. |
| 1801 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 1802 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1803 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1804 | |
| 1805 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1806 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1807 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1808 | req->set_method("GET"); |
| 1809 | req->Start(); |
| 1810 | base::RunLoop().Run(); |
| 1811 | |
| 1812 | // Check that the interceptor got called as expected. |
| 1813 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 1814 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1815 | |
| 1816 | // Check that we see a canceled request. |
| 1817 | EXPECT_FALSE(req->status().is_success()); |
| 1818 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1822 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1823 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1824 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1825 | int connect_time_flags, |
| 1826 | bool used_proxy) { |
| 1827 | LoadTimingInfo load_timing; |
| 1828 | load_timing.socket_log_id = 1; |
| 1829 | |
| 1830 | if (used_proxy) { |
| 1831 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1832 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1833 | } |
| 1834 | |
| 1835 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1836 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1837 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1838 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1839 | } |
| 1840 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1841 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1842 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1843 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1844 | } |
| 1845 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1846 | |
| 1847 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1848 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1849 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1850 | return load_timing; |
| 1851 | } |
| 1852 | |
| 1853 | // Same as above, but in the case of a reused socket. |
| 1854 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1855 | bool used_proxy) { |
| 1856 | LoadTimingInfo load_timing; |
| 1857 | load_timing.socket_log_id = 1; |
| 1858 | load_timing.socket_reused = true; |
| 1859 | |
| 1860 | if (used_proxy) { |
| 1861 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1862 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1863 | } |
| 1864 | |
| 1865 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1866 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1867 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1868 | return load_timing; |
| 1869 | } |
| 1870 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1871 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 1872 | const LoadTimingInfo& job_load_timing, |
| 1873 | const URLRequestContext& context, |
| 1874 | MockURLRequestInterceptor* interceptor) { |
| 1875 | interceptor->set_intercept_main_request(true); |
| 1876 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 1877 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1878 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1879 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1880 | req->Start(); |
| 1881 | base::RunLoop().Run(); |
| 1882 | |
| 1883 | LoadTimingInfo resulting_load_timing; |
| 1884 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 1885 | |
| 1886 | // None of these should be modified by the URLRequest. |
| 1887 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1888 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1889 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1890 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1891 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1892 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 1893 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 1894 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1895 | |
| 1896 | return resulting_load_timing; |
| 1897 | } |
| 1898 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1899 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1900 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1901 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1902 | LoadTimingInfo job_load_timing = |
| 1903 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1904 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1905 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1906 | RunURLRequestInterceptorLoadTimingTest( |
| 1907 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1908 | |
| 1909 | // Nothing should have been changed by the URLRequest. |
| 1910 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1911 | load_timing_result.proxy_resolve_start); |
| 1912 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1913 | load_timing_result.proxy_resolve_end); |
| 1914 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1915 | load_timing_result.connect_timing.dns_start); |
| 1916 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1917 | load_timing_result.connect_timing.dns_end); |
| 1918 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1919 | load_timing_result.connect_timing.connect_start); |
| 1920 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1921 | load_timing_result.connect_timing.connect_end); |
| 1922 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1923 | load_timing_result.connect_timing.ssl_start); |
| 1924 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1925 | load_timing_result.connect_timing.ssl_end); |
| 1926 | |
| 1927 | // Redundant sanity check. |
| 1928 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1929 | } |
| 1930 | |
| 1931 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1932 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1933 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1934 | LoadTimingInfo job_load_timing = |
| 1935 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1936 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1937 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1938 | RunURLRequestInterceptorLoadTimingTest( |
| 1939 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1940 | |
| 1941 | // Nothing should have been changed by the URLRequest. |
| 1942 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1943 | load_timing_result.proxy_resolve_start); |
| 1944 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1945 | load_timing_result.proxy_resolve_end); |
| 1946 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1947 | load_timing_result.connect_timing.dns_start); |
| 1948 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1949 | load_timing_result.connect_timing.dns_end); |
| 1950 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1951 | load_timing_result.connect_timing.connect_start); |
| 1952 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1953 | load_timing_result.connect_timing.connect_end); |
| 1954 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1955 | load_timing_result.connect_timing.ssl_start); |
| 1956 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1957 | load_timing_result.connect_timing.ssl_end); |
| 1958 | |
| 1959 | // Redundant sanity check. |
| 1960 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1961 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1962 | } |
| 1963 | |
| 1964 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1965 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1966 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1967 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1968 | // |
| 1969 | // 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] | 1970 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1971 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1972 | LoadTimingInfo job_load_timing = |
| 1973 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1974 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1975 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1976 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1977 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1978 | job_load_timing.connect_timing.connect_start = |
| 1979 | now - base::TimeDelta::FromDays(2); |
| 1980 | job_load_timing.connect_timing.connect_end = |
| 1981 | now - base::TimeDelta::FromDays(1); |
| 1982 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1983 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1984 | RunURLRequestInterceptorLoadTimingTest( |
| 1985 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1986 | |
| 1987 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1988 | // request_start. |
| 1989 | EXPECT_EQ(load_timing_result.request_start, |
| 1990 | load_timing_result.proxy_resolve_start); |
| 1991 | EXPECT_EQ(load_timing_result.request_start, |
| 1992 | load_timing_result.proxy_resolve_end); |
| 1993 | EXPECT_EQ(load_timing_result.request_start, |
| 1994 | load_timing_result.connect_timing.dns_start); |
| 1995 | EXPECT_EQ(load_timing_result.request_start, |
| 1996 | load_timing_result.connect_timing.dns_end); |
| 1997 | EXPECT_EQ(load_timing_result.request_start, |
| 1998 | load_timing_result.connect_timing.connect_start); |
| 1999 | EXPECT_EQ(load_timing_result.request_start, |
| 2000 | load_timing_result.connect_timing.connect_end); |
| 2001 | |
| 2002 | // Other times should have been left null. |
| 2003 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2004 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2005 | } |
| 2006 | |
| 2007 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2008 | TEST_F(URLRequestInterceptorTest, |
| 2009 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2010 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2011 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2012 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2013 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2014 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2015 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2016 | RunURLRequestInterceptorLoadTimingTest( |
| 2017 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2018 | |
| 2019 | // Proxy times and connect times should all be replaced with request_start. |
| 2020 | EXPECT_EQ(load_timing_result.request_start, |
| 2021 | load_timing_result.proxy_resolve_start); |
| 2022 | EXPECT_EQ(load_timing_result.request_start, |
| 2023 | load_timing_result.proxy_resolve_end); |
| 2024 | |
| 2025 | // Other times should have been left null. |
| 2026 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2027 | } |
| 2028 | |
| 2029 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2030 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2031 | // not considered reused in this test (May be a preconnect). |
| 2032 | // |
| 2033 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2034 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2035 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2036 | LoadTimingInfo job_load_timing = |
| 2037 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2038 | job_load_timing.connect_timing.connect_start = |
| 2039 | now - base::TimeDelta::FromDays(1); |
| 2040 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2041 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2042 | job_load_timing.connect_timing.connect_end = |
| 2043 | now - base::TimeDelta::FromDays(4); |
| 2044 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2045 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2046 | RunURLRequestInterceptorLoadTimingTest( |
| 2047 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2048 | |
| 2049 | // Connect times, and SSL times should be replaced with request_start. |
| 2050 | EXPECT_EQ(load_timing_result.request_start, |
| 2051 | load_timing_result.connect_timing.connect_start); |
| 2052 | EXPECT_EQ(load_timing_result.request_start, |
| 2053 | load_timing_result.connect_timing.ssl_start); |
| 2054 | EXPECT_EQ(load_timing_result.request_start, |
| 2055 | load_timing_result.connect_timing.ssl_end); |
| 2056 | EXPECT_EQ(load_timing_result.request_start, |
| 2057 | load_timing_result.connect_timing.connect_end); |
| 2058 | |
| 2059 | // Other times should have been left null. |
| 2060 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2061 | } |
| 2062 | |
| 2063 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2064 | // |request_start|, due to reusing a connected socket in the case that there |
| 2065 | // are also proxy times. The connected socket is not considered reused in this |
| 2066 | // test (May be a preconnect). |
| 2067 | // |
| 2068 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2069 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2070 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2071 | LoadTimingInfo job_load_timing = |
| 2072 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2073 | job_load_timing.connect_timing.connect_start = |
| 2074 | now - base::TimeDelta::FromDays(1); |
| 2075 | job_load_timing.connect_timing.connect_end = |
| 2076 | now - base::TimeDelta::FromDays(2); |
| 2077 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2078 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2079 | RunURLRequestInterceptorLoadTimingTest( |
| 2080 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2081 | |
| 2082 | // Connect times should be replaced with proxy_resolve_end. |
| 2083 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2084 | load_timing_result.connect_timing.connect_start); |
| 2085 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2086 | load_timing_result.connect_timing.connect_end); |
| 2087 | |
| 2088 | // Other times should have been left null. |
| 2089 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2090 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2091 | } |
| 2092 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2093 | // Check that two different URL requests have different identifiers. |
| 2094 | TEST_F(URLRequestTest, Identifiers) { |
| 2095 | TestDelegate d; |
| 2096 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2097 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2098 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2099 | std::unique_ptr<URLRequest> other_req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2100 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2101 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2102 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2103 | } |
| 2104 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2105 | #if defined(OS_IOS) |
| 2106 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2107 | // the network delegate. crbug.com/496743 |
| 2108 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2109 | #else |
| 2110 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2111 | #endif |
| 2112 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2113 | MockHostResolver host_resolver; |
| 2114 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2115 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2116 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2117 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2118 | |
| 2119 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2120 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2121 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2122 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2123 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2124 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2125 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2126 | |
| 2127 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2128 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2129 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2130 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 2131 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 2132 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2133 | |
| 2134 | EXPECT_EQ(1, network_delegate.error_count()); |
| 2135 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 2136 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2137 | } |
| 2138 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2139 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2140 | // content is empty. |
| 2141 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2142 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2143 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2144 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2145 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2146 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2147 | EXPECT_EQ("", d.data_received()); |
| 2148 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2149 | } |
| 2150 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2151 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2152 | // correctly, both before and after start. |
| 2153 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2154 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2155 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2156 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2157 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2158 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2159 | req->SetPriority(LOW); |
| 2160 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2161 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2162 | req->Start(); |
| 2163 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2164 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2165 | req->SetPriority(MEDIUM); |
| 2166 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2170 | // Start on it. |
| 2171 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2172 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2173 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2174 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2175 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2176 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2177 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2178 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2179 | req.get(), &default_network_delegate_, &job_priority)); |
| 2180 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2181 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2182 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2183 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2184 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2185 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2186 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | // Make sure that URLRequest passes on its priority updates to its |
| 2190 | // job. |
| 2191 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2192 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2193 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2194 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2195 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2196 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2197 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2198 | req.get(), &default_network_delegate_, &job_priority)); |
| 2199 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2200 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2201 | req->SetPriority(LOW); |
| 2202 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2203 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2204 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2205 | req->SetPriority(MEDIUM); |
| 2206 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2207 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2208 | } |
| 2209 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2210 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2211 | // is MAXIMUM_PRIORITY. |
| 2212 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2213 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2214 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2215 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2216 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2217 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2218 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2219 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2220 | req.get(), &default_network_delegate_, &job_priority)); |
| 2221 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2222 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2223 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2224 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2225 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2226 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2227 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2228 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2229 | req->Start(); |
| 2230 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2231 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2232 | } |
| 2233 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2234 | namespace { |
| 2235 | |
| 2236 | // Less verbose way of running a simple testserver for the tests below. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2237 | class LocalHttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2238 | public: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2239 | explicit LocalHttpTestServer(const base::FilePath& document_root) { |
| 2240 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2241 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2242 | |
| 2243 | LocalHttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
| 2244 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2245 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2246 | } // namespace |
| 2247 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2248 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2249 | LocalHttpTestServer test_server; |
| 2250 | ASSERT_TRUE(test_server.Start()); |
| 2251 | |
| 2252 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2253 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2254 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2255 | |
| 2256 | // Set up a cookie. |
| 2257 | { |
| 2258 | TestNetworkDelegate network_delegate; |
| 2259 | context.set_network_delegate(&network_delegate); |
| 2260 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2261 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2262 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2263 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2264 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2265 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2266 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2267 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2268 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2269 | } |
| 2270 | |
| 2271 | // Verify that the cookie is set. |
| 2272 | { |
| 2273 | TestNetworkDelegate network_delegate; |
| 2274 | context.set_network_delegate(&network_delegate); |
| 2275 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2276 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2277 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2278 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2279 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2280 | |
| 2281 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2282 | != std::string::npos); |
| 2283 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2284 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2285 | } |
| 2286 | } |
| 2287 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2288 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2289 | LocalHttpTestServer test_server; |
| 2290 | ASSERT_TRUE(test_server.Start()); |
| 2291 | |
| 2292 | // Set up a cookie. |
| 2293 | { |
| 2294 | TestNetworkDelegate network_delegate; |
| 2295 | default_context_.set_network_delegate(&network_delegate); |
| 2296 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2297 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2298 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2299 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2300 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2301 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2302 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2303 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2304 | } |
| 2305 | |
| 2306 | // Verify that the cookie is set. |
| 2307 | { |
| 2308 | TestNetworkDelegate network_delegate; |
| 2309 | default_context_.set_network_delegate(&network_delegate); |
| 2310 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2311 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2312 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2313 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2314 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2315 | |
| 2316 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2317 | != std::string::npos); |
| 2318 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2319 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2320 | } |
| 2321 | |
| 2322 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2323 | { |
| 2324 | TestNetworkDelegate network_delegate; |
| 2325 | default_context_.set_network_delegate(&network_delegate); |
| 2326 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2327 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2328 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2329 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2330 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2331 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2332 | |
| 2333 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2334 | == std::string::npos); |
| 2335 | |
| 2336 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2337 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2338 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2343 | LocalHttpTestServer test_server; |
| 2344 | ASSERT_TRUE(test_server.Start()); |
| 2345 | |
| 2346 | // Set up a cookie. |
| 2347 | { |
| 2348 | TestNetworkDelegate network_delegate; |
| 2349 | default_context_.set_network_delegate(&network_delegate); |
| 2350 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2351 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2352 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2353 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2354 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2355 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2356 | |
| 2357 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2358 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2359 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2360 | } |
| 2361 | |
| 2362 | // Try to set-up another cookie and update the previous cookie. |
| 2363 | { |
| 2364 | TestNetworkDelegate network_delegate; |
| 2365 | default_context_.set_network_delegate(&network_delegate); |
| 2366 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2367 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2368 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2369 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2370 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2371 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2372 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2373 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2374 | |
| 2375 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2376 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2377 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2378 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2379 | } |
| 2380 | |
| 2381 | // Verify the cookies weren't saved or updated. |
| 2382 | { |
| 2383 | TestNetworkDelegate network_delegate; |
| 2384 | default_context_.set_network_delegate(&network_delegate); |
| 2385 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2386 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2387 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2388 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2389 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2390 | |
| 2391 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2392 | == std::string::npos); |
| 2393 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2394 | != std::string::npos); |
| 2395 | |
| 2396 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2397 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2398 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2403 | LocalHttpTestServer test_server; |
| 2404 | ASSERT_TRUE(test_server.Start()); |
| 2405 | |
| 2406 | // Set up a cookie. |
| 2407 | { |
| 2408 | TestNetworkDelegate network_delegate; |
| 2409 | default_context_.set_network_delegate(&network_delegate); |
| 2410 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2411 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2412 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2413 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2414 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2415 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2416 | |
| 2417 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2418 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2419 | } |
| 2420 | |
| 2421 | // Verify that the cookie is set. |
| 2422 | { |
| 2423 | TestNetworkDelegate network_delegate; |
| 2424 | default_context_.set_network_delegate(&network_delegate); |
| 2425 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2426 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2427 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2428 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2429 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2430 | |
| 2431 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2432 | != std::string::npos); |
| 2433 | |
| 2434 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2435 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2436 | } |
| 2437 | |
| 2438 | // Verify that the cookie isn't sent. |
| 2439 | { |
| 2440 | TestNetworkDelegate network_delegate; |
| 2441 | default_context_.set_network_delegate(&network_delegate); |
| 2442 | TestDelegate d; |
| 2443 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2444 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2445 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2446 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2447 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2448 | |
| 2449 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2450 | == std::string::npos); |
| 2451 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2452 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2453 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2454 | } |
| 2455 | } |
| 2456 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2457 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2458 | #if defined(OS_IOS) |
| 2459 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2460 | #else |
| 2461 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2462 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2463 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2464 | LocalHttpTestServer test_server; |
| 2465 | ASSERT_TRUE(test_server.Start()); |
| 2466 | |
| 2467 | // Set up a cookie. |
| 2468 | { |
| 2469 | TestNetworkDelegate network_delegate; |
| 2470 | default_context_.set_network_delegate(&network_delegate); |
| 2471 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2472 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2473 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2474 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2475 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2476 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2477 | |
| 2478 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2479 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2480 | } |
| 2481 | |
| 2482 | // Try to set-up another cookie and update the previous cookie. |
| 2483 | { |
| 2484 | TestNetworkDelegate network_delegate; |
| 2485 | default_context_.set_network_delegate(&network_delegate); |
| 2486 | TestDelegate d; |
| 2487 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2488 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2489 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2490 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2491 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2492 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2493 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2494 | |
| 2495 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2496 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2497 | } |
| 2498 | |
| 2499 | // Verify the cookies weren't saved or updated. |
| 2500 | { |
| 2501 | TestNetworkDelegate network_delegate; |
| 2502 | default_context_.set_network_delegate(&network_delegate); |
| 2503 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2504 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2505 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2506 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2507 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2508 | |
| 2509 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2510 | == std::string::npos); |
| 2511 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2512 | != std::string::npos); |
| 2513 | |
| 2514 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2515 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| 2519 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2520 | LocalHttpTestServer test_server; |
| 2521 | ASSERT_TRUE(test_server.Start()); |
| 2522 | |
| 2523 | // Set up an empty cookie. |
| 2524 | { |
| 2525 | TestNetworkDelegate network_delegate; |
| 2526 | default_context_.set_network_delegate(&network_delegate); |
| 2527 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2528 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2529 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2530 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2531 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2532 | |
| 2533 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2534 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2535 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2540 | LocalHttpTestServer test_server; |
| 2541 | ASSERT_TRUE(test_server.Start()); |
| 2542 | |
| 2543 | // Set up a cookie. |
| 2544 | { |
| 2545 | TestNetworkDelegate network_delegate; |
| 2546 | default_context_.set_network_delegate(&network_delegate); |
| 2547 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2548 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2549 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2550 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2551 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2552 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2553 | |
| 2554 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2555 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2556 | } |
| 2557 | |
| 2558 | // Verify that the cookie is set. |
| 2559 | { |
| 2560 | TestNetworkDelegate network_delegate; |
| 2561 | default_context_.set_network_delegate(&network_delegate); |
| 2562 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2563 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2564 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2565 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2566 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2567 | |
| 2568 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2569 | != std::string::npos); |
| 2570 | |
| 2571 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2572 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2573 | } |
| 2574 | |
| 2575 | // Verify that the cookie isn't sent. |
| 2576 | { |
| 2577 | TestNetworkDelegate network_delegate; |
| 2578 | default_context_.set_network_delegate(&network_delegate); |
| 2579 | TestDelegate d; |
| 2580 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2581 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2582 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2583 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2584 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2585 | |
| 2586 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2587 | == std::string::npos); |
| 2588 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2589 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2590 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2595 | LocalHttpTestServer test_server; |
| 2596 | ASSERT_TRUE(test_server.Start()); |
| 2597 | |
| 2598 | // Set up a cookie. |
| 2599 | { |
| 2600 | TestNetworkDelegate network_delegate; |
| 2601 | default_context_.set_network_delegate(&network_delegate); |
| 2602 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2603 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2604 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2605 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2606 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2607 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2608 | |
| 2609 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2610 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2611 | } |
| 2612 | |
| 2613 | // Try to set-up another cookie and update the previous cookie. |
| 2614 | { |
| 2615 | TestNetworkDelegate network_delegate; |
| 2616 | default_context_.set_network_delegate(&network_delegate); |
| 2617 | TestDelegate d; |
| 2618 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2619 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2620 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2621 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2622 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2623 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2624 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2625 | |
| 2626 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2627 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2628 | } |
| 2629 | |
| 2630 | // Verify the cookies weren't saved or updated. |
| 2631 | { |
| 2632 | TestNetworkDelegate network_delegate; |
| 2633 | default_context_.set_network_delegate(&network_delegate); |
| 2634 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2635 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2636 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2637 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2638 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2639 | |
| 2640 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2641 | == std::string::npos); |
| 2642 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2643 | != std::string::npos); |
| 2644 | |
| 2645 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2646 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2647 | } |
| 2648 | } |
| 2649 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2650 | TEST_F(URLRequestTest, SameSiteCookies) { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2651 | LocalHttpTestServer test_server; |
| 2652 | ASSERT_TRUE(test_server.Start()); |
| 2653 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2654 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2655 | default_context_.set_network_delegate(&network_delegate); |
| 2656 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2657 | const std::string kHost = "example.test"; |
| 2658 | const std::string kSubHost = "subdomain.example.test"; |
| 2659 | const std::string kCrossHost = "cross-origin.test"; |
| 2660 | |
| 2661 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2662 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2663 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2664 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2665 | test_server.GetURL(kHost, |
| 2666 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2667 | "LaxSameSiteCookie=1;SameSite=Lax"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2668 | DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2669 | req->Start(); |
| 2670 | base::RunLoop().Run(); |
| 2671 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2672 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2673 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2674 | } |
| 2675 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2676 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2677 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2678 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2679 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2680 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2681 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2682 | req->set_initiator(url::Origin(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2683 | req->Start(); |
| 2684 | base::RunLoop().Run(); |
| 2685 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2686 | EXPECT_NE(std::string::npos, |
| 2687 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2688 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2689 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2690 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2691 | } |
| 2692 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2693 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2694 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2695 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2696 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2697 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2698 | req->set_first_party_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 2699 | req->set_initiator(url::Origin(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2700 | req->Start(); |
| 2701 | base::RunLoop().Run(); |
| 2702 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2703 | EXPECT_NE(std::string::npos, |
| 2704 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2705 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2706 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2707 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2708 | } |
| 2709 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2710 | // Verify that neither cookie is not sent for cross-site requests. |
| 2711 | { |
| 2712 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2713 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2714 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2715 | req->set_first_party_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 2716 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 2717 | req->Start(); |
| 2718 | base::RunLoop().Run(); |
| 2719 | |
| 2720 | EXPECT_EQ(std::string::npos, |
| 2721 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2722 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 2723 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2724 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2725 | } |
| 2726 | |
| 2727 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2728 | // method is "safe". |
| 2729 | { |
| 2730 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2731 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2732 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2733 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2734 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 2735 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2736 | req->Start(); |
| 2737 | base::RunLoop().Run(); |
| 2738 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2739 | EXPECT_EQ(std::string::npos, |
| 2740 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2741 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2742 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2743 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2744 | } |
| 2745 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2746 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2747 | // method is unsafe (e.g. POST). |
| 2748 | { |
| 2749 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2750 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2751 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2752 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2753 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2754 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2755 | req->Start(); |
| 2756 | base::RunLoop().Run(); |
| 2757 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2758 | EXPECT_EQ(std::string::npos, |
| 2759 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2760 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2761 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2762 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2763 | } |
| 2764 | } |
| 2765 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2766 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2767 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2768 | EmbeddedTestServer http_server; |
| 2769 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2770 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2771 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2772 | https_server.AddDefaultHandlers( |
| 2773 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2774 | ASSERT_TRUE(http_server.Start()); |
| 2775 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2776 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2777 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2778 | TestURLRequestContext context(true); |
| 2779 | context.set_network_delegate(&network_delegate); |
| 2780 | context.Init(); |
| 2781 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2782 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2783 | { |
| 2784 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2785 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2786 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2787 | DEFAULT_PRIORITY, &d)); |
| 2788 | req->Start(); |
| 2789 | base::RunLoop().Run(); |
| 2790 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2791 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2792 | } |
| 2793 | |
| 2794 | // Verify that the cookie is not set. |
| 2795 | { |
| 2796 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2797 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2798 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2799 | req->Start(); |
| 2800 | base::RunLoop().Run(); |
| 2801 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2802 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 2803 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2804 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2805 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2806 | } |
| 2807 | } |
| 2808 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2809 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2810 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2811 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2812 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2813 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2814 | |
| 2815 | TestNetworkDelegate network_delegate; |
| 2816 | TestURLRequestContext context(true); |
| 2817 | context.set_network_delegate(&network_delegate); |
| 2818 | context.Init(); |
| 2819 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2820 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2821 | { |
| 2822 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2823 | std::unique_ptr<URLRequest> req( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2824 | context.CreateRequest(https_server.GetURL("/set-cookie?__Secure-foo=1"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2825 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2826 | req->Start(); |
| 2827 | base::RunLoop().Run(); |
| 2828 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2829 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2830 | } |
| 2831 | |
| 2832 | // Verify that the cookie is not set. |
| 2833 | { |
| 2834 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2835 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2836 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2837 | req->Start(); |
| 2838 | base::RunLoop().Run(); |
| 2839 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2840 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2841 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2842 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2843 | } |
| 2844 | } |
| 2845 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2846 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2847 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2848 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2849 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2850 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2851 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2852 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2853 | TestURLRequestContext context(true); |
| 2854 | context.set_network_delegate(&network_delegate); |
| 2855 | context.Init(); |
| 2856 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2857 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2858 | { |
| 2859 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2860 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2861 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2862 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2863 | req->Start(); |
| 2864 | base::RunLoop().Run(); |
| 2865 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2866 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2867 | } |
| 2868 | |
| 2869 | // Verify that the cookie is set. |
| 2870 | { |
| 2871 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2872 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2873 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2874 | req->Start(); |
| 2875 | base::RunLoop().Run(); |
| 2876 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2877 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 2878 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2879 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 2884 | // cookies are enabled. |
| 2885 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 2886 | EmbeddedTestServer http_server; |
| 2887 | http_server.AddDefaultHandlers( |
| 2888 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2889 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2890 | https_server.AddDefaultHandlers( |
| 2891 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2892 | ASSERT_TRUE(http_server.Start()); |
| 2893 | ASSERT_TRUE(https_server.Start()); |
| 2894 | |
| 2895 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2896 | TestURLRequestContext context(true); |
| 2897 | context.set_network_delegate(&network_delegate); |
| 2898 | context.Init(); |
| 2899 | |
| 2900 | // Try to set a Secure cookie, with experimental features enabled. |
| 2901 | { |
| 2902 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2903 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2904 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
| 2905 | DEFAULT_PRIORITY, &d)); |
| 2906 | req->Start(); |
| 2907 | base::RunLoop().Run(); |
| 2908 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2909 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2910 | } |
| 2911 | |
| 2912 | // Verify that the cookie is not set. |
| 2913 | { |
| 2914 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2915 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2916 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2917 | req->Start(); |
| 2918 | base::RunLoop().Run(); |
| 2919 | |
| 2920 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 2921 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2922 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 2927 | // cookies are enabled. |
| 2928 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 2929 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2930 | https_server.AddDefaultHandlers( |
| 2931 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2932 | ASSERT_TRUE(https_server.Start()); |
| 2933 | |
| 2934 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2935 | TestURLRequestContext context(true); |
| 2936 | context.set_network_delegate(&network_delegate); |
| 2937 | context.Init(); |
| 2938 | |
| 2939 | // Try to set a Secure cookie, with experimental features enabled. |
| 2940 | { |
| 2941 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2942 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2943 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
| 2944 | DEFAULT_PRIORITY, &d)); |
| 2945 | req->Start(); |
| 2946 | base::RunLoop().Run(); |
| 2947 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2948 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2949 | } |
| 2950 | |
| 2951 | // Verify that the cookie is not set. |
| 2952 | { |
| 2953 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2954 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2955 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2956 | req->Start(); |
| 2957 | base::RunLoop().Run(); |
| 2958 | |
| 2959 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2960 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2961 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2962 | } |
| 2963 | } |
| 2964 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 2965 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 2966 | // rather than a spawned test server because the connection used to talk to |
| 2967 | // the test server is affected by entering suspend mode on Android. |
| 2968 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 2969 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2970 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 2971 | |
| 2972 | URLRequestFailedJob::AddUrlHandler(); |
| 2973 | |
| 2974 | TestDelegate d; |
| 2975 | // Request that just hangs. |
| 2976 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2977 | std::unique_ptr<URLRequest> r( |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 2978 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| 2979 | r->Start(); |
| 2980 | |
| 2981 | power_monitor_source->Suspend(); |
| 2982 | // Wait for the suspend notification to cause the request to fail. |
| 2983 | base::RunLoop().Run(); |
| 2984 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 2985 | EXPECT_TRUE(d.request_failed()); |
| 2986 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2987 | |
| 2988 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 2989 | |
| 2990 | // Shouldn't be needed, but just in case. |
| 2991 | power_monitor_source->Resume(); |
| 2992 | } |
| 2993 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2994 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2995 | // value for the |fixed_date| argument given to the constructor. |
| 2996 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2997 | public: |
| 2998 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2999 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3000 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3001 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3002 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3003 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3004 | URLRequest* request, |
| 3005 | const CompletionCallback& callback, |
| 3006 | const HttpResponseHeaders* original_response_headers, |
| 3007 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3008 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3009 | |
| 3010 | private: |
| 3011 | std::string fixed_date_; |
| 3012 | |
| 3013 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3014 | }; |
| 3015 | |
| 3016 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3017 | URLRequest* request, |
| 3018 | const CompletionCallback& callback, |
| 3019 | const HttpResponseHeaders* original_response_headers, |
| 3020 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3021 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3022 | HttpResponseHeaders* new_response_headers = |
| 3023 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3024 | |
| 3025 | new_response_headers->RemoveHeader("Date"); |
| 3026 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3027 | |
| 3028 | *override_response_headers = new_response_headers; |
| 3029 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3030 | callback, |
| 3031 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3032 | override_response_headers, |
| 3033 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | // Test that cookie expiration times are adjusted for server/client clock |
| 3037 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3038 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3039 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 3040 | LocalHttpTestServer test_server; |
| 3041 | ASSERT_TRUE(test_server.Start()); |
| 3042 | |
| 3043 | // Set up an expired cookie. |
| 3044 | { |
| 3045 | TestNetworkDelegate network_delegate; |
| 3046 | default_context_.set_network_delegate(&network_delegate); |
| 3047 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3048 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3049 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3050 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3051 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3052 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3053 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3054 | } |
| 3055 | // Verify that the cookie is not set. |
| 3056 | { |
| 3057 | TestNetworkDelegate network_delegate; |
| 3058 | default_context_.set_network_delegate(&network_delegate); |
| 3059 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3060 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3061 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3062 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3063 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3064 | |
| 3065 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3066 | } |
| 3067 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3068 | { |
| 3069 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3070 | default_context_.set_network_delegate(&network_delegate); |
| 3071 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3072 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3073 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3074 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3075 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3076 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3077 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3078 | } |
| 3079 | // Verify that the cookie is set. |
| 3080 | { |
| 3081 | TestNetworkDelegate network_delegate; |
| 3082 | default_context_.set_network_delegate(&network_delegate); |
| 3083 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3084 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3085 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3086 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3087 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3088 | |
| 3089 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3094 | // Check that it is impossible to change the referrer in the extra headers of |
| 3095 | // an URLRequest. |
| 3096 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 3097 | LocalHttpTestServer test_server; |
| 3098 | ASSERT_TRUE(test_server.Start()); |
| 3099 | |
| 3100 | // If extra headers contain referer and the request contains a referer, |
| 3101 | // only the latter shall be respected. |
| 3102 | { |
| 3103 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3104 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3105 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3106 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3107 | |
| 3108 | HttpRequestHeaders headers; |
| 3109 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3110 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3111 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3112 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3113 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3114 | |
| 3115 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3116 | } |
| 3117 | |
| 3118 | // If extra headers contain a referer but the request does not, no referer |
| 3119 | // shall be sent in the header. |
| 3120 | { |
| 3121 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3122 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3123 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3124 | |
| 3125 | HttpRequestHeaders headers; |
| 3126 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3127 | req->SetExtraRequestHeaders(headers); |
| 3128 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3129 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3130 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3131 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3132 | |
| 3133 | EXPECT_EQ("None", d.data_received()); |
| 3134 | } |
| 3135 | } |
| 3136 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3137 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3138 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3139 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3140 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3141 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3142 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3143 | // |request_method| is the method to use for the initial request. |
| 3144 | // |redirect_method| is the method that is expected to be used for the second |
| 3145 | // request, after redirection. |
| 3146 | // If |include_data| is true, data is uploaded with the request. The |
| 3147 | // response body is expected to match it exactly, if and only if |
| 3148 | // |request_method| == |redirect_method|. |
| 3149 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3150 | const std::string& request_method, |
| 3151 | const std::string& redirect_method, |
| 3152 | bool include_data) { |
| 3153 | static const char kData[] = "hello world"; |
| 3154 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3155 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3156 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3157 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3158 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3159 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3160 | HttpRequestHeaders headers; |
| 3161 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3162 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3163 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3164 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3165 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3166 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3167 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3168 | EXPECT_EQ(redirect_method, req->method()); |
| 3169 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 3170 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3171 | if (include_data) { |
| 3172 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3173 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3174 | HttpRequestHeaders::kContentLength)); |
| 3175 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3176 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3177 | EXPECT_EQ(kData, d.data_received()); |
| 3178 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3179 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3180 | HttpRequestHeaders::kContentLength)); |
| 3181 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3182 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3183 | EXPECT_NE(kData, d.data_received()); |
| 3184 | } |
| 3185 | } |
| 3186 | if (HasFailure()) |
| 3187 | LOG(WARNING) << "Request method was: " << request_method; |
| 3188 | } |
| 3189 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3190 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3191 | // |request_method| is the method to use for the initial request. |
| 3192 | // |redirect_method| is the method that is expected to be used for the second |
| 3193 | // request, after redirection. |
| 3194 | // |origin_value| is the expected value for the Origin header after |
| 3195 | // redirection. If empty, expects that there will be no Origin header. |
| 3196 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3197 | const std::string& request_method, |
| 3198 | const std::string& redirect_method, |
| 3199 | const std::string& origin_value) { |
| 3200 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3201 | std::unique_ptr<URLRequest> req( |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3202 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3203 | req->set_method(request_method); |
| 3204 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3205 | redirect_url.GetOrigin().spec(), false); |
| 3206 | req->Start(); |
| 3207 | |
| 3208 | base::RunLoop().Run(); |
| 3209 | |
| 3210 | EXPECT_EQ(redirect_method, req->method()); |
| 3211 | // Note that there is no check for request success here because, for |
| 3212 | // purposes of testing, the request very well may fail. For example, if the |
| 3213 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3214 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3215 | // request would fail. However, that's fine, as long as the request headers |
| 3216 | // are in order and pass the checks below. |
| 3217 | if (origin_value.empty()) { |
| 3218 | EXPECT_FALSE( |
| 3219 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3220 | } else { |
| 3221 | std::string origin_header; |
| 3222 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3223 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 3224 | EXPECT_EQ(origin_value, origin_header); |
| 3225 | } |
| 3226 | } |
| 3227 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3228 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3229 | const int kMsgSize = 20000; // multiple of 10 |
| 3230 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3231 | char* uploadBytes = new char[kMsgSize+1]; |
| 3232 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3233 | char marker = 'a'; |
| 3234 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3235 | memcpy(ptr, "----------", 10); |
| 3236 | ptr += 10; |
| 3237 | if (idx % 100 == 0) { |
| 3238 | ptr--; |
| 3239 | *ptr++ = marker; |
| 3240 | if (++marker > 'z') |
| 3241 | marker = 'a'; |
| 3242 | } |
| 3243 | } |
| 3244 | uploadBytes[kMsgSize] = '\0'; |
| 3245 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3246 | for (int i = 0; i < kIterations; ++i) { |
| 3247 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3248 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3249 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3250 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3251 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3252 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3253 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3254 | r->Start(); |
| 3255 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3256 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3257 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3258 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3259 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3260 | << "request failed: " << r->status().status() |
| 3261 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3262 | |
| 3263 | EXPECT_FALSE(d.received_data_before_response()); |
| 3264 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3265 | } |
| 3266 | delete[] uploadBytes; |
| 3267 | } |
| 3268 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3269 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3270 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3271 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3272 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3273 | base::IntToString(num_cookies)), |
| 3274 | DEFAULT_PRIORITY, &d)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3275 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3276 | r->Start(); |
| 3277 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3278 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3279 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3280 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3281 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3282 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3283 | if (!is_success) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3284 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3285 | |
| 3286 | return is_success; |
| 3287 | } |
| 3288 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3289 | LocalHttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3290 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3291 | private: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3292 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3293 | }; |
| 3294 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3295 | namespace { |
| 3296 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3297 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3298 | const test_server::HttpRequest& request) { |
| 3299 | if (request.headers.find("Host") == request.headers.end() || |
| 3300 | request.headers.at("Host") != "www.redirect.com" || |
| 3301 | request.method != test_server::METHOD_CONNECT) { |
| 3302 | return nullptr; |
| 3303 | } |
| 3304 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3305 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3306 | new test_server::BasicHttpResponse); |
| 3307 | http_response->set_code(HTTP_FOUND); |
| 3308 | http_response->AddCustomHeader("Location", |
| 3309 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3310 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | } // namespace |
| 3314 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3315 | class TestSSLConfigService : public SSLConfigService { |
| 3316 | public: |
| 3317 | TestSSLConfigService(bool ev_enabled, |
| 3318 | bool online_rev_checking, |
| 3319 | bool rev_checking_required_local_anchors, |
| 3320 | bool token_binding_enabled) |
| 3321 | : ev_enabled_(ev_enabled), |
| 3322 | online_rev_checking_(online_rev_checking), |
| 3323 | rev_checking_required_local_anchors_( |
| 3324 | rev_checking_required_local_anchors), |
| 3325 | token_binding_enabled_(token_binding_enabled), |
| 3326 | min_version_(kDefaultSSLVersionMin), |
| 3327 | fallback_min_version_(kDefaultSSLVersionFallbackMin) {} |
| 3328 | |
| 3329 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3330 | |
| 3331 | void set_fallback_min_version(uint16_t version) { |
| 3332 | fallback_min_version_ = version; |
| 3333 | } |
| 3334 | |
| 3335 | // SSLConfigService: |
| 3336 | void GetSSLConfig(SSLConfig* config) override { |
| 3337 | *config = SSLConfig(); |
| 3338 | config->rev_checking_enabled = online_rev_checking_; |
| 3339 | config->verify_ev_cert = ev_enabled_; |
| 3340 | config->rev_checking_required_local_anchors = |
| 3341 | rev_checking_required_local_anchors_; |
| 3342 | if (fallback_min_version_) { |
| 3343 | config->version_fallback_min = fallback_min_version_; |
| 3344 | } |
| 3345 | if (min_version_) { |
| 3346 | config->version_min = min_version_; |
| 3347 | } |
| 3348 | if (token_binding_enabled_) { |
| 3349 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | protected: |
| 3354 | ~TestSSLConfigService() override {} |
| 3355 | |
| 3356 | private: |
| 3357 | const bool ev_enabled_; |
| 3358 | const bool online_rev_checking_; |
| 3359 | const bool rev_checking_required_local_anchors_; |
| 3360 | const bool token_binding_enabled_; |
| 3361 | uint16_t min_version_; |
| 3362 | uint16_t fallback_min_version_; |
| 3363 | }; |
| 3364 | |
| 3365 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3366 | #if !defined(OS_IOS) |
| 3367 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3368 | public: |
| 3369 | void SetUp() override { |
| 3370 | default_context_.set_ssl_config_service( |
| 3371 | new TestSSLConfigService(false, false, false, true)); |
| 3372 | channel_id_service_.reset(new ChannelIDService( |
| 3373 | new DefaultChannelIDStore(NULL), base::ThreadTaskRunnerHandle::Get())); |
| 3374 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3375 | URLRequestTestHTTP::SetUp(); |
| 3376 | } |
| 3377 | |
| 3378 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3379 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3380 | }; |
| 3381 | |
| 3382 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3383 | SpawnedTestServer::SSLOptions ssl_options; |
| 3384 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3385 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3386 | ssl_options, |
| 3387 | base::FilePath(kTestFilePath)); |
| 3388 | ASSERT_TRUE(https_test_server.Start()); |
| 3389 | |
| 3390 | TestDelegate d; |
| 3391 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3392 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3393 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d)); |
| 3394 | r->Start(); |
| 3395 | EXPECT_TRUE(r->is_pending()); |
| 3396 | |
| 3397 | base::RunLoop().Run(); |
| 3398 | |
| 3399 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3400 | |
| 3401 | HttpRequestHeaders headers; |
| 3402 | std::string token_binding_header, token_binding_message; |
| 3403 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3404 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3405 | &token_binding_header)); |
| 3406 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3407 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3408 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3409 | std::vector<TokenBinding> token_bindings; |
| 3410 | ASSERT_TRUE( |
| 3411 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3412 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3413 | |
| 3414 | EXPECT_GT(d.bytes_received(), 0); |
| 3415 | std::string ekm = d.data_received(); |
| 3416 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3417 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3418 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3419 | token_bindings[0].signature, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3420 | } |
| 3421 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3422 | |
| 3423 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3424 | SpawnedTestServer::SSLOptions ssl_options; |
| 3425 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3426 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3427 | ssl_options, |
| 3428 | base::FilePath(kTestFilePath)); |
| 3429 | ASSERT_TRUE(https_test_server.Start()); |
| 3430 | |
| 3431 | TestDelegate d; |
| 3432 | { |
| 3433 | GURL redirect_url = |
| 3434 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3435 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3436 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3437 | r->Start(); |
| 3438 | EXPECT_TRUE(r->is_pending()); |
| 3439 | |
| 3440 | base::RunLoop().Run(); |
| 3441 | |
| 3442 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3443 | |
| 3444 | HttpRequestHeaders headers; |
| 3445 | std::string token_binding_header, token_binding_message; |
| 3446 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3447 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3448 | &token_binding_header)); |
| 3449 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3450 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3451 | &token_binding_message)); |
| 3452 | std::vector<TokenBinding> token_bindings; |
| 3453 | ASSERT_TRUE( |
| 3454 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3455 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3456 | |
| 3457 | EXPECT_GT(d.bytes_received(), 0); |
| 3458 | std::string ekm = d.data_received(); |
| 3459 | |
| 3460 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3461 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3462 | token_bindings[0].signature, ekm)); |
| 3463 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
| 3464 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[1].ec_point, |
| 3465 | token_bindings[1].signature, ekm)); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3470 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3471 | #if !defined(OS_ANDROID) |
| 3472 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
| 3473 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, |
| 3474 | SpawnedTestServer::kLocalhost, |
| 3475 | base::FilePath()); |
| 3476 | ASSERT_TRUE(http_server.Start()); |
| 3477 | SpawnedTestServer::SSLOptions ssl_options; |
| 3478 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3479 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3480 | ssl_options, |
| 3481 | base::FilePath(kTestFilePath)); |
| 3482 | ASSERT_TRUE(https_test_server.Start()); |
| 3483 | |
| 3484 | TestDelegate d; |
| 3485 | { |
| 3486 | GURL redirect_url = http_server.GetURL( |
| 3487 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3488 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3489 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3490 | r->Start(); |
| 3491 | EXPECT_TRUE(r->is_pending()); |
| 3492 | |
| 3493 | base::RunLoop().Run(); |
| 3494 | |
| 3495 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3496 | |
| 3497 | HttpRequestHeaders headers; |
| 3498 | std::string token_binding_header, token_binding_message; |
| 3499 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3500 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3501 | &token_binding_header)); |
| 3502 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3503 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3504 | &token_binding_message)); |
| 3505 | std::vector<TokenBinding> token_bindings; |
| 3506 | ASSERT_TRUE( |
| 3507 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3508 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3509 | |
| 3510 | EXPECT_GT(d.bytes_received(), 0); |
| 3511 | std::string ekm = d.data_received(); |
| 3512 | |
| 3513 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3514 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3515 | token_bindings[0].signature, ekm)); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | // Test that if a server supporting Token Binding redirects (with |
| 3520 | // Include-Referer-Token-Binding-ID) to an https url on a server that does not |
| 3521 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3522 | // the redirect. |
| 3523 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3524 | SpawnedTestServer::SSLOptions ssl_options; |
| 3525 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3526 | ssl_options, |
| 3527 | base::FilePath(kTestFilePath)); |
| 3528 | ASSERT_TRUE(https_test_server.Start()); |
| 3529 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3530 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3531 | ssl_options, |
| 3532 | base::FilePath(kTestFilePath)); |
| 3533 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3534 | |
| 3535 | TestDelegate d; |
| 3536 | { |
| 3537 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3538 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3539 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3540 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3541 | r->Start(); |
| 3542 | EXPECT_TRUE(r->is_pending()); |
| 3543 | |
| 3544 | base::RunLoop().Run(); |
| 3545 | |
| 3546 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3547 | |
| 3548 | HttpRequestHeaders headers; |
| 3549 | std::string token_binding_header, token_binding_message; |
| 3550 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3551 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3552 | &token_binding_header)); |
| 3553 | } |
| 3554 | } |
| 3555 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3556 | #endif // !defined(OS_IOS) |
| 3557 | |
| 3558 | // 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] | 3559 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3560 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3561 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3562 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3563 | http_test_server()->RegisterRequestHandler( |
| 3564 | base::Bind(&HandleRedirectConnect)); |
| 3565 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3566 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3567 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3568 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3569 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3570 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3571 | TestDelegate d; |
| 3572 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3573 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3574 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3575 | r->Start(); |
| 3576 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3577 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3578 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3579 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3580 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3581 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3582 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3583 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3584 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3585 | // We should not have followed the redirect. |
| 3586 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3587 | } |
| 3588 | } |
| 3589 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3590 | // This is the same as the previous test, but checks that the network delegate |
| 3591 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3592 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3593 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3594 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3595 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3596 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3597 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3598 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3599 | TestDelegate d; |
| 3600 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3601 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3602 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3603 | r->Start(); |
| 3604 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3605 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3606 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3607 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3608 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3609 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3610 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3611 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3612 | EXPECT_EQ(1, d.response_started_count()); |
| 3613 | // We should not have followed the redirect. |
| 3614 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3615 | |
| 3616 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3617 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3618 | } |
| 3619 | } |
| 3620 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3621 | // Tests that we can block and asynchronously return OK in various stages. |
| 3622 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 3623 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 3624 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3625 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3626 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 3627 | }; |
| 3628 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 3629 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3630 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3631 | |
| 3632 | TestDelegate d; |
| 3633 | BlockingNetworkDelegate network_delegate( |
| 3634 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3635 | network_delegate.set_block_on( |
| 3636 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 3637 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 3638 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3639 | |
| 3640 | TestURLRequestContext context(true); |
| 3641 | context.set_network_delegate(&network_delegate); |
| 3642 | context.Init(); |
| 3643 | |
| 3644 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3645 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3646 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3647 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3648 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3649 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3650 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3651 | EXPECT_EQ(blocking_stages[i], |
| 3652 | network_delegate.stage_blocked_for_callback()); |
| 3653 | network_delegate.DoCallback(OK); |
| 3654 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3655 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3656 | EXPECT_EQ(200, r->GetResponseCode()); |
| 3657 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3658 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3659 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3660 | } |
| 3661 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3662 | } |
| 3663 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3664 | // Tests that the network delegate can block and cancel a request. |
| 3665 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3666 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3667 | |
| 3668 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3669 | BlockingNetworkDelegate network_delegate( |
| 3670 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3671 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 3672 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3673 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3674 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3675 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3676 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3677 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3678 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3679 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3680 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3681 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3682 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3683 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3684 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3685 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3686 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3687 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3688 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3689 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3690 | } |
| 3691 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3692 | } |
| 3693 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3694 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 3695 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 3696 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 3697 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 3698 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 3699 | BlockingNetworkDelegate::Stage stage, |
| 3700 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3701 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3702 | BlockingNetworkDelegate network_delegate(block_mode); |
| 3703 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 3704 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3705 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3706 | TestURLRequestContext context(true); |
| 3707 | context.set_network_delegate(&network_delegate); |
| 3708 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3709 | |
| 3710 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3711 | std::unique_ptr<URLRequest> r( |
| 3712 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3713 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3714 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3715 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3716 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3717 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3718 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3719 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3720 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3721 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3722 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3723 | } |
| 3724 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3725 | } |
| 3726 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3727 | // The following 3 tests check that the network delegate can cancel a request |
| 3728 | // synchronously in various stages of the request. |
| 3729 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3730 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3731 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3732 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3733 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3737 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3738 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3739 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3740 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3744 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3745 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3746 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3747 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | // The following 3 tests check that the network delegate can cancel a request |
| 3751 | // asynchronously in various stages of the request. |
| 3752 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3753 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3754 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3755 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3756 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3760 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3761 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3762 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3763 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3767 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3768 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3769 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3770 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3771 | } |
| 3772 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3773 | // Tests that the network delegate can block and redirect a request to a new |
| 3774 | // URL. |
| 3775 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3776 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3777 | |
| 3778 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3779 | BlockingNetworkDelegate network_delegate( |
| 3780 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3781 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3782 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3783 | network_delegate.set_redirect_url(redirect_url); |
| 3784 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3785 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3786 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3787 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3788 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3789 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3790 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3791 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3792 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3793 | // Quit after hitting the redirect, so can check the headers. |
| 3794 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3795 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3796 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3797 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3798 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3799 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3800 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3801 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3802 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3803 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3804 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3805 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3806 | |
| 3807 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3808 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3809 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3810 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3811 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3812 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3813 | // through an untunneled proxy. |
| 3814 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3815 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3816 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3817 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3818 | EXPECT_EQ(0, r->status().error()); |
| 3819 | EXPECT_EQ(redirect_url, r->url()); |
| 3820 | EXPECT_EQ(original_url, r->original_url()); |
| 3821 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3822 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3823 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3824 | } |
| 3825 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3826 | } |
| 3827 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3828 | // Tests that the network delegate can block and redirect a request to a new |
| 3829 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 3830 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3831 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3832 | |
| 3833 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3834 | BlockingNetworkDelegate network_delegate( |
| 3835 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3836 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3837 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3838 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3839 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3840 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3841 | |
| 3842 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3843 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3844 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3845 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3846 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3847 | // Quit after hitting the redirect, so can check the headers. |
| 3848 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3849 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3850 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3851 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3852 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3853 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3854 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3855 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3856 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3857 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3858 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3859 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3860 | |
| 3861 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3862 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3863 | base::RunLoop().Run(); |
| 3864 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3865 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3866 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3867 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3868 | // through an untunneled proxy. |
| 3869 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3870 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3871 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3872 | EXPECT_EQ(0, r->status().error()); |
| 3873 | EXPECT_EQ(redirect_url, r->url()); |
| 3874 | EXPECT_EQ(original_url, r->original_url()); |
| 3875 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3876 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3877 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3878 | } |
| 3879 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3880 | } |
| 3881 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3882 | // Tests that redirects caused by the network delegate preserve POST data. |
| 3883 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3884 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3885 | |
| 3886 | const char kData[] = "hello world"; |
| 3887 | |
| 3888 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3889 | BlockingNetworkDelegate network_delegate( |
| 3890 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3891 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3892 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3893 | network_delegate.set_redirect_url(redirect_url); |
| 3894 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3895 | TestURLRequestContext context(true); |
| 3896 | context.set_network_delegate(&network_delegate); |
| 3897 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3898 | |
| 3899 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3900 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3901 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3902 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3903 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3904 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3905 | HttpRequestHeaders headers; |
| 3906 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3907 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3908 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3909 | |
| 3910 | // Quit after hitting the redirect, so can check the headers. |
| 3911 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3912 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3913 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3914 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3915 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3916 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3917 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3918 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3919 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3920 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3921 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3922 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3923 | |
| 3924 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3925 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3926 | base::RunLoop().Run(); |
| 3927 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3928 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3929 | EXPECT_EQ(0, r->status().error()); |
| 3930 | EXPECT_EQ(redirect_url, r->url()); |
| 3931 | EXPECT_EQ(original_url, r->original_url()); |
| 3932 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3933 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3934 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3935 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3936 | EXPECT_EQ(kData, d.data_received()); |
| 3937 | } |
| 3938 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3939 | } |
| 3940 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3941 | // Tests that the network delegate can block and redirect a request to a new |
| 3942 | // URL during OnHeadersReceived. |
| 3943 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3944 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3945 | |
| 3946 | TestDelegate d; |
| 3947 | BlockingNetworkDelegate network_delegate( |
| 3948 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3949 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3950 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3951 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3952 | |
| 3953 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3954 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3955 | |
| 3956 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3957 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3958 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3959 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3960 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3961 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3962 | base::RunLoop().Run(); |
| 3963 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3964 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3965 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3966 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3967 | // through an untunneled proxy. |
| 3968 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3969 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3970 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3971 | |
| 3972 | EXPECT_EQ(OK, r->status().error()); |
| 3973 | EXPECT_EQ(redirect_url, r->url()); |
| 3974 | EXPECT_EQ(original_url, r->original_url()); |
| 3975 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3976 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3977 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3978 | } |
| 3979 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3980 | } |
| 3981 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3982 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3983 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3984 | // handle the challenge, and is passing the buck along to the |
| 3985 | // URLRequest::Delegate. |
| 3986 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3987 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3988 | |
| 3989 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3990 | BlockingNetworkDelegate network_delegate( |
| 3991 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3992 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3993 | TestURLRequestContext context(true); |
| 3994 | context.set_network_delegate(&network_delegate); |
| 3995 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3996 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3997 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3998 | |
| 3999 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4000 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4001 | std::unique_ptr<URLRequest> r( |
| 4002 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4003 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4004 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4005 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4006 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4007 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4008 | EXPECT_EQ(0, r->status().error()); |
| 4009 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4010 | EXPECT_TRUE(d.auth_required_called()); |
| 4011 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4012 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4013 | } |
| 4014 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4015 | } |
| 4016 | |
| 4017 | TEST_F(URLRequestTestHTTP, |
| 4018 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4019 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4020 | |
| 4021 | TestDelegate d; |
| 4022 | BlockingNetworkDelegate network_delegate( |
| 4023 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4024 | |
| 4025 | TestURLRequestContext context(true); |
| 4026 | context.set_network_delegate(&network_delegate); |
| 4027 | context.Init(); |
| 4028 | |
| 4029 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4030 | |
| 4031 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4032 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4033 | std::unique_ptr<URLRequest> r( |
| 4034 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4035 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4036 | |
| 4037 | { |
| 4038 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4039 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4040 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 4041 | } |
| 4042 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4043 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4044 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4045 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4046 | EXPECT_EQ(0, r->status().error()); |
| 4047 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4048 | EXPECT_TRUE(d.auth_required_called()); |
| 4049 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4050 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4051 | } |
| 4052 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4053 | } |
| 4054 | |
| 4055 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4056 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4057 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4058 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4059 | |
| 4060 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4061 | BlockingNetworkDelegate network_delegate( |
| 4062 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4063 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4064 | network_delegate.set_auth_retval( |
| 4065 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4066 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4067 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4068 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4069 | TestURLRequestContext context(true); |
| 4070 | context.set_network_delegate(&network_delegate); |
| 4071 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4072 | |
| 4073 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4074 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4075 | std::unique_ptr<URLRequest> r( |
| 4076 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4077 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4078 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4079 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4080 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4081 | EXPECT_EQ(0, r->status().error()); |
| 4082 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4083 | EXPECT_FALSE(d.auth_required_called()); |
| 4084 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4085 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4086 | } |
| 4087 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4088 | } |
| 4089 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4090 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4091 | // headers (for the first or second request) when called at the proper times. |
| 4092 | TEST_F(URLRequestTestHTTP, |
| 4093 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4094 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4095 | |
| 4096 | TestDelegate d; |
| 4097 | BlockingNetworkDelegate network_delegate( |
| 4098 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4099 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4100 | network_delegate.set_auth_retval( |
| 4101 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4102 | |
| 4103 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4104 | |
| 4105 | TestURLRequestContext context(true); |
| 4106 | context.set_network_delegate(&network_delegate); |
| 4107 | context.Init(); |
| 4108 | |
| 4109 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4110 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4111 | std::unique_ptr<URLRequest> r( |
| 4112 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4113 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4114 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4115 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4116 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4117 | EXPECT_EQ(0, r->status().error()); |
| 4118 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4119 | EXPECT_FALSE(d.auth_required_called()); |
| 4120 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4121 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4122 | |
| 4123 | { |
| 4124 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4125 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4126 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4127 | } |
| 4128 | } |
| 4129 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4130 | } |
| 4131 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4132 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4133 | // by cancelling authentication. |
| 4134 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4135 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4136 | |
| 4137 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4138 | BlockingNetworkDelegate network_delegate( |
| 4139 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4140 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4141 | network_delegate.set_auth_retval( |
| 4142 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4143 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4144 | TestURLRequestContext context(true); |
| 4145 | context.set_network_delegate(&network_delegate); |
| 4146 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4147 | |
| 4148 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4149 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4150 | std::unique_ptr<URLRequest> r( |
| 4151 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4152 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4153 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4154 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4155 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4156 | EXPECT_EQ(OK, r->status().error()); |
| 4157 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4158 | EXPECT_FALSE(d.auth_required_called()); |
| 4159 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4160 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4161 | } |
| 4162 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4163 | } |
| 4164 | |
| 4165 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4166 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4167 | // to handle the challenge, and is passing the buck along to the |
| 4168 | // URLRequest::Delegate. |
| 4169 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4170 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4171 | |
| 4172 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4173 | BlockingNetworkDelegate network_delegate( |
| 4174 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4175 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4176 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4177 | TestURLRequestContext context(true); |
| 4178 | context.set_network_delegate(&network_delegate); |
| 4179 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4180 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4181 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4182 | |
| 4183 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4184 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4185 | std::unique_ptr<URLRequest> r( |
| 4186 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4187 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4188 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4189 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4190 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4191 | EXPECT_EQ(0, r->status().error()); |
| 4192 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4193 | EXPECT_TRUE(d.auth_required_called()); |
| 4194 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4195 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4196 | } |
| 4197 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4198 | } |
| 4199 | |
| 4200 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4201 | // by setting credentials. |
| 4202 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4203 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4204 | |
| 4205 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4206 | BlockingNetworkDelegate network_delegate( |
| 4207 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4208 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4209 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4210 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4211 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4212 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4213 | network_delegate.set_auth_credentials(auth_credentials); |
| 4214 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4215 | TestURLRequestContext context(true); |
| 4216 | context.set_network_delegate(&network_delegate); |
| 4217 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4218 | |
| 4219 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4220 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4221 | std::unique_ptr<URLRequest> r( |
| 4222 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4223 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4224 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4225 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4226 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4227 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4228 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4229 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4230 | EXPECT_FALSE(d.auth_required_called()); |
| 4231 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4232 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4233 | } |
| 4234 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4235 | } |
| 4236 | |
| 4237 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4238 | // by cancelling authentication. |
| 4239 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4240 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4241 | |
| 4242 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4243 | BlockingNetworkDelegate network_delegate( |
| 4244 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4245 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4246 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4247 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4248 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4249 | TestURLRequestContext context(true); |
| 4250 | context.set_network_delegate(&network_delegate); |
| 4251 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4252 | |
| 4253 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4254 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4255 | std::unique_ptr<URLRequest> r( |
| 4256 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4257 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4258 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4259 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4260 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4261 | EXPECT_EQ(OK, r->status().error()); |
| 4262 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4263 | EXPECT_FALSE(d.auth_required_called()); |
| 4264 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4265 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4266 | } |
| 4267 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4268 | } |
| 4269 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4270 | // Tests that we can handle when a network request was canceled while we were |
| 4271 | // waiting for the network delegate. |
| 4272 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4273 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4274 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4275 | |
| 4276 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4277 | BlockingNetworkDelegate network_delegate( |
| 4278 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4279 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4280 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4281 | TestURLRequestContext context(true); |
| 4282 | context.set_network_delegate(&network_delegate); |
| 4283 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4284 | |
| 4285 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4286 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4287 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4288 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4289 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4290 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4291 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4292 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4293 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4294 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4295 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4296 | // Ensure that network delegate is notified. |
| 4297 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4298 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4299 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4300 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4301 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4302 | } |
| 4303 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4304 | } |
| 4305 | |
| 4306 | // Tests that we can handle when a network request was canceled while we were |
| 4307 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4308 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4309 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4310 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4311 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4312 | |
| 4313 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4314 | BlockingNetworkDelegate network_delegate( |
| 4315 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4316 | network_delegate.set_block_on( |
| 4317 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4318 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4319 | TestURLRequestContext context(true); |
| 4320 | context.set_network_delegate(&network_delegate); |
| 4321 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4322 | |
| 4323 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4324 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4325 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4326 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4327 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4328 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4329 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4330 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4331 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4332 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4333 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4334 | // Ensure that network delegate is notified. |
| 4335 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4336 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4337 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4338 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4339 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4340 | } |
| 4341 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4342 | } |
| 4343 | |
| 4344 | // Tests that we can handle when a network request was canceled while we were |
| 4345 | // waiting for the network delegate. |
| 4346 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4347 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4349 | |
| 4350 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4351 | BlockingNetworkDelegate network_delegate( |
| 4352 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4353 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4354 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4355 | TestURLRequestContext context(true); |
| 4356 | context.set_network_delegate(&network_delegate); |
| 4357 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4358 | |
| 4359 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4360 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4361 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4362 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4363 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4364 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4365 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4366 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4367 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4368 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4369 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4370 | // Ensure that network delegate is notified. |
| 4371 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4372 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4373 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4374 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4375 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4376 | } |
| 4377 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4378 | } |
| 4379 | |
| 4380 | // Tests that we can handle when a network request was canceled while we were |
| 4381 | // waiting for the network delegate. |
| 4382 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4383 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4384 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4385 | |
| 4386 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4387 | BlockingNetworkDelegate network_delegate( |
| 4388 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4389 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4390 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4391 | TestURLRequestContext context(true); |
| 4392 | context.set_network_delegate(&network_delegate); |
| 4393 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4394 | |
| 4395 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4396 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4397 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4398 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4399 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4400 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4401 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4402 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4403 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4404 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4405 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4406 | // Ensure that network delegate is notified. |
| 4407 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4408 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4409 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4410 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4411 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4412 | } |
| 4413 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4414 | } |
| 4415 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4416 | namespace { |
| 4417 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4418 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4419 | const test_server::HttpRequest& request) { |
| 4420 | if (request.headers.find("Host") == request.headers.end() || |
| 4421 | request.headers.at("Host") != "www.server-auth.com" || |
| 4422 | request.method != test_server::METHOD_CONNECT) { |
| 4423 | return nullptr; |
| 4424 | } |
| 4425 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4426 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4427 | new test_server::BasicHttpResponse); |
| 4428 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4429 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4430 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4431 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | } // namespace |
| 4435 | |
| 4436 | // 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] | 4437 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4438 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4439 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4440 | http_test_server()->RegisterRequestHandler( |
| 4441 | base::Bind(&HandleServerAuthConnect)); |
| 4442 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4443 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4444 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4445 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4446 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4447 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4448 | TestDelegate d; |
| 4449 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4450 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4451 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4452 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4453 | r->Start(); |
| 4454 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4455 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4456 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4457 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4458 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4459 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4460 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 4461 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4462 | } |
| 4463 | } |
| 4464 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4465 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4466 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4467 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4468 | TestDelegate d; |
| 4469 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4470 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4471 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4472 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4473 | r->Start(); |
| 4474 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4475 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4476 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4477 | |
| 4478 | EXPECT_EQ(1, d.response_started_count()); |
| 4479 | EXPECT_FALSE(d.received_data_before_response()); |
| 4480 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4481 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4482 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4483 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4484 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4485 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4486 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4487 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4488 | } |
| 4489 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4490 | // This test has the server send a large number of cookies to the client. |
| 4491 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4492 | // search is used to estimate that maximum number of cookies that are accepted |
| 4493 | // by the browser. Beyond the maximum number, the request will fail with |
| 4494 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4495 | #if defined(OS_WIN) |
| 4496 | // http://crbug.com/177916 |
| 4497 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4498 | #else |
| 4499 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4500 | #endif // defined(OS_WIN) |
| 4501 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4502 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4503 | |
| 4504 | int lower_bound = 0; |
| 4505 | int upper_bound = 1; |
| 4506 | |
| 4507 | // Double the number of cookies until the response header limits are |
| 4508 | // exceeded. |
| 4509 | while (DoManyCookiesRequest(upper_bound)) { |
| 4510 | lower_bound = upper_bound; |
| 4511 | upper_bound *= 2; |
| 4512 | ASSERT_LT(upper_bound, 1000000); |
| 4513 | } |
| 4514 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4515 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4516 | if (tolerance < 2) |
| 4517 | tolerance = 2; |
| 4518 | |
| 4519 | // Perform a binary search to find the highest possible number of cookies, |
| 4520 | // within the desired tolerance. |
| 4521 | while (upper_bound - lower_bound >= tolerance) { |
| 4522 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4523 | |
| 4524 | if (DoManyCookiesRequest(num_cookies)) |
| 4525 | lower_bound = num_cookies; |
| 4526 | else |
| 4527 | upper_bound = num_cookies; |
| 4528 | } |
| 4529 | // Success: the test did not crash. |
| 4530 | } |
| 4531 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4532 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4533 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4534 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4535 | TestDelegate d; |
| 4536 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4537 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4538 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4539 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4540 | r->Start(); |
| 4541 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4542 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4543 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4544 | |
| 4545 | EXPECT_EQ(1, d.response_started_count()); |
| 4546 | EXPECT_FALSE(d.received_data_before_response()); |
| 4547 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4548 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4549 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4550 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4551 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4552 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4553 | } |
| 4554 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4555 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4556 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4557 | |
| 4558 | TestDelegate d; |
| 4559 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4560 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4561 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4562 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4563 | |
| 4564 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4565 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4566 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4567 | r->Start(); |
| 4568 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4569 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4570 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4571 | |
| 4572 | EXPECT_EQ(1, d.response_started_count()); |
| 4573 | EXPECT_FALSE(d.received_data_before_response()); |
| 4574 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4575 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4576 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4577 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4578 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4579 | |
| 4580 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4581 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4582 | } |
| 4583 | } |
| 4584 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4585 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4586 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4587 | |
| 4588 | TestDelegate d; |
| 4589 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4590 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4591 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4592 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4593 | r->Start(); |
| 4594 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4595 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4596 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4597 | |
| 4598 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4599 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4600 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4601 | |
| 4602 | EXPECT_EQ(1, d.response_started_count()); |
| 4603 | EXPECT_FALSE(d.received_data_before_response()); |
| 4604 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4605 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4606 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4607 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4608 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4609 | } |
| 4610 | } |
| 4611 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4612 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 4613 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4614 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4615 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 4616 | SpawnedTestServer::kLocalhost, |
| 4617 | base::FilePath(kTestFilePath)); |
| 4618 | |
| 4619 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4620 | |
| 4621 | // Parameter that specifies the Content-Length field in the response: |
| 4622 | // C - Compressed length. |
| 4623 | // U - Uncompressed length. |
| 4624 | // L - Large length (larger than both C & U). |
| 4625 | // M - Medium length (between C & U). |
| 4626 | // S - Small length (smaller than both C & U). |
| 4627 | const char test_parameters[] = "CULMS"; |
| 4628 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 4629 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 4630 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4631 | // S has too little data, but we seem to accept it. |
| 4632 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 4633 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4634 | |
| 4635 | for (int i = 0; i < num_tests ; i++) { |
| 4636 | TestDelegate d; |
| 4637 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4638 | std::string test_file = base::StringPrintf( |
| 4639 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4640 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4641 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4642 | TestURLRequestContext context(true); |
| 4643 | context.set_network_delegate(&network_delegate); |
| 4644 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4645 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4646 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4647 | test_server.GetURL(test_file), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4648 | r->Start(); |
| 4649 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4650 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4651 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4652 | |
| 4653 | EXPECT_EQ(1, d.response_started_count()); |
| 4654 | EXPECT_FALSE(d.received_data_before_response()); |
| 4655 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4656 | << " status = " << r->status().status() |
| 4657 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4658 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4659 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4660 | << " Parameter = \"" << test_file << "\""; |
| 4661 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4662 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4663 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4664 | << " Parameter = \"" << test_file << "\""; |
| 4665 | } |
| 4666 | } |
| 4667 | } |
| 4668 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4669 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4670 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4671 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4672 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4673 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4674 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4675 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4676 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4677 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4678 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4679 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4680 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4681 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4682 | |
| 4683 | EXPECT_EQ(1, d.response_started_count()); |
| 4684 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4685 | EXPECT_EQ(destination_url, req->url()); |
| 4686 | EXPECT_EQ(original_url, req->original_url()); |
| 4687 | ASSERT_EQ(2U, req->url_chain().size()); |
| 4688 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4689 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4690 | |
| 4691 | LoadTimingInfo load_timing_info_before_redirect; |
| 4692 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 4693 | &load_timing_info_before_redirect)); |
| 4694 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 4695 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 4696 | |
| 4697 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4698 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4699 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4700 | |
| 4701 | // Check that a new socket was used on redirect, since the server does not |
| 4702 | // supposed keep-alive sockets, and that the times before the redirect are |
| 4703 | // before the ones recorded for the second request. |
| 4704 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 4705 | load_timing_info.socket_log_id); |
| 4706 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 4707 | load_timing_info.connect_timing.connect_start); |
| 4708 | } |
| 4709 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4710 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4711 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4712 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4713 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4714 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4715 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 4716 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 4717 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4718 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4719 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4720 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4721 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4722 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4723 | |
| 4724 | EXPECT_EQ(1, d.response_started_count()); |
| 4725 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4726 | EXPECT_EQ(destination_url, req->url()); |
| 4727 | EXPECT_EQ(original_url, req->original_url()); |
| 4728 | ASSERT_EQ(3U, req->url_chain().size()); |
| 4729 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4730 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 4731 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4732 | } |
| 4733 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4734 | // First and second pieces of information logged by delegates to URLRequests. |
| 4735 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 4736 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 4737 | |
| 4738 | // Logs delegate information to a URLRequest. The first string is logged |
| 4739 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 4740 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 4741 | // another asynchronous call is used to clear the delegate information |
| 4742 | // before calling a callback. The object then deletes itself. |
| 4743 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 4744 | public: |
| 4745 | typedef base::Callback<void()> Callback; |
| 4746 | |
| 4747 | // Each time delegate information is added to the URLRequest, the resulting |
| 4748 | // load state is checked. The expected load state after each request is |
| 4749 | // passed in as an argument. |
| 4750 | static void Run(URLRequest* url_request, |
| 4751 | LoadState expected_first_load_state, |
| 4752 | LoadState expected_second_load_state, |
| 4753 | LoadState expected_third_load_state, |
| 4754 | const Callback& callback) { |
| 4755 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 4756 | url_request, |
| 4757 | expected_first_load_state, |
| 4758 | expected_second_load_state, |
| 4759 | expected_third_load_state, |
| 4760 | callback); |
| 4761 | logger->Start(); |
| 4762 | } |
| 4763 | |
| 4764 | // Checks that the log entries, starting with log_position, contain the |
| 4765 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 4766 | // recorded. Returns the index of entry after the expected number of |
| 4767 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4768 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4769 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4770 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 4771 | if (log_position + 3 >= entries.size()) { |
| 4772 | ADD_FAILURE() << "Not enough log entries"; |
| 4773 | return entries.size(); |
| 4774 | } |
| 4775 | std::string delegate_info; |
| 4776 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4777 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4778 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4779 | &delegate_info)); |
| 4780 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 4781 | |
| 4782 | ++log_position; |
| 4783 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4784 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4785 | |
| 4786 | ++log_position; |
| 4787 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4788 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4789 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4790 | &delegate_info)); |
| 4791 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 4792 | |
| 4793 | ++log_position; |
| 4794 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4795 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4796 | |
| 4797 | return log_position + 1; |
| 4798 | } |
| 4799 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4800 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 4801 | // Returns the position of the end message. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4802 | static size_t ExpectBeforeNetworkEvents(const TestNetLogEntry::List& entries, |
| 4803 | size_t log_position) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4804 | log_position = |
| 4805 | ExpectLogContainsSomewhereAfter(entries, |
| 4806 | log_position, |
| 4807 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4808 | NetLog::PHASE_BEGIN); |
| 4809 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4810 | entries[log_position + 1].type); |
| 4811 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 4812 | return log_position + 1; |
| 4813 | } |
| 4814 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4815 | private: |
| 4816 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 4817 | |
| 4818 | AsyncDelegateLogger(URLRequest* url_request, |
| 4819 | LoadState expected_first_load_state, |
| 4820 | LoadState expected_second_load_state, |
| 4821 | LoadState expected_third_load_state, |
| 4822 | const Callback& callback) |
| 4823 | : url_request_(url_request), |
| 4824 | expected_first_load_state_(expected_first_load_state), |
| 4825 | expected_second_load_state_(expected_second_load_state), |
| 4826 | expected_third_load_state_(expected_third_load_state), |
| 4827 | callback_(callback) { |
| 4828 | } |
| 4829 | |
| 4830 | ~AsyncDelegateLogger() {} |
| 4831 | |
| 4832 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4833 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4834 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4835 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 4836 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4837 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4838 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4842 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4843 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4844 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 4845 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 4846 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4847 | } else { |
| 4848 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4849 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4850 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4851 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4855 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4856 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4857 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 4858 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4859 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4860 | callback_.Run(); |
| 4861 | } |
| 4862 | |
| 4863 | URLRequest* url_request_; |
| 4864 | const int expected_first_load_state_; |
| 4865 | const int expected_second_load_state_; |
| 4866 | const int expected_third_load_state_; |
| 4867 | const Callback callback_; |
| 4868 | |
| 4869 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 4870 | }; |
| 4871 | |
| 4872 | // NetworkDelegate that logs delegate information before a request is started, |
| 4873 | // before headers are sent, when headers are read, and when auth information |
| 4874 | // is requested. Uses AsyncDelegateLogger. |
| 4875 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 4876 | public: |
| 4877 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4878 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4879 | |
| 4880 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4881 | int OnBeforeURLRequest(URLRequest* request, |
| 4882 | const CompletionCallback& callback, |
| 4883 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4884 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 4885 | return RunCallbackAsynchronously(request, callback); |
| 4886 | } |
| 4887 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4888 | int OnBeforeStartTransaction(URLRequest* request, |
| 4889 | const CompletionCallback& callback, |
| 4890 | HttpRequestHeaders* headers) override { |
| 4891 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4892 | return RunCallbackAsynchronously(request, callback); |
| 4893 | } |
| 4894 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4895 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4896 | URLRequest* request, |
| 4897 | const CompletionCallback& callback, |
| 4898 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4899 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4900 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4901 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4902 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4903 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4904 | override_response_headers, |
| 4905 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4906 | return RunCallbackAsynchronously(request, callback); |
| 4907 | } |
| 4908 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4909 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4910 | URLRequest* request, |
| 4911 | const AuthChallengeInfo& auth_info, |
| 4912 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4913 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4914 | AsyncDelegateLogger::Run( |
| 4915 | request, |
| 4916 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4917 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4918 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4919 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4920 | callback, credentials)); |
| 4921 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4922 | } |
| 4923 | |
| 4924 | private: |
| 4925 | static int RunCallbackAsynchronously( |
| 4926 | URLRequest* request, |
| 4927 | const CompletionCallback& callback) { |
| 4928 | AsyncDelegateLogger::Run( |
| 4929 | request, |
| 4930 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4931 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4932 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4933 | base::Bind(callback, OK)); |
| 4934 | return ERR_IO_PENDING; |
| 4935 | } |
| 4936 | |
| 4937 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4938 | AuthCredentials* credentials) { |
| 4939 | *credentials = AuthCredentials(kUser, kSecret); |
| 4940 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4941 | } |
| 4942 | |
| 4943 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4944 | }; |
| 4945 | |
| 4946 | // URLRequest::Delegate that logs delegate information when the headers |
| 4947 | // are received, when each read completes, and during redirects. Uses |
| 4948 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4949 | // |
| 4950 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4951 | // advancing to the next step in most cases, as well as cancellation. |
| 4952 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4953 | public: |
| 4954 | enum CancelStage { |
| 4955 | NO_CANCEL = 0, |
| 4956 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4957 | CANCEL_ON_RESPONSE_STARTED, |
| 4958 | CANCEL_ON_READ_COMPLETED |
| 4959 | }; |
| 4960 | |
| 4961 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4962 | : cancel_stage_(cancel_stage) { |
| 4963 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4964 | set_cancel_in_received_redirect(true); |
| 4965 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4966 | set_cancel_in_response_started(true); |
| 4967 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4968 | set_cancel_in_received_data(true); |
| 4969 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4970 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4971 | |
| 4972 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4973 | void OnReceivedRedirect(URLRequest* request, |
| 4974 | const RedirectInfo& redirect_info, |
| 4975 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4976 | *defer_redirect = true; |
| 4977 | AsyncDelegateLogger::Run( |
| 4978 | request, |
| 4979 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4980 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4981 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4982 | base::Bind( |
| 4983 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4984 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4985 | } |
| 4986 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4987 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4988 | AsyncDelegateLogger::Run( |
| 4989 | request, |
| 4990 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4991 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4992 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4993 | base::Bind( |
| 4994 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4995 | base::Unretained(this), request)); |
| 4996 | } |
| 4997 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4998 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4999 | AsyncDelegateLogger::Run( |
| 5000 | request, |
| 5001 | LOAD_STATE_IDLE, |
| 5002 | LOAD_STATE_IDLE, |
| 5003 | LOAD_STATE_IDLE, |
| 5004 | base::Bind( |
| 5005 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5006 | base::Unretained(this), request, bytes_read)); |
| 5007 | } |
| 5008 | |
| 5009 | private: |
| 5010 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5011 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5012 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5013 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5014 | // FollowDeferredRedirect should not be called after cancellation. |
| 5015 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5016 | return; |
| 5017 | if (!defer_redirect) |
| 5018 | request->FollowDeferredRedirect(); |
| 5019 | } |
| 5020 | |
| 5021 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 5022 | // The parent class continues the request. |
| 5023 | TestDelegate::OnResponseStarted(request); |
| 5024 | } |
| 5025 | |
| 5026 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5027 | // The parent class continues the request. |
| 5028 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5029 | } |
| 5030 | |
| 5031 | const CancelStage cancel_stage_; |
| 5032 | |
| 5033 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5034 | }; |
| 5035 | |
| 5036 | // Tests handling of delegate info before a request starts. |
| 5037 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5038 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5039 | |
| 5040 | TestDelegate request_delegate; |
| 5041 | TestURLRequestContext context(true); |
| 5042 | context.set_network_delegate(NULL); |
| 5043 | context.set_net_log(&net_log_); |
| 5044 | context.Init(); |
| 5045 | |
| 5046 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5047 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5048 | context.CreateRequest(http_test_server()->GetURL("/defaultresponse"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5049 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5050 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5051 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5052 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5053 | |
| 5054 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5055 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5056 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5057 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5058 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5059 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5060 | |
| 5061 | base::RunLoop().Run(); |
| 5062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5063 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5064 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5065 | } |
| 5066 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5067 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5068 | net_log_.GetEntries(&entries); |
| 5069 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 5070 | entries, |
| 5071 | 0, |
| 5072 | NetLog::TYPE_DELEGATE_INFO, |
| 5073 | NetLog::PHASE_BEGIN); |
| 5074 | |
| 5075 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5076 | |
| 5077 | // Nothing else should add any delegate info to the request. |
| 5078 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5079 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5080 | } |
| 5081 | |
| 5082 | // Tests handling of delegate info from a network delegate. |
| 5083 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5084 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5085 | |
| 5086 | TestDelegate request_delegate; |
| 5087 | AsyncLoggingNetworkDelegate network_delegate; |
| 5088 | TestURLRequestContext context(true); |
| 5089 | context.set_network_delegate(&network_delegate); |
| 5090 | context.set_net_log(&net_log_); |
| 5091 | context.Init(); |
| 5092 | |
| 5093 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5094 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5095 | context.CreateRequest(http_test_server()->GetURL("/simple.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5096 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5097 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5098 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5099 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5100 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5101 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5102 | base::RunLoop().Run(); |
| 5103 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5104 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5105 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5106 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5107 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5108 | } |
| 5109 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5110 | |
| 5111 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5112 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5113 | net_log_.GetEntries(&entries); |
| 5114 | for (size_t i = 0; i < 3; ++i) { |
| 5115 | log_position = ExpectLogContainsSomewhereAfter( |
| 5116 | entries, |
| 5117 | log_position + 1, |
| 5118 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5119 | NetLog::PHASE_BEGIN); |
| 5120 | |
| 5121 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5122 | log_position + 1); |
| 5123 | |
| 5124 | ASSERT_LT(log_position, entries.size()); |
| 5125 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5126 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5127 | |
| 5128 | if (i == 1) { |
| 5129 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5130 | entries, log_position + 1); |
| 5131 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5135 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5136 | } |
| 5137 | |
| 5138 | // Tests handling of delegate info from a network delegate in the case of an |
| 5139 | // HTTP redirect. |
| 5140 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5141 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5142 | |
| 5143 | TestDelegate request_delegate; |
| 5144 | AsyncLoggingNetworkDelegate network_delegate; |
| 5145 | TestURLRequestContext context(true); |
| 5146 | context.set_network_delegate(&network_delegate); |
| 5147 | context.set_net_log(&net_log_); |
| 5148 | context.Init(); |
| 5149 | |
| 5150 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5151 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5152 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5153 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5154 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5155 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5156 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5157 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5158 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5159 | base::RunLoop().Run(); |
| 5160 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5161 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5162 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5163 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5164 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5165 | } |
| 5166 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5167 | |
| 5168 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5169 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5170 | net_log_.GetEntries(&entries); |
| 5171 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5172 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5173 | for (size_t i = 0; i < 3; ++i) { |
| 5174 | log_position = ExpectLogContainsSomewhereAfter( |
| 5175 | entries, |
| 5176 | log_position + 1, |
| 5177 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5178 | NetLog::PHASE_BEGIN); |
| 5179 | |
| 5180 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5181 | log_position + 1); |
| 5182 | |
| 5183 | ASSERT_LT(log_position, entries.size()); |
| 5184 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5185 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5186 | |
| 5187 | if (i == 1) { |
| 5188 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5189 | entries, log_position + 1); |
| 5190 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5194 | log_position = ExpectLogContainsSomewhereAfter( |
| 5195 | entries, |
| 5196 | log_position + 1, |
| 5197 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5198 | NetLog::PHASE_BEGIN); |
| 5199 | |
| 5200 | // The NetworkDelegate logged information in the same three events as before. |
| 5201 | for (size_t i = 0; i < 3; ++i) { |
| 5202 | log_position = ExpectLogContainsSomewhereAfter( |
| 5203 | entries, |
| 5204 | log_position + 1, |
| 5205 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5206 | NetLog::PHASE_BEGIN); |
| 5207 | |
| 5208 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5209 | log_position + 1); |
| 5210 | |
| 5211 | ASSERT_LT(log_position, entries.size()); |
| 5212 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5213 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5214 | } |
| 5215 | |
| 5216 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5217 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5218 | } |
| 5219 | |
| 5220 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5221 | // AUTH. |
| 5222 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5223 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5224 | |
| 5225 | TestDelegate request_delegate; |
| 5226 | AsyncLoggingNetworkDelegate network_delegate; |
| 5227 | TestURLRequestContext context(true); |
| 5228 | context.set_network_delegate(&network_delegate); |
| 5229 | context.set_net_log(&net_log_); |
| 5230 | context.Init(); |
| 5231 | |
| 5232 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5233 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5234 | context.CreateRequest(http_test_server()->GetURL("/auth-basic"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5235 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5236 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5237 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5238 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5239 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5240 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5241 | base::RunLoop().Run(); |
| 5242 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5243 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5244 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5245 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5246 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5247 | } |
| 5248 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5249 | |
| 5250 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5251 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5252 | net_log_.GetEntries(&entries); |
| 5253 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5254 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5255 | // in |
| 5256 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5257 | for (size_t i = 0; i < 6; ++i) { |
| 5258 | log_position = ExpectLogContainsSomewhereAfter( |
| 5259 | entries, |
| 5260 | log_position + 1, |
| 5261 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5262 | NetLog::PHASE_BEGIN); |
| 5263 | |
| 5264 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5265 | log_position + 1); |
| 5266 | |
| 5267 | ASSERT_LT(log_position, entries.size()); |
| 5268 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5269 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5270 | |
| 5271 | if (i == 1) { |
| 5272 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5273 | entries, log_position + 1); |
| 5274 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5278 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5279 | } |
| 5280 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5281 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5282 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5283 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5284 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5285 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 5286 | SpawnedTestServer::kLocalhost, |
| 5287 | base::FilePath(kTestFilePath)); |
| 5288 | |
| 5289 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5290 | |
| 5291 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5292 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5293 | TestURLRequestContext context(true); |
| 5294 | context.set_network_delegate(NULL); |
| 5295 | context.set_net_log(&net_log_); |
| 5296 | context.Init(); |
| 5297 | |
| 5298 | { |
| 5299 | // A chunked response with delays between chunks is used to make sure that |
| 5300 | // attempts by the URLRequest delegate to log information while reading the |
| 5301 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5302 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5303 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5304 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5305 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5306 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5307 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5308 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5309 | base::RunLoop().Run(); |
| 5310 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5311 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5312 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5313 | } |
| 5314 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5315 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5316 | net_log_.GetEntries(&entries); |
| 5317 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5318 | size_t log_position = 0; |
| 5319 | |
| 5320 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5321 | entries, log_position); |
| 5322 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5323 | // The delegate info should only have been logged on header complete. Other |
| 5324 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5325 | log_position = |
| 5326 | ExpectLogContainsSomewhereAfter(entries, |
| 5327 | log_position + 1, |
| 5328 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5329 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5330 | |
| 5331 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5332 | log_position + 1); |
| 5333 | |
| 5334 | ASSERT_LT(log_position, entries.size()); |
| 5335 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5336 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5337 | |
| 5338 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5339 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5340 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5341 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5342 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5343 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5344 | |
| 5345 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5346 | // an HTTP redirect. |
| 5347 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5349 | |
| 5350 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5351 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5352 | TestURLRequestContext context(true); |
| 5353 | context.set_network_delegate(NULL); |
| 5354 | context.set_net_log(&net_log_); |
| 5355 | context.Init(); |
| 5356 | |
| 5357 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5358 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5359 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5360 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5361 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5362 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5363 | base::RunLoop().Run(); |
| 5364 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5365 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5366 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5367 | } |
| 5368 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5369 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5370 | net_log_.GetEntries(&entries); |
| 5371 | |
| 5372 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5373 | // OnResponseStarted. |
| 5374 | size_t log_position = 0; |
| 5375 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5376 | if (i == 0) { |
| 5377 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5378 | entries, log_position) + 1; |
| 5379 | } |
| 5380 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5381 | log_position = ExpectLogContainsSomewhereAfter( |
| 5382 | entries, |
| 5383 | log_position, |
| 5384 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5385 | NetLog::PHASE_BEGIN); |
| 5386 | |
| 5387 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5388 | log_position + 1); |
| 5389 | |
| 5390 | ASSERT_LT(log_position, entries.size()); |
| 5391 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5392 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5393 | } |
| 5394 | |
| 5395 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5396 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5397 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5398 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5399 | } |
| 5400 | |
| 5401 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5402 | // an HTTP redirect, with cancellation at various points. |
| 5403 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5404 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5405 | |
| 5406 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5407 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5408 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5409 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5410 | }; |
| 5411 | |
| 5412 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5413 | ++test_case) { |
| 5414 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 5415 | TestURLRequestContext context(true); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5416 | TestNetLog net_log; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5417 | context.set_network_delegate(NULL); |
| 5418 | context.set_net_log(&net_log); |
| 5419 | context.Init(); |
| 5420 | |
| 5421 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5422 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5423 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5424 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5425 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5426 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5427 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5428 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5429 | } |
| 5430 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5431 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5432 | net_log.GetEntries(&entries); |
| 5433 | |
| 5434 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5435 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5436 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5437 | // still currently supported in that call. |
| 5438 | size_t log_position = 0; |
| 5439 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5440 | if (i == 0) { |
| 5441 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5442 | entries, log_position) + 1; |
| 5443 | } |
| 5444 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5445 | log_position = ExpectLogContainsSomewhereAfter( |
| 5446 | entries, |
| 5447 | log_position, |
| 5448 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5449 | NetLog::PHASE_BEGIN); |
| 5450 | |
| 5451 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5452 | log_position + 1); |
| 5453 | |
| 5454 | ASSERT_LT(log_position, entries.size()); |
| 5455 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5456 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5457 | } |
| 5458 | |
| 5459 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5460 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5461 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5462 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5463 | } |
| 5464 | } |
| 5465 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5466 | namespace { |
| 5467 | |
| 5468 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5469 | const char kExtraValue[] = "fubar"; |
| 5470 | |
| 5471 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5472 | void OnReceivedRedirect(URLRequest* request, |
| 5473 | const RedirectInfo& redirect_info, |
| 5474 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5475 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5476 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5477 | } |
| 5478 | }; |
| 5479 | |
| 5480 | } // namespace |
| 5481 | |
| 5482 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5483 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5484 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5485 | GURL destination_url = |
| 5486 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5487 | GURL original_url = |
| 5488 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5489 | RedirectWithAdditionalHeadersDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5490 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5491 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5492 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5493 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5494 | |
| 5495 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5496 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5497 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5498 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5499 | EXPECT_FALSE(req->is_pending()); |
| 5500 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5501 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5502 | } |
| 5503 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5504 | namespace { |
| 5505 | |
| 5506 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5507 | |
| 5508 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5509 | void OnReceivedRedirect(URLRequest* request, |
| 5510 | const RedirectInfo& redirect_info, |
| 5511 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5512 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5513 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5514 | } |
| 5515 | }; |
| 5516 | |
| 5517 | } // namespace |
| 5518 | |
| 5519 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5520 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5521 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5522 | GURL destination_url = http_test_server()->GetURL( |
| 5523 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5524 | GURL original_url = |
| 5525 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5526 | RedirectWithHeaderRemovalDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5527 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5528 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5529 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5530 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5531 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5532 | |
| 5533 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5534 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5535 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5536 | EXPECT_FALSE(req->is_pending()); |
| 5537 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5538 | EXPECT_EQ("None", d.data_received()); |
| 5539 | } |
| 5540 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5541 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 5542 | TestDelegate d; |
| 5543 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5544 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5545 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5547 | r->Start(); |
| 5548 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5549 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5550 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5551 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5552 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5553 | |
| 5554 | // We expect to receive OnResponseStarted even though the request has been |
| 5555 | // cancelled. |
| 5556 | EXPECT_EQ(1, d.response_started_count()); |
| 5557 | EXPECT_EQ(0, d.bytes_received()); |
| 5558 | EXPECT_FALSE(d.received_data_before_response()); |
| 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5563 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5564 | |
| 5565 | TestDelegate d; |
| 5566 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5567 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5568 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5569 | |
| 5570 | d.set_cancel_in_response_started(true); |
| 5571 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5572 | r->Start(); |
| 5573 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5574 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5575 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5576 | |
| 5577 | EXPECT_EQ(1, d.response_started_count()); |
| 5578 | EXPECT_EQ(0, d.bytes_received()); |
| 5579 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5580 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
| 5584 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5585 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5586 | |
| 5587 | TestDelegate d; |
| 5588 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5589 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5590 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5591 | |
| 5592 | d.set_cancel_in_received_data(true); |
| 5593 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5594 | r->Start(); |
| 5595 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5596 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5597 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5598 | |
| 5599 | EXPECT_EQ(1, d.response_started_count()); |
| 5600 | // There is no guarantee about how much data was received |
| 5601 | // before the cancel was issued. It could have been 0 bytes, |
| 5602 | // or it could have been all the bytes. |
| 5603 | // EXPECT_EQ(0, d.bytes_received()); |
| 5604 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5605 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5606 | } |
| 5607 | } |
| 5608 | |
| 5609 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5610 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5611 | |
| 5612 | TestDelegate d; |
| 5613 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5614 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5615 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5616 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5617 | r->Start(); |
| 5618 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5619 | |
| 5620 | // The request will be implicitly canceled when it is destroyed. The |
| 5621 | // test delegate must not post a quit message when this happens because |
| 5622 | // this test doesn't actually have a message loop. The quit message would |
| 5623 | // get put on this thread's message queue and the next test would exit |
| 5624 | // early, causing problems. |
| 5625 | d.set_quit_on_complete(false); |
| 5626 | } |
| 5627 | // expect things to just cleanup properly. |
| 5628 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 5629 | // 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] | 5630 | // message loop |
| 5631 | EXPECT_FALSE(d.received_data_before_response()); |
| 5632 | EXPECT_EQ(0, d.bytes_received()); |
| 5633 | } |
| 5634 | |
| 5635 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5636 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5637 | |
| 5638 | // populate cache |
| 5639 | { |
| 5640 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5641 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5642 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5643 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5644 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5645 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | // cancel read from cache (see bug 990242) |
| 5649 | { |
| 5650 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5651 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5652 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5653 | r->Start(); |
| 5654 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5655 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5656 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5657 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5658 | EXPECT_EQ(1, d.response_started_count()); |
| 5659 | EXPECT_EQ(0, d.bytes_received()); |
| 5660 | EXPECT_FALSE(d.received_data_before_response()); |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5665 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5666 | HTTPUploadDataOperationTest("POST"); |
| 5667 | } |
| 5668 | |
| 5669 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5670 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5671 | HTTPUploadDataOperationTest("PUT"); |
| 5672 | } |
| 5673 | |
| 5674 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5675 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5676 | |
| 5677 | TestDelegate d; |
| 5678 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5679 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5680 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5681 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5682 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5683 | r->Start(); |
| 5684 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5685 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5686 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5687 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5688 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5689 | << "request failed: " << r->status().status() |
| 5690 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5691 | |
| 5692 | EXPECT_FALSE(d.received_data_before_response()); |
| 5693 | EXPECT_TRUE(d.data_received().empty()); |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5698 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5699 | |
| 5700 | TestDelegate d; |
| 5701 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5702 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5703 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5704 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5705 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5706 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5707 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 5708 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5709 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5710 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5711 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5712 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5713 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 5714 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5715 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5716 | element_readers.push_back(base::WrapUnique(new UploadFileElementReader( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5717 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
| 5718 | std::numeric_limits<uint64_t>::max(), base::Time()))); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5719 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5720 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5722 | r->Start(); |
| 5723 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5724 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5725 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5726 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5727 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5728 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 5729 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 5730 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5731 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5732 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 5733 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5734 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5735 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5736 | << "request failed: " << r->status().status() |
| 5737 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5738 | |
| 5739 | EXPECT_FALSE(d.received_data_before_response()); |
| 5740 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5741 | EXPECT_EQ(size, d.bytes_received()); |
| 5742 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5743 | } |
| 5744 | } |
| 5745 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5746 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5747 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5748 | |
| 5749 | TestDelegate d; |
| 5750 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5751 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5752 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5753 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5754 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5755 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5756 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5757 | element_readers.push_back(base::WrapUnique(new UploadFileElementReader( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5758 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5759 | base::FilePath(FILE_PATH_LITERAL( |
| 5760 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5761 | 0, std::numeric_limits<uint64_t>::max(), base::Time()))); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5762 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5763 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5765 | r->Start(); |
| 5766 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5767 | |
| 5768 | base::RunLoop().Run(); |
| 5769 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5770 | EXPECT_TRUE(d.request_failed()); |
| 5771 | EXPECT_FALSE(d.received_data_before_response()); |
| 5772 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5773 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5774 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5775 | } |
| 5776 | } |
| 5777 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5778 | namespace { |
| 5779 | |
| 5780 | // Adds a standard set of data to an upload for chunked upload integration |
| 5781 | // tests. |
| 5782 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 5783 | writer->AppendData("a", 1, false); |
| 5784 | writer->AppendData("bcd", 3, false); |
| 5785 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 5786 | writer->AppendData("\r\n\r\n", 4, false); |
| 5787 | writer->AppendData("0", 1, false); |
| 5788 | writer->AppendData("2323", 4, true); |
| 5789 | } |
| 5790 | |
| 5791 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 5792 | // the server. |
| 5793 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 5794 | // This should match the chunks sent by AddChunksToUpload(). |
| 5795 | const std::string expected_data = |
| 5796 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 5797 | |
| 5798 | ASSERT_EQ(1, d->response_started_count()) |
| 5799 | << "request failed: " << r->status().status() |
| 5800 | << ", os error: " << r->status().error(); |
| 5801 | |
| 5802 | EXPECT_FALSE(d->received_data_before_response()); |
| 5803 | |
| 5804 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 5805 | EXPECT_EQ(expected_data, d->data_received()); |
| 5806 | } |
| 5807 | |
| 5808 | } // namespace |
| 5809 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5810 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5811 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5812 | |
| 5813 | TestDelegate d; |
| 5814 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5815 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5816 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5817 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5818 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5819 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5820 | upload_data_stream->CreateWriter(); |
| 5821 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5822 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5823 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5824 | r->Start(); |
| 5825 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5826 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5827 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5828 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5829 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5830 | } |
| 5831 | } |
| 5832 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5833 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5834 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5835 | |
| 5836 | TestDelegate d; |
| 5837 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5838 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5839 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5840 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5841 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5842 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5843 | upload_data_stream->CreateWriter(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5844 | r->set_upload(base::WrapUnique(upload_data_stream.release())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5845 | r->set_method("POST"); |
| 5846 | r->Start(); |
| 5847 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5848 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5849 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5850 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5851 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5852 | } |
| 5853 | } |
| 5854 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5855 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5856 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5857 | |
| 5858 | TestDelegate d; |
| 5859 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5860 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5861 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5862 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5863 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5864 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5865 | upload_data_stream->CreateWriter(); |
| 5866 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5867 | r->set_method("POST"); |
| 5868 | r->Start(); |
| 5869 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5870 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5871 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5872 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5873 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5874 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5875 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5880 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5881 | |
| 5882 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5883 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5884 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5885 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5886 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5887 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5888 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5889 | |
| 5890 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5891 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5892 | |
| 5893 | std::string header; |
| 5894 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 5895 | EXPECT_EQ("private", header); |
| 5896 | |
| 5897 | header.clear(); |
| 5898 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 5899 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 5900 | |
| 5901 | // The response has two "X-Multiple-Entries" headers. |
| 5902 | // This verfies our output has them concatenated together. |
| 5903 | header.clear(); |
| 5904 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 5905 | EXPECT_EQ("a, b", header); |
| 5906 | } |
| 5907 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5908 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 5909 | // security state. (see http://crbug.com/550977). |
| 5910 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5911 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5912 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5913 | https_test_server.SetSSLConfig( |
| 5914 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5915 | https_test_server.ServeFilesFromSourceDirectory( |
| 5916 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5917 | ASSERT_TRUE(https_test_server.Start()); |
| 5918 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5919 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5920 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5921 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5922 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5923 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5924 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5925 | |
| 5926 | TransportSecurityState* security_state = |
| 5927 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5928 | TransportSecurityState::STSState sts_state; |
| 5929 | TransportSecurityState::PKPState pkp_state; |
| 5930 | EXPECT_TRUE( |
| 5931 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 5932 | EXPECT_FALSE( |
| 5933 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 5934 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 5935 | sts_state.upgrade_mode); |
| 5936 | EXPECT_TRUE(sts_state.include_subdomains); |
| 5937 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5938 | #if defined(OS_ANDROID) |
| 5939 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5940 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5941 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5942 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5943 | } |
| 5944 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5945 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5946 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5947 | https_test_server.ServeFilesFromSourceDirectory( |
| 5948 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5949 | ASSERT_TRUE(https_test_server.Start()); |
| 5950 | // Make sure this test fails if the test server is changed to not |
| 5951 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5952 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 5953 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5954 | |
| 5955 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5956 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5957 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5958 | request->Start(); |
| 5959 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5960 | TransportSecurityState* security_state = |
| 5961 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5962 | TransportSecurityState::STSState sts_state; |
| 5963 | EXPECT_FALSE( |
| 5964 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5965 | } |
| 5966 | |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5967 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 5968 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 5969 | // PKPState present because header rejected). |
| 5970 | #if defined(OS_ANDROID) |
| 5971 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 5972 | #define MAYBE_ProcessPKPAndSendReport DISABLED_ProcessPKPAndSendReport |
| 5973 | #define MAYBE_ProcessPKPReportOnly DISABLED_ProcessPKPReportOnly |
| 5974 | #define MAYBE_ProcessPKPReportOnlyWithNoViolation \ |
| 5975 | DISABLED_ProcessPKPReportOnlyWithNoViolation |
| 5976 | #else |
| 5977 | #define MAYBE_ProcessPKP ProcessPKP |
| 5978 | #define MAYBE_ProcessPKPAndSendReport ProcessPKPAndSendReport |
| 5979 | #define MAYBE_ProcessPKPReportOnly ProcessPKPReportOnly |
| 5980 | #define MAYBE_ProcessPKPReportOnlyWithNoViolation \ |
| 5981 | ProcessPKPReportOnlyWithNoViolation |
| 5982 | #endif |
| 5983 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5984 | namespace { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 5985 | const char kExpectCTStaticHostname[] = "preloaded-expect-ct.badssl.com"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5986 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 5987 | } // namespace |
| 5988 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5989 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5990 | // validity/expiration. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5991 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5992 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5993 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5994 | https_test_server.SetSSLConfig( |
| 5995 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5996 | https_test_server.ServeFilesFromSourceDirectory( |
| 5997 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5998 | ASSERT_TRUE(https_test_server.Start()); |
| 5999 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6000 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6001 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6002 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6003 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6004 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6005 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6006 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6007 | TransportSecurityState* security_state = |
| 6008 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6009 | TransportSecurityState::STSState sts_state; |
| 6010 | TransportSecurityState::PKPState pkp_state; |
| 6011 | EXPECT_FALSE( |
| 6012 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6013 | EXPECT_TRUE( |
| 6014 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6015 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6016 | sts_state.upgrade_mode); |
| 6017 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6018 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6019 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6020 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6021 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6022 | } |
| 6023 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6024 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6025 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6026 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6027 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6028 | https_test_server.SetSSLConfig( |
| 6029 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6030 | https_test_server.ServeFilesFromSourceDirectory( |
| 6031 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6032 | ASSERT_TRUE(https_test_server.Start()); |
| 6033 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6034 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6035 | |
| 6036 | // Set up a pin for |test_server_hostname|. |
| 6037 | TransportSecurityState security_state; |
| 6038 | const base::Time current_time(base::Time::Now()); |
| 6039 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6040 | HashValueVector hashes; |
| 6041 | HashValue hash1; |
| 6042 | HashValue hash2; |
| 6043 | // The values here don't matter, as long as they are different from |
| 6044 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6045 | ASSERT_TRUE( |
| 6046 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6047 | ASSERT_TRUE( |
| 6048 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6049 | hashes.push_back(hash1); |
| 6050 | hashes.push_back(hash2); |
| 6051 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6052 | false, /* include subdomains */ |
| 6053 | hashes, report_uri); |
| 6054 | |
| 6055 | MockCertificateReportSender mock_report_sender; |
| 6056 | security_state.SetReportSender(&mock_report_sender); |
| 6057 | |
| 6058 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6059 | // set pin. |
| 6060 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6061 | ASSERT_TRUE(cert); |
| 6062 | |
| 6063 | MockCertVerifier cert_verifier; |
| 6064 | CertVerifyResult verify_result; |
| 6065 | verify_result.verified_cert = cert; |
| 6066 | verify_result.is_issued_by_known_root = true; |
| 6067 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6068 | ASSERT_TRUE( |
| 6069 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6070 | verify_result.public_key_hashes.push_back(hash3); |
| 6071 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6072 | |
| 6073 | TestNetworkDelegate network_delegate; |
| 6074 | TestURLRequestContext context(true); |
| 6075 | context.set_transport_security_state(&security_state); |
| 6076 | context.set_network_delegate(&network_delegate); |
| 6077 | context.set_cert_verifier(&cert_verifier); |
| 6078 | context.Init(); |
| 6079 | |
| 6080 | // Now send a request to trigger the violation. |
| 6081 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6082 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6083 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6084 | violating_request->Start(); |
| 6085 | base::RunLoop().Run(); |
| 6086 | |
| 6087 | // Check that a report was sent. |
| 6088 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6089 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6090 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6091 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6092 | ASSERT_TRUE(value); |
| 6093 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 6094 | base::DictionaryValue* report_dict; |
| 6095 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6096 | std::string report_hostname; |
| 6097 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6098 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6099 | } |
| 6100 | |
| 6101 | // Tests that reports get sent on requests with |
| 6102 | // Public-Key-Pins-Report-Only headers. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6103 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6104 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6105 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6106 | https_test_server.SetSSLConfig( |
| 6107 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6108 | https_test_server.ServeFilesFromSourceDirectory( |
| 6109 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6110 | ASSERT_TRUE(https_test_server.Start()); |
| 6111 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6112 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6113 | |
| 6114 | TransportSecurityState security_state; |
| 6115 | MockCertificateReportSender mock_report_sender; |
| 6116 | security_state.SetReportSender(&mock_report_sender); |
| 6117 | |
| 6118 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6119 | // header. |
| 6120 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6121 | ASSERT_TRUE(cert); |
| 6122 | |
| 6123 | MockCertVerifier cert_verifier; |
| 6124 | CertVerifyResult verify_result; |
| 6125 | verify_result.verified_cert = cert; |
| 6126 | verify_result.is_issued_by_known_root = true; |
| 6127 | HashValue hash; |
| 6128 | // This value doesn't matter, as long as it is different from the pins |
| 6129 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6130 | ASSERT_TRUE( |
| 6131 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6132 | verify_result.public_key_hashes.push_back(hash); |
| 6133 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6134 | |
| 6135 | TestNetworkDelegate network_delegate; |
| 6136 | TestURLRequestContext context(true); |
| 6137 | context.set_transport_security_state(&security_state); |
| 6138 | context.set_network_delegate(&network_delegate); |
| 6139 | context.set_cert_verifier(&cert_verifier); |
| 6140 | context.Init(); |
| 6141 | |
| 6142 | // Now send a request to trigger the violation. |
| 6143 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6144 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6145 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6146 | DEFAULT_PRIORITY, &d)); |
| 6147 | violating_request->Start(); |
| 6148 | base::RunLoop().Run(); |
| 6149 | |
| 6150 | // Check that a report was sent. |
| 6151 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6152 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6153 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6154 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6155 | ASSERT_TRUE(value); |
| 6156 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 6157 | base::DictionaryValue* report_dict; |
| 6158 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6159 | std::string report_hostname; |
| 6160 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6161 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6162 | } |
| 6163 | |
| 6164 | // Tests that reports do not get sent on requests with |
| 6165 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6166 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6167 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6168 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6169 | https_test_server.SetSSLConfig( |
| 6170 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6171 | https_test_server.ServeFilesFromSourceDirectory( |
| 6172 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6173 | ASSERT_TRUE(https_test_server.Start()); |
| 6174 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6175 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6176 | |
| 6177 | TransportSecurityState security_state; |
| 6178 | MockCertificateReportSender mock_report_sender; |
| 6179 | security_state.SetReportSender(&mock_report_sender); |
| 6180 | |
| 6181 | TestNetworkDelegate network_delegate; |
| 6182 | MockCertVerifier mock_cert_verifier; |
| 6183 | TestURLRequestContext context(true); |
| 6184 | context.set_transport_security_state(&security_state); |
| 6185 | context.set_network_delegate(&network_delegate); |
| 6186 | context.set_cert_verifier(&mock_cert_verifier); |
| 6187 | mock_cert_verifier.set_default_result(OK); |
| 6188 | context.Init(); |
| 6189 | |
| 6190 | // Now send a request that does not trigger the violation. |
| 6191 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6192 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6193 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6194 | DEFAULT_PRIORITY, &d)); |
| 6195 | request->Start(); |
| 6196 | base::RunLoop().Run(); |
| 6197 | |
| 6198 | // Check that a report was not sent. |
| 6199 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6200 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6201 | } |
| 6202 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6203 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6204 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6205 | https_test_server.ServeFilesFromSourceDirectory( |
| 6206 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6207 | ASSERT_TRUE(https_test_server.Start()); |
| 6208 | // Make sure this test fails if the test server is changed to not |
| 6209 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6210 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6211 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6212 | |
| 6213 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6214 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6215 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6216 | request->Start(); |
| 6217 | base::RunLoop().Run(); |
| 6218 | |
| 6219 | TransportSecurityState* security_state = |
| 6220 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6221 | TransportSecurityState::PKPState pkp_state; |
| 6222 | EXPECT_FALSE( |
| 6223 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6224 | } |
| 6225 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6226 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6227 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6228 | https_test_server.SetSSLConfig( |
| 6229 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6230 | https_test_server.ServeFilesFromSourceDirectory( |
| 6231 | base::FilePath(kTestFilePath)); |
| 6232 | ASSERT_TRUE(https_test_server.Start()); |
| 6233 | |
| 6234 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6235 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6236 | ASSERT_TRUE(cert); |
| 6237 | |
| 6238 | MockCertVerifier cert_verifier; |
| 6239 | CertVerifyResult verify_result; |
| 6240 | verify_result.verified_cert = cert; |
| 6241 | verify_result.is_issued_by_known_root = false; |
| 6242 | HashValue hash; |
| 6243 | ASSERT_TRUE( |
| 6244 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6245 | verify_result.public_key_hashes.push_back(hash); |
| 6246 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6247 | cert_verifier.set_default_result(OK); |
| 6248 | |
| 6249 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6250 | |
| 6251 | // Set up HPKP |
| 6252 | base::Time current_time = base::Time::Now(); |
| 6253 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6254 | HashValue pin; |
| 6255 | ASSERT_TRUE( |
| 6256 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6257 | HashValueVector hashes; |
| 6258 | hashes.push_back(pin); |
| 6259 | GURL report_uri(kHPKPReportUri); |
| 6260 | TransportSecurityState security_state; |
| 6261 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6262 | false, /* include subdomains */ |
| 6263 | hashes, report_uri); |
| 6264 | |
| 6265 | TestNetworkDelegate network_delegate; |
| 6266 | TestURLRequestContext context(true); |
| 6267 | context.set_transport_security_state(&security_state); |
| 6268 | context.set_network_delegate(&network_delegate); |
| 6269 | context.set_cert_verifier(&cert_verifier); |
| 6270 | context.Init(); |
| 6271 | |
| 6272 | TestDelegate d; |
| 6273 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6274 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
| 6275 | request->Start(); |
| 6276 | base::RunLoop().Run(); |
| 6277 | |
| 6278 | TransportSecurityState::PKPState pkp_state; |
| 6279 | EXPECT_TRUE( |
| 6280 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6281 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6282 | } |
| 6283 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6284 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6285 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6286 | https_test_server.SetSSLConfig( |
| 6287 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6288 | https_test_server.ServeFilesFromSourceDirectory( |
| 6289 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6290 | ASSERT_TRUE(https_test_server.Start()); |
| 6291 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6292 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6293 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6294 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6295 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6296 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
| 6297 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6298 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6299 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6300 | |
| 6301 | // We should have set parameters from the first header, not the second. |
| 6302 | TransportSecurityState* security_state = |
| 6303 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6304 | TransportSecurityState::STSState sts_state; |
| 6305 | EXPECT_TRUE( |
| 6306 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6307 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6308 | sts_state.upgrade_mode); |
| 6309 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6310 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6311 | } |
| 6312 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6313 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6314 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6315 | https_test_server.SetSSLConfig( |
| 6316 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6317 | https_test_server.ServeFilesFromSourceDirectory( |
| 6318 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6319 | ASSERT_TRUE(https_test_server.Start()); |
| 6320 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6321 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6322 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6323 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6324 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6325 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
| 6326 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6327 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6328 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6329 | |
| 6330 | // We should have set parameters from the first header, not the second. |
| 6331 | TransportSecurityState* security_state = |
| 6332 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6333 | TransportSecurityState::STSState sts_state; |
| 6334 | TransportSecurityState::PKPState pkp_state; |
| 6335 | EXPECT_TRUE( |
| 6336 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6337 | EXPECT_TRUE( |
| 6338 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6339 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6340 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6341 | #if defined(OS_ANDROID) |
| 6342 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6343 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6344 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6345 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6346 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6347 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6348 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6349 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6350 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6351 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6352 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6353 | } |
| 6354 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6355 | // Tests that when multiple HPKP headers are present, asserting different |
| 6356 | // policies, that only the first such policy is processed. |
| 6357 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6358 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6359 | https_test_server.SetSSLConfig( |
| 6360 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6361 | https_test_server.ServeFilesFromSourceDirectory( |
| 6362 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6363 | ASSERT_TRUE(https_test_server.Start()); |
| 6364 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6365 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6366 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6367 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6368 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6369 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6370 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6371 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6372 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6373 | |
| 6374 | TransportSecurityState* security_state = |
| 6375 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6376 | TransportSecurityState::STSState sts_state; |
| 6377 | TransportSecurityState::PKPState pkp_state; |
| 6378 | EXPECT_TRUE( |
| 6379 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6380 | EXPECT_TRUE( |
| 6381 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6382 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6383 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6384 | #if defined(OS_ANDROID) |
| 6385 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6386 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6387 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6388 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6389 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6390 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6391 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6392 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6393 | } |
| 6394 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6395 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6396 | // called. |
| 6397 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6398 | public: |
| 6399 | MockExpectCTReporter() : num_failures_(0) {} |
| 6400 | ~MockExpectCTReporter() override {} |
| 6401 | |
| 6402 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6403 | const GURL& report_uri, |
| 6404 | const net::SSLInfo& ssl_info) override { |
| 6405 | num_failures_++; |
| 6406 | } |
| 6407 | |
| 6408 | uint32_t num_failures() { return num_failures_; } |
| 6409 | |
| 6410 | private: |
| 6411 | uint32_t num_failures_; |
| 6412 | }; |
| 6413 | |
| 6414 | // A CTVerifier that returns net::OK for every certificate. |
| 6415 | class MockCTVerifier : public CTVerifier { |
| 6416 | public: |
| 6417 | MockCTVerifier() {} |
| 6418 | ~MockCTVerifier() override {} |
| 6419 | |
| 6420 | int Verify(X509Certificate* cert, |
| 6421 | const std::string& stapled_ocsp_response, |
| 6422 | const std::string& sct_list_from_tls_extension, |
| 6423 | ct::CTVerifyResult* result, |
| 6424 | const BoundNetLog& net_log) override { |
| 6425 | return net::OK; |
| 6426 | } |
| 6427 | |
| 6428 | void SetObserver(Observer* observer) override {} |
| 6429 | }; |
| 6430 | |
| 6431 | // A CTPolicyEnforcer that returns a default CertPolicyCompliance value |
| 6432 | // for every certificate. |
| 6433 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6434 | public: |
| 6435 | MockCTPolicyEnforcer() |
| 6436 | : default_result_( |
| 6437 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS) {} |
| 6438 | ~MockCTPolicyEnforcer() override {} |
| 6439 | |
| 6440 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 6441 | X509Certificate* cert, |
| 6442 | const SCTList& verified_scts, |
| 6443 | const BoundNetLog& net_log) override { |
| 6444 | return default_result_; |
| 6445 | } |
| 6446 | |
| 6447 | void set_default_result(ct::CertPolicyCompliance default_result) { |
| 6448 | default_result_ = default_result; |
| 6449 | } |
| 6450 | |
| 6451 | private: |
| 6452 | ct::CertPolicyCompliance default_result_; |
| 6453 | }; |
| 6454 | |
| 6455 | // Tests that Expect CT headers are processed correctly. |
| 6456 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6457 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6458 | https_test_server.SetSSLConfig( |
| 6459 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6460 | https_test_server.ServeFilesFromSourceDirectory( |
| 6461 | base::FilePath(kTestFilePath)); |
| 6462 | ASSERT_TRUE(https_test_server.Start()); |
| 6463 | |
| 6464 | MockExpectCTReporter reporter; |
| 6465 | TransportSecurityState transport_security_state; |
| 6466 | transport_security_state.enable_static_expect_ct_ = true; |
| 6467 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6468 | |
| 6469 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6470 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6471 | ASSERT_TRUE(cert); |
| 6472 | MockCertVerifier cert_verifier; |
| 6473 | CertVerifyResult verify_result; |
| 6474 | verify_result.verified_cert = cert; |
| 6475 | verify_result.is_issued_by_known_root = true; |
| 6476 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6477 | |
| 6478 | // Set up a MockCTVerifier and MockCTPolicyEnforcer to trigger an Expect CT |
| 6479 | // violation. |
| 6480 | MockCTVerifier ct_verifier; |
| 6481 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6482 | ct_policy_enforcer.set_default_result( |
| 6483 | ct::CertPolicyCompliance::CERT_POLICY_NOT_ENOUGH_SCTS); |
| 6484 | |
| 6485 | TestNetworkDelegate network_delegate; |
| 6486 | // Use a MockHostResolver (which by default maps all hosts to |
| 6487 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6488 | // CT preload list. |
| 6489 | MockHostResolver host_resolver; |
| 6490 | TestURLRequestContext context(true); |
| 6491 | context.set_host_resolver(&host_resolver); |
| 6492 | context.set_transport_security_state(&transport_security_state); |
| 6493 | context.set_network_delegate(&network_delegate); |
| 6494 | context.set_cert_verifier(&cert_verifier); |
| 6495 | context.set_cert_transparency_verifier(&ct_verifier); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 6496 | context.set_ct_policy_enforcer(std::move(&ct_policy_enforcer)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6497 | context.Init(); |
| 6498 | |
| 6499 | // Now send a request to trigger the violation. |
| 6500 | TestDelegate d; |
| 6501 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6502 | GURL::Replacements replace_host; |
| 6503 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6504 | url = url.ReplaceComponents(replace_host); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6505 | std::unique_ptr<URLRequest> violating_request( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6506 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| 6507 | violating_request->Start(); |
| 6508 | base::RunLoop().Run(); |
| 6509 | |
| 6510 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6511 | } |
| 6512 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6513 | #endif // !defined(OS_IOS) |
| 6514 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6515 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6516 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6517 | |
| 6518 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6519 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6520 | http_test_server()->GetURL("/content-type-normalization.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6521 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6522 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6523 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6524 | |
| 6525 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6526 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6527 | EXPECT_EQ("text/html", mime_type); |
| 6528 | |
| 6529 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6530 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6531 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6532 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6533 | } |
| 6534 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6535 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6536 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6537 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6538 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6539 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6540 | |
| 6541 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6542 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6543 | } |
| 6544 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6545 | #if !defined(DISABLE_FILE_SUPPORT) |
| 6546 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 6547 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 6548 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6549 | FileProtocolHandler file_protocol_handler( |
| 6550 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6551 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 6552 | |
| 6553 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6554 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6555 | } |
| 6556 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6557 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6558 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6559 | |
| 6560 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6561 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6562 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6563 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6564 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6565 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6566 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6567 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6568 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6569 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6570 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6571 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6572 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6573 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6574 | |
| 6575 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6576 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6577 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6578 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6579 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 6580 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6581 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6582 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6583 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6584 | } |
| 6585 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6586 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6587 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6588 | |
| 6589 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6590 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6591 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6592 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6593 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6594 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6595 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6596 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6597 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6598 | } |
| 6599 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6600 | // Make sure redirects are cached, despite not reading their bodies. |
| 6601 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6603 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6604 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6605 | |
| 6606 | { |
| 6607 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6608 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6609 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6610 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6611 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6612 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6613 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6614 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6615 | } |
| 6616 | |
| 6617 | { |
| 6618 | TestDelegate d; |
| 6619 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6620 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6621 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6622 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6623 | base::RunLoop().Run(); |
| 6624 | |
| 6625 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6626 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6627 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6628 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6629 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6630 | base::RunLoop().Run(); |
| 6631 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6632 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6633 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6634 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 6639 | // when the headers are read, since the body won't have been read. |
| 6640 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6641 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6642 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6643 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6644 | |
| 6645 | { |
| 6646 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6647 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6648 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 6649 | redirect_to_url); |
| 6650 | |
| 6651 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6652 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6653 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6654 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6655 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6656 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6657 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6658 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6659 | } |
| 6660 | |
| 6661 | { |
| 6662 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6663 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6664 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6665 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6666 | base::RunLoop().Run(); |
| 6667 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6668 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 6669 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6670 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6671 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6672 | } |
| 6673 | } |
| 6674 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6675 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 6676 | // safe. |
| 6677 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6678 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6679 | |
| 6680 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 6681 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6682 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6683 | |
| 6684 | TestDelegate d; |
| 6685 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6686 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6687 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6688 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6689 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6690 | base::RunLoop().Run(); |
| 6691 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6692 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6693 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6694 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6695 | EXPECT_EQ(OK, r->status().error()); |
| 6696 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6697 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 6698 | } |
| 6699 | } |
| 6700 | |
| 6701 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 6702 | // some other URL to the whitelist. |
| 6703 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6704 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6705 | |
| 6706 | GURL unsafe_url("data:text/html,something"); |
| 6707 | GURL different_unsafe_url("data:text/html,something-else"); |
| 6708 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6709 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 6710 | different_unsafe_url); |
| 6711 | |
| 6712 | TestDelegate d; |
| 6713 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6714 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6715 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6716 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6717 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6718 | base::RunLoop().Run(); |
| 6719 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6720 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 6721 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6722 | } |
| 6723 | } |
| 6724 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6725 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 6726 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 6727 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6728 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6729 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6730 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6731 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6732 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6733 | |
| 6734 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6735 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6736 | |
| 6737 | TestDelegate d; |
| 6738 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6739 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6740 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6741 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6742 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6743 | base::RunLoop().Run(); |
| 6744 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6745 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6746 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6747 | EXPECT_EQ(OK, r->status().error()); |
| 6748 | EXPECT_EQ(original_url, r->original_url()); |
| 6749 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6750 | } |
| 6751 | } |
| 6752 | |
| 6753 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 6754 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6755 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6756 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6757 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6758 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6759 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6760 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 6761 | GURL expected_redirect_url( |
| 6762 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6763 | |
| 6764 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6765 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6766 | |
| 6767 | TestDelegate d; |
| 6768 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6769 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6770 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6771 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6772 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6773 | base::RunLoop().Run(); |
| 6774 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6775 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6776 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6777 | EXPECT_EQ(OK, r->status().error()); |
| 6778 | EXPECT_EQ(original_url, r->original_url()); |
| 6779 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6780 | } |
| 6781 | } |
| 6782 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6783 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 6784 | // URL should not be changed. In particular, the reference fragment should not |
| 6785 | // be modified. |
| 6786 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6787 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6788 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6789 | GURL original_url( |
| 6790 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6791 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 6792 | |
| 6793 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6794 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 6795 | |
| 6796 | TestDelegate d; |
| 6797 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6798 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6799 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6800 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6801 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6802 | base::RunLoop().Run(); |
| 6803 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6804 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6805 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6806 | EXPECT_EQ(OK, r->status().error()); |
| 6807 | EXPECT_EQ(original_url, r->original_url()); |
| 6808 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6809 | } |
| 6810 | } |
| 6811 | |
| 6812 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 6813 | // the reference fragment of the target URL must not be modified. |
| 6814 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6815 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6816 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6817 | GURL original_url( |
| 6818 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 6819 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6820 | |
| 6821 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6822 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6823 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6824 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6825 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6826 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 6827 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 6828 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6829 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6830 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6831 | base::RunLoop().Run(); |
| 6832 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6833 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6834 | EXPECT_EQ(OK, r->status().error()); |
| 6835 | EXPECT_EQ(original_url, r->original_url()); |
| 6836 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6837 | } |
| 6838 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 6839 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 6840 | ASSERT_TRUE(http_test_server()->Start()); |
| 6841 | |
| 6842 | const std::string referrer("foobar://totally.legit.referrer"); |
| 6843 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6844 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 6845 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
| 6846 | req->SetReferrer(referrer); |
| 6847 | req->Start(); |
| 6848 | base::RunLoop().Run(); |
| 6849 | |
| 6850 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 6851 | } |
| 6852 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6853 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6854 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6855 | |
| 6856 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6857 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6858 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6859 | req->SetReferrer("http://user:[email protected]/"); |
| 6860 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6861 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6862 | |
| 6863 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 6864 | } |
| 6865 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6866 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6867 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6868 | |
| 6869 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6870 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6871 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6872 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6873 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6874 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6875 | |
| 6876 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 6877 | } |
| 6878 | |
| 6879 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6880 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6881 | |
| 6882 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6883 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6884 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6885 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6886 | req->SetReferrer(""); |
| 6887 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6888 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6889 | |
| 6890 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 6891 | } |
| 6892 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6893 | // Defer network start and then resume, checking that the request was a success |
| 6894 | // and bytes were received. |
| 6895 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6896 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6897 | |
| 6898 | TestDelegate d; |
| 6899 | { |
| 6900 | d.set_quit_on_network_start(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6901 | GURL test_url(http_test_server()->GetURL("/echo")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6902 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6903 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6904 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6905 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6906 | base::RunLoop().Run(); |
| 6907 | |
| 6908 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6909 | EXPECT_EQ(0, d.response_started_count()); |
| 6910 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6911 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6912 | base::RunLoop().Run(); |
| 6913 | |
| 6914 | EXPECT_EQ(1, d.response_started_count()); |
| 6915 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6916 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | |
| 6920 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 6921 | // redirect. |
| 6922 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6924 | |
| 6925 | TestDelegate d; |
| 6926 | { |
| 6927 | d.set_quit_on_redirect(true); |
| 6928 | d.set_quit_on_network_start(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6929 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6930 | http_test_server()->GetURL("/server-redirect?echo"), DEFAULT_PRIORITY, |
| 6931 | &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6932 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6933 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6934 | base::RunLoop().Run(); |
| 6935 | |
| 6936 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6937 | EXPECT_EQ(0, d.response_started_count()); |
| 6938 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6940 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6941 | base::RunLoop().Run(); |
| 6942 | |
| 6943 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6944 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6945 | base::RunLoop().Run(); |
| 6946 | |
| 6947 | // Check that the redirect's new network transaction does not get propagated |
| 6948 | // to a second OnBeforeNetworkStart() notification. |
| 6949 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6950 | |
| 6951 | EXPECT_EQ(1, d.response_started_count()); |
| 6952 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6953 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6954 | } |
| 6955 | } |
| 6956 | |
| 6957 | // Cancel the request after learning that the request would use the network. |
| 6958 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6959 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6960 | |
| 6961 | TestDelegate d; |
| 6962 | { |
| 6963 | d.set_quit_on_network_start(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6964 | GURL test_url(http_test_server()->GetURL("/echo")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6965 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6966 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6967 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6968 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6969 | base::RunLoop().Run(); |
| 6970 | |
| 6971 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6972 | EXPECT_EQ(0, d.response_started_count()); |
| 6973 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6974 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6975 | base::RunLoop().Run(); |
| 6976 | |
| 6977 | EXPECT_EQ(1, d.response_started_count()); |
| 6978 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6979 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6980 | } |
| 6981 | } |
| 6982 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6983 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6984 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6985 | |
| 6986 | TestDelegate d; |
| 6987 | { |
| 6988 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6989 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6990 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 6991 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6992 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6993 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6994 | |
| 6995 | EXPECT_EQ(1, d.response_started_count()); |
| 6996 | EXPECT_EQ(0, d.bytes_received()); |
| 6997 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6998 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6999 | } |
| 7000 | } |
| 7001 | |
| 7002 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7003 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7004 | |
| 7005 | TestDelegate d; |
| 7006 | { |
| 7007 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7008 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7009 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7010 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7011 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7012 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7013 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7014 | |
| 7015 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7016 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7017 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7018 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7019 | |
| 7020 | EXPECT_EQ(1, d.response_started_count()); |
| 7021 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7022 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7023 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7024 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7025 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7026 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7027 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7028 | |
| 7029 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7030 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7031 | EXPECT_EQ(contents, d.data_received()); |
| 7032 | } |
| 7033 | } |
| 7034 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7035 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7036 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7037 | |
| 7038 | TestDelegate d; |
| 7039 | { |
| 7040 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7041 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7042 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7043 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7044 | |
| 7045 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7046 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7047 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7048 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7049 | |
| 7050 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7051 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7052 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7053 | d.ClearFullRequestHeaders(); |
| 7054 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7055 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7056 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7057 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7058 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7059 | EXPECT_EQ(1, d.response_started_count()); |
| 7060 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7061 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7062 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7063 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7064 | |
| 7065 | base::FilePath path; |
| 7066 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7067 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7068 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7069 | |
| 7070 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7071 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7072 | EXPECT_EQ(contents, d.data_received()); |
| 7073 | } |
| 7074 | } |
| 7075 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7076 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7077 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7078 | |
| 7079 | TestDelegate d; |
| 7080 | { |
| 7081 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7082 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7083 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 7084 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7085 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7086 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7087 | |
| 7088 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7089 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7090 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7091 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7092 | |
| 7093 | EXPECT_EQ(1, d.response_started_count()); |
| 7094 | EXPECT_EQ(0, d.bytes_received()); |
| 7095 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7096 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7101 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7102 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7103 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7104 | { |
| 7105 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7106 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7107 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 7108 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7109 | HttpRequestHeaders headers; |
| 7110 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7111 | req->SetExtraRequestHeaders(headers); |
| 7112 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7113 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7114 | |
| 7115 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7116 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7117 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7118 | } |
| 7119 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7120 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7121 | { |
| 7122 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7123 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7124 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 7125 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7126 | HttpRequestHeaders headers; |
| 7127 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7128 | req->SetExtraRequestHeaders(headers); |
| 7129 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7130 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7131 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7132 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7133 | |
| 7134 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7135 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7136 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7137 | } |
| 7138 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7139 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7140 | { |
| 7141 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7142 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7143 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 7144 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7145 | HttpRequestHeaders headers; |
| 7146 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7147 | req->SetExtraRequestHeaders(headers); |
| 7148 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7149 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7150 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7151 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7152 | |
| 7153 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7154 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7155 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7156 | } |
| 7157 | } |
| 7158 | |
| 7159 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7160 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7161 | |
| 7162 | // populate the cache |
| 7163 | { |
| 7164 | TestDelegate d; |
| 7165 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7166 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7167 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7168 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7169 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7170 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7171 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7172 | |
| 7173 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7174 | } |
| 7175 | |
| 7176 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7177 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7178 | // response should be fetched from the cache. |
| 7179 | { |
| 7180 | TestDelegate d; |
| 7181 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7182 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7183 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7184 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7185 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7186 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7187 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7188 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7189 | |
| 7190 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7191 | |
| 7192 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7193 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7194 | } |
| 7195 | } |
| 7196 | |
| 7197 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7198 | // http://crbug.com/6450 |
| 7199 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7200 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7201 | |
| 7202 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7203 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7204 | |
| 7205 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7206 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7207 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7208 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7209 | TestURLRequestContext context(true); |
| 7210 | context.set_network_delegate(&network_delegate); |
| 7211 | context.Init(); |
| 7212 | |
| 7213 | TestDelegate d; |
| 7214 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7215 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7216 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7217 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7218 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7219 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7220 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7221 | |
| 7222 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7223 | |
| 7224 | // Make sure we sent the cookie in the restarted transaction. |
| 7225 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7226 | != std::string::npos); |
| 7227 | } |
| 7228 | |
| 7229 | // Same test as above, except this time the restart is initiated earlier |
| 7230 | // (without user intervention since identity is embedded in the URL). |
| 7231 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7232 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7233 | TestURLRequestContext context(true); |
| 7234 | context.set_network_delegate(&network_delegate); |
| 7235 | context.Init(); |
| 7236 | |
| 7237 | TestDelegate d; |
| 7238 | |
| 7239 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 7240 | replacements.SetUsernameStr("user2"); |
| 7241 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7242 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 7243 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7244 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7245 | context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7246 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7247 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7248 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7249 | |
| 7250 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 7251 | |
| 7252 | // Make sure we sent the cookie in the restarted transaction. |
| 7253 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7254 | != std::string::npos); |
| 7255 | } |
| 7256 | } |
| 7257 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7258 | // Tests that load timing works as expected with auth and the cache. |
| 7259 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7260 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7261 | |
| 7262 | // populate the cache |
| 7263 | { |
| 7264 | TestDelegate d; |
| 7265 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7266 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7267 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7268 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7269 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7270 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7271 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7272 | |
| 7273 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7274 | |
| 7275 | LoadTimingInfo load_timing_info_before_auth; |
| 7276 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 7277 | &load_timing_info_before_auth)); |
| 7278 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 7279 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 7280 | |
| 7281 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7282 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7283 | // The test server does not support keep alive sockets, so the second |
| 7284 | // request with auth should use a new socket. |
| 7285 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 7286 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 7287 | load_timing_info.socket_log_id); |
| 7288 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 7289 | load_timing_info.connect_timing.connect_start); |
| 7290 | } |
| 7291 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7292 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 7293 | // 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] | 7294 | // response should be fetched from the cache. |
| 7295 | { |
| 7296 | TestDelegate d; |
| 7297 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7298 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7299 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7300 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7301 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7302 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7303 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7304 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7305 | |
| 7306 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7307 | |
| 7308 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7309 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7310 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7311 | // Since there was a request that went over the wire, the load timing |
| 7312 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7313 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7314 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7315 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7316 | } |
| 7317 | } |
| 7318 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7319 | // In this test, we do a POST which the server will 302 redirect. |
| 7320 | // The subsequent transaction should use GET, and should not send the |
| 7321 | // Content-Type header. |
| 7322 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 7323 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7324 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7325 | |
| 7326 | const char kData[] = "hello world"; |
| 7327 | |
| 7328 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7329 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7330 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, |
| 7331 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7332 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7333 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7334 | |
| 7335 | // Set headers (some of which are specific to the POST). |
| 7336 | HttpRequestHeaders headers; |
| 7337 | headers.AddHeadersFromString( |
| 7338 | "Content-Type: multipart/form-data; " |
| 7339 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 7340 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 7341 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 7342 | "Accept-Language: en-US,en\r\n" |
| 7343 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 7344 | "Content-Length: 11\r\n" |
| 7345 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7346 | req->SetExtraRequestHeaders(headers); |
| 7347 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7348 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7349 | |
| 7350 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7351 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7352 | EXPECT_EQ("text/html", mime_type); |
| 7353 | |
| 7354 | const std::string& data = d.data_received(); |
| 7355 | |
| 7356 | // Check that the post-specific headers were stripped: |
| 7357 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 7358 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 7359 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 7360 | |
| 7361 | // These extra request headers should not have been stripped. |
| 7362 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 7363 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 7364 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 7365 | } |
| 7366 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7367 | // The following tests check that we handle mutating the request for HTTP |
| 7368 | // redirects as expected. |
| 7369 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 7370 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7371 | |
| 7372 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7373 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7374 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7375 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7376 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7377 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7378 | |
| 7379 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7380 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7381 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7382 | |
| 7383 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7384 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7385 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7386 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7387 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7388 | } |
| 7389 | |
| 7390 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7391 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7392 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7393 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7394 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7395 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7396 | |
| 7397 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7398 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7399 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7400 | |
| 7401 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7402 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7403 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7404 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7405 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7409 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7410 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7411 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7412 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7413 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7414 | |
| 7415 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7416 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 7417 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7418 | |
| 7419 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7420 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7421 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7422 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7423 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7424 | } |
| 7425 | |
| 7426 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7427 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7428 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7429 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7430 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7431 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7432 | |
| 7433 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7434 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7435 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7436 | |
| 7437 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7438 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7439 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7440 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7441 | } |
| 7442 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7443 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7444 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7445 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7446 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7447 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7448 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7449 | |
| 7450 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7451 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7452 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7453 | |
| 7454 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7455 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7456 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7457 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 7461 | // Certain legacy apis that pre-date the response code expect this behavior |
| 7462 | // (Like Google Drive). |
| 7463 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7464 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7465 | |
| 7466 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7467 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7468 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7469 | std::unique_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7470 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7471 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7472 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7473 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7474 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 7475 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7476 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7477 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7478 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 7479 | } |
| 7480 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7481 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7482 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7483 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7484 | GURL original_url( |
| 7485 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 7486 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7487 | |
| 7488 | TestDelegate d; |
| 7489 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7490 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7491 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7492 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7493 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7494 | base::RunLoop().Run(); |
| 7495 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7496 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7497 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7498 | EXPECT_EQ(OK, r->status().error()); |
| 7499 | EXPECT_EQ(original_url, r->original_url()); |
| 7500 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7501 | } |
| 7502 | } |
| 7503 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7504 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7505 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7506 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7507 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7508 | GURL first_party_url("http://example.com"); |
| 7509 | |
| 7510 | TestDelegate d; |
| 7511 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7512 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7513 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7514 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7515 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7516 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7517 | base::RunLoop().Run(); |
| 7518 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7519 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7520 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7521 | EXPECT_EQ(OK, r->status().error()); |
| 7522 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7523 | } |
| 7524 | } |
| 7525 | |
| 7526 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7527 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7528 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7529 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7530 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7531 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7532 | |
| 7533 | TestDelegate d; |
| 7534 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7535 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7536 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7537 | r->set_first_party_for_cookies(original_first_party_url); |
| 7538 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7539 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 7540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7541 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7542 | base::RunLoop().Run(); |
| 7543 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7544 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7545 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7546 | EXPECT_EQ(OK, r->status().error()); |
| 7547 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7548 | } |
| 7549 | } |
| 7550 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7551 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7552 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7553 | |
| 7554 | const char kData[] = "hello world"; |
| 7555 | |
| 7556 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7557 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7558 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7559 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7560 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7561 | HttpRequestHeaders headers; |
| 7562 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7563 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7564 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7565 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7566 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7567 | req.get(), &default_network_delegate_, |
| 7568 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7569 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7570 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7571 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7572 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7573 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7574 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7578 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7579 | |
| 7580 | const char kData[] = "hello world"; |
| 7581 | |
| 7582 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7583 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7584 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7585 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7586 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7587 | HttpRequestHeaders headers; |
| 7588 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7589 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7590 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7591 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7592 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7593 | req.get(), &default_network_delegate_, |
| 7594 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 7595 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7596 | "Very Good Reason")); |
| 7597 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7598 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7599 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7600 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7601 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7602 | EXPECT_EQ(kData, d.data_received()); |
| 7603 | } |
| 7604 | |
| 7605 | // Check that default A-L header is sent. |
| 7606 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7607 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7608 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7609 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7610 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7611 | TestURLRequestContext context(true); |
| 7612 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7613 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7614 | context.Init(); |
| 7615 | |
| 7616 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7617 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7618 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7619 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7620 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7621 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7622 | EXPECT_EQ("en", d.data_received()); |
| 7623 | } |
| 7624 | |
| 7625 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 7626 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7627 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7628 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7629 | std::string empty_string; // Avoid most vexing parse on line below. |
| 7630 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7631 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7632 | TestURLRequestContext context(true); |
| 7633 | context.set_network_delegate(&network_delegate); |
| 7634 | context.Init(); |
| 7635 | // We override the language after initialization because empty entries |
| 7636 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7637 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7638 | |
| 7639 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7640 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7641 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7642 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7643 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7644 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7645 | EXPECT_EQ("None", d.data_received()); |
| 7646 | } |
| 7647 | |
| 7648 | // Check that if request overrides the A-L header, the default is not appended. |
| 7649 | // See http://crbug.com/20894 |
| 7650 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7651 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7652 | |
| 7653 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7654 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7655 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7656 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7657 | HttpRequestHeaders headers; |
| 7658 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7659 | req->SetExtraRequestHeaders(headers); |
| 7660 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7661 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7662 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 7663 | } |
| 7664 | |
| 7665 | // Check that default A-E header is sent. |
| 7666 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7667 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7668 | |
| 7669 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7670 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7671 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
| 7672 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7673 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7674 | req->SetExtraRequestHeaders(headers); |
| 7675 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7676 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7677 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 7678 | } |
| 7679 | |
| 7680 | // Check that if request overrides the A-E header, the default is not appended. |
| 7681 | // See http://crbug.com/47381 |
| 7682 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7683 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7684 | |
| 7685 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7686 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7687 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
| 7688 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7689 | HttpRequestHeaders headers; |
| 7690 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7691 | req->SetExtraRequestHeaders(headers); |
| 7692 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7693 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7694 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 7695 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 7696 | } |
| 7697 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 7698 | // Check that setting the A-C header sends the proper header. |
| 7699 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7700 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7701 | |
| 7702 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7703 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7704 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
| 7705 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7706 | HttpRequestHeaders headers; |
| 7707 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7708 | req->SetExtraRequestHeaders(headers); |
| 7709 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7710 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7711 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 7712 | } |
| 7713 | |
| 7714 | // Check that default User-Agent header is sent. |
| 7715 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7716 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7717 | |
| 7718 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7719 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7720 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7721 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7722 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7723 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7724 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 7725 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7726 | } |
| 7727 | |
| 7728 | // Check that if request overrides the User-Agent header, |
| 7729 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 7730 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 7731 | #if defined(OS_IOS) |
| 7732 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 7733 | #else |
| 7734 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 7735 | #endif |
| 7736 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7737 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7738 | |
| 7739 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7740 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7741 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7742 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7743 | HttpRequestHeaders headers; |
| 7744 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7745 | req->SetExtraRequestHeaders(headers); |
| 7746 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7747 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 7748 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7749 | } |
| 7750 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7751 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 7752 | // User-Agent header to be sent but does not send the Accept-Language and |
| 7753 | // Accept-Charset headers. |
| 7754 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7755 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7756 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7757 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7758 | TestURLRequestContext context(true); |
| 7759 | context.set_network_delegate(&network_delegate); |
| 7760 | context.Init(); |
| 7761 | // We override the HttpUserAgentSettings after initialization because empty |
| 7762 | // entries get overridden by Init(). |
| 7763 | context.set_http_user_agent_settings(NULL); |
| 7764 | |
| 7765 | struct { |
| 7766 | const char* request; |
| 7767 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7768 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 7769 | {"/echoheader?Accept-Charset", "None"}, |
| 7770 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7771 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 7772 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7773 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7774 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7775 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7776 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7777 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7778 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 7779 | << " Request = \"" << tests[i].request << "\""; |
| 7780 | } |
| 7781 | } |
| 7782 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7783 | // Make sure that URLRequest passes on its priority updates to |
| 7784 | // newly-created jobs after the first one. |
| 7785 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7786 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7787 | |
| 7788 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7789 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7790 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7791 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7792 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7793 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7794 | req.get(), &default_network_delegate_, |
| 7795 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7796 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7797 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7798 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7799 | req->SetPriority(LOW); |
| 7800 | req->Start(); |
| 7801 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7802 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7803 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7804 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7805 | req.get(), &default_network_delegate_, &job_priority)); |
| 7806 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7807 | |
| 7808 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7809 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7810 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7811 | } |
| 7812 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7813 | // Check that creating a network request while entering/exiting suspend mode |
| 7814 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 7815 | // does not return an HttpTransaction. |
| 7816 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 7817 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7818 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7819 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7820 | network_layer->OnSuspend(); |
| 7821 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7822 | HttpCache http_cache(std::move(network_layer), |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 7823 | HttpCache::DefaultBackend::InMemory(0), true); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7824 | |
| 7825 | TestURLRequestContext context(true); |
| 7826 | context.set_http_transaction_factory(&http_cache); |
| 7827 | context.Init(); |
| 7828 | |
| 7829 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7830 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7831 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7832 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7833 | base::RunLoop().Run(); |
| 7834 | |
| 7835 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7836 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 7837 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7838 | } |
| 7839 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7840 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7841 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7842 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 7843 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 7844 | public: |
| 7845 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 7846 | : network_session_(network_session) {} |
| 7847 | |
| 7848 | ~FailingHttpTransactionFactory() override {} |
| 7849 | |
| 7850 | // HttpTransactionFactory methods: |
| 7851 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7852 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7853 | return ERR_FAILED; |
| 7854 | } |
| 7855 | |
| 7856 | HttpCache* GetCache() override { return nullptr; } |
| 7857 | |
| 7858 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 7859 | |
| 7860 | private: |
| 7861 | HttpNetworkSession* network_session_; |
| 7862 | |
| 7863 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 7864 | }; |
| 7865 | |
| 7866 | } // namespace |
| 7867 | |
| 7868 | // Check that when a request that fails to create an HttpTransaction can be |
| 7869 | // cancelled while the failure notification is pending, and doesn't send two |
| 7870 | // failure notifications. |
| 7871 | // |
| 7872 | // This currently only happens when in suspend mode and there's no cache, but |
| 7873 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 7874 | // behaviors. |
| 7875 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 7876 | FailingHttpTransactionFactory http_transaction_factory( |
| 7877 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7878 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7879 | context.set_http_transaction_factory(&http_transaction_factory); |
| 7880 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7881 | context.Init(); |
| 7882 | |
| 7883 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7884 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7885 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7886 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 7887 | // try to create an HttpNetworkTransaction synchronously on start). |
| 7888 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7889 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7890 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7891 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7892 | // Run pending error task, if there is one. |
| 7893 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7894 | |
| 7895 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7896 | EXPECT_EQ(1, d.response_started_count()); |
| 7897 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 7898 | |
| 7899 | // NetworkDelegate should see the cancellation, but not the error. |
| 7900 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 7901 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7902 | } |
| 7903 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7904 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7905 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7906 | |
| 7907 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7908 | GURL test_url(http_test_server()->GetURL("/")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7909 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7910 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7911 | |
| 7912 | req->Start(); |
| 7913 | base::RunLoop().Run(); |
| 7914 | |
| 7915 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7916 | } |
| 7917 | |
| 7918 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7919 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7920 | |
| 7921 | // Populate the cache. |
| 7922 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7923 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7924 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7925 | req->Start(); |
| 7926 | base::RunLoop().Run(); |
| 7927 | |
| 7928 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7929 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7930 | EXPECT_FALSE(req->response_info().was_cached); |
| 7931 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7932 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7933 | DEFAULT_PRIORITY, &d); |
| 7934 | req->Start(); |
| 7935 | base::RunLoop().Run(); |
| 7936 | |
| 7937 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7938 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7939 | EXPECT_TRUE(req->response_info().was_cached); |
| 7940 | } |
| 7941 | |
| 7942 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7943 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7944 | |
| 7945 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7946 | GURL test_url(http_test_server()->GetURL("/")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7947 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7948 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7949 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE); |
| 7950 | |
| 7951 | req->Start(); |
| 7952 | base::RunLoop().Run(); |
| 7953 | |
| 7954 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7955 | } |
| 7956 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 7957 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 7958 | ASSERT_TRUE(http_test_server()->Start()); |
| 7959 | |
| 7960 | TestDelegate d; |
| 7961 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 7962 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
| 7963 | req->Start(); |
| 7964 | base::RunLoop().Run(); |
| 7965 | |
| 7966 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 7967 | } |
| 7968 | |
| 7969 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 7970 | ASSERT_TRUE(http_test_server()->Start()); |
| 7971 | |
| 7972 | TestDelegate d; |
| 7973 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 7974 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d)); |
| 7975 | req->Start(); |
| 7976 | base::RunLoop().Run(); |
| 7977 | |
| 7978 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 7979 | } |
| 7980 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7981 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 7982 | public: |
| 7983 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 7984 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 7985 | // tests into the factory tests. |
| 7986 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 7987 | } |
| 7988 | |
| 7989 | void SetUpFactory() override { |
| 7990 | interceptor_ = new MockURLRequestInterceptor(); |
| 7991 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7992 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7993 | } |
| 7994 | |
| 7995 | MockURLRequestInterceptor* interceptor() const { |
| 7996 | return interceptor_; |
| 7997 | } |
| 7998 | |
| 7999 | private: |
| 8000 | MockURLRequestInterceptor* interceptor_; |
| 8001 | }; |
| 8002 | |
| 8003 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8004 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 8005 | interceptor()->set_intercept_redirect(true); |
| 8006 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 8007 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 8008 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8009 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8010 | |
| 8011 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8012 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8013 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8014 | req->Start(); |
| 8015 | base::RunLoop().Run(); |
| 8016 | |
| 8017 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 8018 | // Check we got one good response |
| 8019 | EXPECT_TRUE(req->status().is_success()); |
| 8020 | if (req->status().is_success()) |
| 8021 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8022 | |
| 8023 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8024 | EXPECT_EQ(1, d.response_started_count()); |
| 8025 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8026 | |
| 8027 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8028 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8029 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 8030 | } |
| 8031 | |
| 8032 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8033 | NetworkDelegateNotificationOnErrorIntercept) { |
| 8034 | // Intercept that error and respond with an OK response. |
| 8035 | interceptor()->set_intercept_final_response(true); |
| 8036 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 8037 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 8038 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 8039 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8040 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8041 | |
| 8042 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8043 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8044 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8045 | &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8046 | req->set_method("GET"); |
| 8047 | req->Start(); |
| 8048 | base::RunLoop().Run(); |
| 8049 | |
| 8050 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 8051 | |
| 8052 | // Check we received one good response. |
| 8053 | EXPECT_TRUE(req->status().is_success()); |
| 8054 | if (req->status().is_success()) |
| 8055 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8056 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8057 | EXPECT_EQ(1, d.response_started_count()); |
| 8058 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8059 | |
| 8060 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8061 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8062 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 8063 | } |
| 8064 | |
| 8065 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8066 | NetworkDelegateNotificationOnResponseIntercept) { |
| 8067 | // Intercept that error and respond with an OK response. |
| 8068 | interceptor()->set_intercept_final_response(true); |
| 8069 | |
| 8070 | // Intercept with a real URLRequestHttpJob. |
| 8071 | interceptor()->set_use_url_request_http_job(true); |
| 8072 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8073 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8074 | |
| 8075 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8076 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8077 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8078 | req->set_method("GET"); |
| 8079 | req->Start(); |
| 8080 | base::RunLoop().Run(); |
| 8081 | |
| 8082 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 8083 | |
| 8084 | // Check we received one good response. |
| 8085 | EXPECT_TRUE(req->status().is_success()); |
| 8086 | if (req->status().is_success()) |
| 8087 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8088 | EXPECT_EQ("hello", d.data_received()); |
| 8089 | EXPECT_EQ(1, d.response_started_count()); |
| 8090 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8091 | |
| 8092 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8093 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8094 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 8095 | } |
| 8096 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8097 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 8098 | public: |
| 8099 | URLRequestTestReferrerPolicy() {} |
| 8100 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8101 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 8102 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 8103 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8104 | origin_server_->AddDefaultHandlers( |
| 8105 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8106 | } else { |
| 8107 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8108 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8109 | ASSERT_TRUE(origin_server_->Start()); |
| 8110 | } |
| 8111 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8112 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 8113 | net::EmbeddedTestServer::Type dest_type) { |
| 8114 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 8115 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8116 | origin_server_->AddDefaultHandlers( |
| 8117 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8118 | } else { |
| 8119 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8120 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8121 | ASSERT_TRUE(origin_server_->Start()); |
| 8122 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8123 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 8124 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8125 | destination_server_->AddDefaultHandlers( |
| 8126 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8127 | } else { |
| 8128 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8129 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8130 | ASSERT_TRUE(destination_server_->Start()); |
| 8131 | } |
| 8132 | |
| 8133 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 8134 | const GURL& referrer, |
| 8135 | const GURL& expected) { |
| 8136 | // Create and execute the request: we'll only have a |destination_server_| |
| 8137 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 8138 | // |origin_server_| for both endpoints. |
| 8139 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8140 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 8141 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8142 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8143 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8144 | |
| 8145 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8146 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8147 | default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8148 | req->set_referrer_policy(policy); |
| 8149 | req->SetReferrer(referrer.spec()); |
| 8150 | req->Start(); |
| 8151 | base::RunLoop().Run(); |
| 8152 | |
| 8153 | EXPECT_EQ(1, d.response_started_count()); |
| 8154 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8155 | EXPECT_EQ(destination_url, req->url()); |
| 8156 | EXPECT_TRUE(req->status().is_success()); |
| 8157 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8158 | |
| 8159 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 8160 | if (expected.is_empty()) |
| 8161 | EXPECT_EQ("None", d.data_received()); |
| 8162 | else |
| 8163 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 8164 | } |
| 8165 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8166 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8167 | |
| 8168 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8169 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 8170 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8171 | }; |
| 8172 | |
| 8173 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8174 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8175 | |
| 8176 | VerifyReferrerAfterRedirect( |
| 8177 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8178 | origin_server()->GetURL("/path/to/file.html"), |
| 8179 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8180 | |
| 8181 | VerifyReferrerAfterRedirect( |
| 8182 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8183 | origin_server()->GetURL("/path/to/file.html"), |
| 8184 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8185 | |
| 8186 | VerifyReferrerAfterRedirect( |
| 8187 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8188 | origin_server()->GetURL("/path/to/file.html"), |
| 8189 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8190 | |
| 8191 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8192 | origin_server()->GetURL("/path/to/file.html"), |
| 8193 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8194 | } |
| 8195 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8196 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 8197 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 8198 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8199 | |
| 8200 | VerifyReferrerAfterRedirect( |
| 8201 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8202 | origin_server()->GetURL("/path/to/file.html"), |
| 8203 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8204 | |
| 8205 | VerifyReferrerAfterRedirect( |
| 8206 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8207 | origin_server()->GetURL("/path/to/file.html"), |
| 8208 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8209 | |
| 8210 | VerifyReferrerAfterRedirect( |
| 8211 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8212 | origin_server()->GetURL("/path/to/file.html"), |
| 8213 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8214 | |
| 8215 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8216 | origin_server()->GetURL("/path/to/file.html"), |
| 8217 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8218 | } |
| 8219 | |
| 8220 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8221 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8222 | |
| 8223 | VerifyReferrerAfterRedirect( |
| 8224 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8225 | origin_server()->GetURL("/path/to/file.html"), |
| 8226 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8227 | |
| 8228 | VerifyReferrerAfterRedirect( |
| 8229 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8230 | origin_server()->GetURL("/path/to/file.html"), |
| 8231 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8232 | |
| 8233 | VerifyReferrerAfterRedirect( |
| 8234 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8235 | origin_server()->GetURL("/path/to/file.html"), |
| 8236 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8237 | |
| 8238 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8239 | origin_server()->GetURL("/path/to/file.html"), |
| 8240 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8241 | } |
| 8242 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8243 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 8244 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 8245 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8246 | |
| 8247 | VerifyReferrerAfterRedirect( |
| 8248 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8249 | origin_server()->GetURL("/path/to/file.html"), |
| 8250 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8251 | |
| 8252 | VerifyReferrerAfterRedirect( |
| 8253 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8254 | origin_server()->GetURL("/path/to/file.html"), |
| 8255 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8256 | |
| 8257 | VerifyReferrerAfterRedirect( |
| 8258 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8259 | origin_server()->GetURL("/path/to/file.html"), |
| 8260 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8261 | |
| 8262 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8263 | origin_server()->GetURL("/path/to/file.html"), |
| 8264 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8265 | } |
| 8266 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8267 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 8268 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 8269 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8270 | |
| 8271 | VerifyReferrerAfterRedirect( |
| 8272 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8273 | origin_server()->GetURL("/path/to/file.html"), |
| 8274 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8275 | |
| 8276 | VerifyReferrerAfterRedirect( |
| 8277 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8278 | origin_server()->GetURL("/path/to/file.html"), |
| 8279 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8280 | |
| 8281 | VerifyReferrerAfterRedirect( |
| 8282 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8283 | origin_server()->GetURL("/path/to/file.html"), |
| 8284 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8285 | |
| 8286 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8287 | origin_server()->GetURL("/path/to/file.html"), |
| 8288 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8289 | } |
| 8290 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8291 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 8292 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 8293 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8294 | |
| 8295 | VerifyReferrerAfterRedirect( |
| 8296 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8297 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8298 | |
| 8299 | VerifyReferrerAfterRedirect( |
| 8300 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8301 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8302 | |
| 8303 | VerifyReferrerAfterRedirect( |
| 8304 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8305 | origin_server()->GetURL("/path/to/file.html"), |
| 8306 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8307 | |
| 8308 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8309 | origin_server()->GetURL("/path/to/file.html"), |
| 8310 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8311 | } |
| 8312 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 8313 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8314 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8315 | HTTPSRequestTest() : default_context_(true) { |
| 8316 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8317 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8318 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8319 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8320 | |
| 8321 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8322 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8323 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8324 | }; |
| 8325 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 8326 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8327 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8328 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8329 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8330 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8331 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8332 | TestDelegate d; |
| 8333 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8334 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8335 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8336 | r->Start(); |
| 8337 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8338 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8339 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8340 | |
| 8341 | EXPECT_EQ(1, d.response_started_count()); |
| 8342 | EXPECT_FALSE(d.received_data_before_response()); |
| 8343 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8344 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8345 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8346 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8347 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8348 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8349 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8350 | } |
| 8351 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 8352 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8353 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8354 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8355 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8356 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8357 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8358 | |
| 8359 | bool err_allowed = true; |
| 8360 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 8361 | TestDelegate d; |
| 8362 | { |
| 8363 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8364 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8365 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8366 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8367 | r->Start(); |
| 8368 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8369 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8370 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8371 | |
| 8372 | EXPECT_EQ(1, d.response_started_count()); |
| 8373 | EXPECT_FALSE(d.received_data_before_response()); |
| 8374 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8375 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8376 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8377 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8378 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8379 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8380 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8381 | } |
| 8382 | } |
| 8383 | } |
| 8384 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 8385 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8386 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8387 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 8388 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8389 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8390 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8391 | |
| 8392 | // Iterate from false to true, just so that we do the opposite of the |
| 8393 | // previous test in order to increase test coverage. |
| 8394 | bool err_allowed = false; |
| 8395 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 8396 | TestDelegate d; |
| 8397 | { |
| 8398 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8399 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8400 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8401 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8402 | r->Start(); |
| 8403 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8404 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8405 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8406 | |
| 8407 | EXPECT_EQ(1, d.response_started_count()); |
| 8408 | EXPECT_FALSE(d.received_data_before_response()); |
| 8409 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8410 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8411 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8412 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8413 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8414 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8415 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8416 | } |
| 8417 | } |
| 8418 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 8419 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8420 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 8421 | // security state. (see http://crbug.com/550977). |
| 8422 | #if !defined(OS_IOS) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8423 | // This tests that a load of www.google.com with a certificate error sets |
| 8424 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 8425 | // the interstitial to be fatal. |
| 8426 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8427 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8428 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8429 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8430 | ASSERT_TRUE(test_server.Start()); |
| 8431 | |
| 8432 | // We require that the URL be www.google.com in order to pick up the |
| 8433 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 8434 | // have to use a MockHostResolver in order to direct www.google.com to the |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8435 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8436 | |
| 8437 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8438 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8439 | TestURLRequestContext context(true); |
| 8440 | context.set_network_delegate(&network_delegate); |
| 8441 | context.set_host_resolver(&host_resolver); |
| 8442 | TransportSecurityState transport_security_state; |
| 8443 | context.set_transport_security_state(&transport_security_state); |
| 8444 | context.Init(); |
| 8445 | |
| 8446 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8447 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8448 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8449 | test_server.host_port_pair().port())), |
| 8450 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8451 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8452 | r->Start(); |
| 8453 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8454 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8455 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8456 | |
| 8457 | EXPECT_EQ(1, d.response_started_count()); |
| 8458 | EXPECT_FALSE(d.received_data_before_response()); |
| 8459 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8460 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8461 | } |
| 8462 | |
| 8463 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 8464 | // TransportSecurityState. |
| 8465 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 8466 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 8467 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8468 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8469 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8470 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8471 | ASSERT_TRUE(test_server.Start()); |
| 8472 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8473 | // We require that the URL be www.google.com in order to pick up the static |
| 8474 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 8475 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 8476 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8477 | |
| 8478 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8479 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8480 | TestURLRequestContext context(true); |
| 8481 | context.set_network_delegate(&network_delegate); |
| 8482 | context.set_host_resolver(&host_resolver); |
| 8483 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8484 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8485 | TransportSecurityState::STSState static_sts_state; |
| 8486 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8487 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8488 | "www.google.com", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8489 | context.set_transport_security_state(&transport_security_state); |
| 8490 | context.Init(); |
| 8491 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8492 | TransportSecurityState::STSState dynamic_sts_state; |
| 8493 | TransportSecurityState::PKPState dynamic_pkp_state; |
| 8494 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState("www.google.com", |
| 8495 | &dynamic_sts_state)); |
| 8496 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState("www.google.com", |
| 8497 | &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8498 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8499 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8500 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8501 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8502 | test_server.host_port_pair().port())), |
| 8503 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8504 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8505 | r->Start(); |
| 8506 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8507 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8508 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8509 | |
| 8510 | EXPECT_EQ(1, d.response_started_count()); |
| 8511 | EXPECT_FALSE(d.received_data_before_response()); |
| 8512 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8513 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8514 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8515 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8516 | TransportSecurityState::STSState new_static_sts_state; |
| 8517 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8518 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8519 | "www.google.com", &new_static_sts_state, &new_static_pkp_state)); |
| 8520 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 8521 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 8522 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 8523 | "www.google.com", &new_dynamic_sts_state)); |
| 8524 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 8525 | "www.google.com", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8526 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8527 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 8528 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 8529 | static_sts_state.include_subdomains); |
| 8530 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 8531 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 8532 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 8533 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 8534 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8535 | } |
| 8536 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8537 | // Make sure HSTS preserves a POST request's method and body. |
| 8538 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 8539 | static const char kData[] = "hello world"; |
| 8540 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8541 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8542 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8543 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8544 | ASSERT_TRUE(test_server.Start()); |
| 8545 | |
| 8546 | |
| 8547 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8548 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8549 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8550 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8551 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8552 | |
| 8553 | // Force https for www.somewhere.com. |
| 8554 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 8555 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 8556 | bool include_subdomains = false; |
| 8557 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 8558 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8559 | |
| 8560 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8561 | |
| 8562 | TestURLRequestContext context(true); |
| 8563 | context.set_host_resolver(&host_resolver); |
| 8564 | context.set_transport_security_state(&transport_security_state); |
| 8565 | context.set_network_delegate(&network_delegate); |
| 8566 | context.Init(); |
| 8567 | |
| 8568 | TestDelegate d; |
| 8569 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 8570 | // cause a certificate error. Ignore the error. |
| 8571 | d.set_allow_certificate_errors(true); |
| 8572 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8573 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8574 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8575 | test_server.host_port_pair().port())), |
| 8576 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8577 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8578 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8579 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8580 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8581 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8582 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8583 | EXPECT_EQ("https", req->url().scheme()); |
| 8584 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8585 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 8586 | |
| 8587 | LoadTimingInfo load_timing_info; |
| 8588 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 8589 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 8590 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8591 | } |
| 8592 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8593 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 8594 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 8595 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 8596 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8597 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8598 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8599 | ASSERT_TRUE(test_server.Start()); |
| 8600 | |
| 8601 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8602 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8603 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 8604 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8605 | MockHostResolver host_resolver; |
| 8606 | |
| 8607 | TransportSecurityState transport_security_state; |
| 8608 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 8609 | bool include_subdomains = false; |
| 8610 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 8611 | |
| 8612 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8613 | |
| 8614 | MockCertVerifier cert_verifier; |
| 8615 | cert_verifier.set_default_result(OK); |
| 8616 | |
| 8617 | TestURLRequestContext context(true); |
| 8618 | context.set_host_resolver(&host_resolver); |
| 8619 | context.set_transport_security_state(&transport_security_state); |
| 8620 | context.set_network_delegate(&network_delegate); |
| 8621 | context.set_cert_verifier(&cert_verifier); |
| 8622 | context.Init(); |
| 8623 | |
| 8624 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 8625 | test_server.host_port_pair().port())); |
| 8626 | url::Replacements<char> replacements; |
| 8627 | const char kNewScheme[] = "https"; |
| 8628 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 8629 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 8630 | |
| 8631 | TestDelegate d; |
| 8632 | // Quit on redirect to allow response header inspection upon redirect. |
| 8633 | d.set_quit_on_redirect(true); |
| 8634 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8635 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8636 | context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8637 | // Set Origin header to simulate a cross-origin request. |
| 8638 | HttpRequestHeaders request_headers; |
| 8639 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 8640 | req->SetExtraRequestHeaders(request_headers); |
| 8641 | |
| 8642 | req->Start(); |
| 8643 | base::RunLoop().Run(); |
| 8644 | |
| 8645 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8646 | |
| 8647 | const HttpResponseHeaders* headers = req->response_headers(); |
| 8648 | std::string redirect_location; |
| 8649 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 8650 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 8651 | |
| 8652 | std::string received_cors_header; |
| 8653 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 8654 | &received_cors_header)); |
| 8655 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 8656 | } |
| 8657 | |
davidben | b4c25b6 | 2016-06-24 02:39:27 | [diff] [blame] | 8658 | // Test that DHE-only servers fail with the expected dedicated error code. |
| 8659 | TEST_F(HTTPSRequestTest, DHE) { |
| 8660 | SpawnedTestServer::SSLOptions ssl_options; |
| 8661 | ssl_options.key_exchanges = |
| 8662 | SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; |
| 8663 | SpawnedTestServer test_server( |
| 8664 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 8665 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8666 | ASSERT_TRUE(test_server.Start()); |
| 8667 | |
| 8668 | TestDelegate d; |
| 8669 | { |
| 8670 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8671 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
| 8672 | |
| 8673 | r->Start(); |
| 8674 | EXPECT_TRUE(r->is_pending()); |
| 8675 | |
| 8676 | base::RunLoop().Run(); |
| 8677 | |
| 8678 | EXPECT_EQ(1, d.response_started_count()); |
| 8679 | EXPECT_FALSE(r->status().is_success()); |
| 8680 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 8681 | EXPECT_EQ(ERR_SSL_OBSOLETE_CIPHER, r->status().error()); |
| 8682 | } |
| 8683 | } |
| 8684 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8685 | namespace { |
| 8686 | |
| 8687 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 8688 | public: |
| 8689 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 8690 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8691 | void OnCertificateRequested(URLRequest* request, |
| 8692 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8693 | on_certificate_requested_count_++; |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 8694 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8695 | } |
| 8696 | int on_certificate_requested_count() { |
| 8697 | return on_certificate_requested_count_; |
| 8698 | } |
| 8699 | private: |
| 8700 | int on_certificate_requested_count_; |
| 8701 | }; |
| 8702 | |
| 8703 | } // namespace |
| 8704 | |
| 8705 | // TODO(davidben): Test the rest of the code. Specifically, |
| 8706 | // - Filtering which certificates to select. |
| 8707 | // - Sending a certificate back. |
| 8708 | // - Getting a certificate request in an SSL renegotiation sending the |
| 8709 | // HTTP request. |
| 8710 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8711 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8712 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 8713 | ssl_config.client_cert_type = |
| 8714 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8715 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 8716 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8717 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8718 | ASSERT_TRUE(test_server.Start()); |
| 8719 | |
| 8720 | SSLClientAuthTestDelegate d; |
| 8721 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8722 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8723 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8724 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8725 | r->Start(); |
| 8726 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8727 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8728 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8729 | |
| 8730 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 8731 | EXPECT_FALSE(d.received_data_before_response()); |
| 8732 | EXPECT_EQ(0, d.bytes_received()); |
| 8733 | |
| 8734 | // Send no certificate. |
| 8735 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 8736 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 8737 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8738 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8739 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8740 | |
| 8741 | EXPECT_EQ(1, d.response_started_count()); |
| 8742 | EXPECT_FALSE(d.received_data_before_response()); |
| 8743 | EXPECT_NE(0, d.bytes_received()); |
| 8744 | } |
| 8745 | } |
| 8746 | |
| 8747 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 8748 | // Test that we attempt a session resume when making two connections to the |
| 8749 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8750 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8751 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8752 | SpawnedTestServer test_server( |
| 8753 | SpawnedTestServer::TYPE_HTTPS, |
| 8754 | ssl_options, |
| 8755 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8756 | ASSERT_TRUE(test_server.Start()); |
| 8757 | |
| 8758 | SSLClientSocket::ClearSessionCache(); |
| 8759 | |
| 8760 | { |
| 8761 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8762 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8763 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8765 | r->Start(); |
| 8766 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8767 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8768 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8769 | |
| 8770 | EXPECT_EQ(1, d.response_started_count()); |
| 8771 | } |
| 8772 | |
| 8773 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8774 | CloseAllConnections(); |
| 8775 | |
| 8776 | { |
| 8777 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8778 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8779 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8780 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8781 | r->Start(); |
| 8782 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8783 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8784 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8785 | |
| 8786 | // The response will look like; |
| 8787 | // insert abc |
| 8788 | // lookup abc |
| 8789 | // insert xyz |
| 8790 | // |
| 8791 | // With a newline at the end which makes the split think that there are |
| 8792 | // four lines. |
| 8793 | |
| 8794 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8795 | std::vector<std::string> lines = base::SplitString( |
| 8796 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8797 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 8798 | |
| 8799 | std::string session_id; |
| 8800 | |
| 8801 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8802 | std::vector<std::string> parts = base::SplitString( |
| 8803 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8804 | ASSERT_EQ(2u, parts.size()); |
| 8805 | if (i == 0) { |
| 8806 | EXPECT_EQ("insert", parts[0]); |
| 8807 | session_id = parts[1]; |
| 8808 | } else { |
| 8809 | EXPECT_EQ("lookup", parts[0]); |
| 8810 | EXPECT_EQ(session_id, parts[1]); |
| 8811 | } |
| 8812 | } |
| 8813 | } |
| 8814 | } |
| 8815 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8816 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 8817 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 8818 | // that exactly two different sessions were inserted, with no lookups etc. |
| 8819 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8820 | std::vector<std::string> lines = base::SplitString( |
| 8821 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8822 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 8823 | |
| 8824 | std::string session_id; |
| 8825 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8826 | std::vector<std::string> parts = base::SplitString( |
| 8827 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8828 | ASSERT_EQ(2u, parts.size()); |
| 8829 | EXPECT_EQ("insert", parts[0]); |
| 8830 | if (i == 0) { |
| 8831 | session_id = parts[1]; |
| 8832 | } else { |
| 8833 | EXPECT_NE(session_id, parts[1]); |
| 8834 | } |
| 8835 | } |
| 8836 | } |
| 8837 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8838 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 8839 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 8840 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8841 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8842 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8843 | SpawnedTestServer test_server( |
| 8844 | SpawnedTestServer::TYPE_HTTPS, |
| 8845 | ssl_options, |
| 8846 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8847 | ASSERT_TRUE(test_server.Start()); |
| 8848 | |
| 8849 | SSLClientSocket::ClearSessionCache(); |
| 8850 | |
| 8851 | { |
| 8852 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8853 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8854 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8855 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8856 | r->Start(); |
| 8857 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8858 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8859 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8860 | |
| 8861 | EXPECT_EQ(1, d.response_started_count()); |
| 8862 | } |
| 8863 | |
| 8864 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 8865 | HttpNetworkSession::Params params; |
| 8866 | params.host_resolver = default_context_.host_resolver(); |
| 8867 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 8868 | params.transport_security_state = default_context_.transport_security_state(); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 8869 | params.cert_transparency_verifier = |
| 8870 | default_context_.cert_transparency_verifier(); |
| 8871 | params.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8872 | params.proxy_service = default_context_.proxy_service(); |
| 8873 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 8874 | params.http_auth_handler_factory = |
| 8875 | default_context_.http_auth_handler_factory(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8876 | params.http_server_properties = default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8877 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8878 | HttpNetworkSession network_session(params); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8879 | std::unique_ptr<HttpCache> cache(new HttpCache( |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8880 | &network_session, HttpCache::DefaultBackend::InMemory(0), false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8881 | |
| 8882 | default_context_.set_http_transaction_factory(cache.get()); |
| 8883 | |
| 8884 | { |
| 8885 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8886 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8887 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8888 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8889 | r->Start(); |
| 8890 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8891 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8892 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8893 | |
| 8894 | // The response will look like; |
| 8895 | // insert abc |
| 8896 | // insert xyz |
| 8897 | // |
| 8898 | // With a newline at the end which makes the split think that there are |
| 8899 | // three lines. |
| 8900 | |
| 8901 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8902 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8903 | } |
| 8904 | } |
| 8905 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8906 | class FallbackTestURLRequestContext : public TestURLRequestContext { |
| 8907 | public: |
| 8908 | explicit FallbackTestURLRequestContext(bool delay_initialization) |
| 8909 | : TestURLRequestContext(delay_initialization) {} |
| 8910 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 8911 | void set_fallback_min_version(uint16_t version) { |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 8912 | TestSSLConfigService* ssl_config_service = new TestSSLConfigService( |
| 8913 | true /* check for EV */, false /* online revocation checking */, |
| 8914 | false /* require rev. checking for local |
| 8915 | anchors */, |
| 8916 | false /* token binding enabled */); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8917 | ssl_config_service->set_fallback_min_version(version); |
| 8918 | set_ssl_config_service(ssl_config_service); |
| 8919 | } |
| 8920 | }; |
| 8921 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8922 | class HTTPSFallbackTest : public testing::Test { |
| 8923 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8924 | HTTPSFallbackTest() : context_(true) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8925 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8926 | |
| 8927 | protected: |
| 8928 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 8929 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8930 | context_.Init(); |
| 8931 | delegate_.set_allow_certificate_errors(true); |
| 8932 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8933 | SpawnedTestServer test_server( |
| 8934 | SpawnedTestServer::TYPE_HTTPS, |
| 8935 | ssl_options, |
| 8936 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8937 | ASSERT_TRUE(test_server.Start()); |
| 8938 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8939 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 8940 | &delegate_); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8941 | request_->Start(); |
| 8942 | |
| 8943 | base::RunLoop().Run(); |
| 8944 | } |
| 8945 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 8946 | void set_fallback_min_version(uint16_t version) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8947 | context_.set_fallback_min_version(version); |
| 8948 | } |
| 8949 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8950 | void ExpectConnection(int version) { |
| 8951 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8952 | EXPECT_NE(0, delegate_.bytes_received()); |
| 8953 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 8954 | request_->ssl_info().connection_status)); |
| 8955 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 8956 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8957 | } |
| 8958 | |
| 8959 | void ExpectFailure(int error) { |
| 8960 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8961 | EXPECT_FALSE(request_->status().is_success()); |
| 8962 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 8963 | EXPECT_EQ(error, request_->status().error()); |
| 8964 | } |
| 8965 | |
| 8966 | private: |
| 8967 | TestDelegate delegate_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8968 | FallbackTestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8969 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8970 | }; |
| 8971 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8972 | // Tests the TLS 1.0 fallback doesn't happen. |
| 8973 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8974 | SpawnedTestServer::SSLOptions ssl_options( |
| 8975 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8976 | ssl_options.tls_intolerant = |
| 8977 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 8978 | |
| 8979 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 8980 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8981 | } |
| 8982 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 8983 | // Tests the TLS 1.1 fallback doesn't happen but 1.2-intolerance is detected. |
| 8984 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8985 | SpawnedTestServer::SSLOptions ssl_options( |
| 8986 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8987 | ssl_options.tls_intolerant = |
| 8988 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 8989 | |
| 8990 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 8991 | ExpectFailure(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION); |
| 8992 | } |
| 8993 | |
| 8994 | // Tests the TLS 1.1 fallback when explicitly enabled. |
| 8995 | TEST_F(HTTPSFallbackTest, TLSv1_1Fallback) { |
| 8996 | SpawnedTestServer::SSLOptions ssl_options( |
| 8997 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8998 | ssl_options.tls_intolerant = |
| 8999 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 9000 | |
| 9001 | set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
| 9002 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9003 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
| 9004 | } |
| 9005 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9006 | // Tests that the TLS 1.1 fallback, if enabled, triggers on closed connections. |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9007 | TEST_F(HTTPSFallbackTest, TLSv1_1FallbackClosed) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9008 | SpawnedTestServer::SSLOptions ssl_options( |
| 9009 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 9010 | ssl_options.tls_intolerant = |
| 9011 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 9012 | ssl_options.tls_intolerance_type = |
| 9013 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 9014 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9015 | set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9016 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 9017 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9018 | } |
| 9019 | |
| 9020 | // This test is disabled on Android because the remote test server doesn't cause |
| 9021 | // a TCP reset. |
| 9022 | #if !defined(OS_ANDROID) |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9023 | // Tests fallback to TLS 1.1, if enabled, on connection reset. |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9024 | TEST_F(HTTPSFallbackTest, TLSv1_1FallbackReset) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9025 | SpawnedTestServer::SSLOptions ssl_options( |
| 9026 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 9027 | ssl_options.tls_intolerant = |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9028 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9029 | ssl_options.tls_intolerance_type = |
| 9030 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 9031 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9032 | set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9033 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 9034 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9035 | } |
| 9036 | #endif // !OS_ANDROID |
| 9037 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9038 | // Tests that we don't fallback, even if enabled, on handshake failure with |
| 9039 | // servers that implement TLS_FALLBACK_SCSV. Also ensure that the original error |
| 9040 | // code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9041 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9042 | SpawnedTestServer::SSLOptions ssl_options( |
| 9043 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 9044 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9045 | // a version fallback. |
| 9046 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 9047 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9048 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 9049 | // connections are rejected. |
| 9050 | ssl_options.fallback_scsv_enabled = true; |
| 9051 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9052 | set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 9053 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 9054 | |
| 9055 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 9056 | // intolerance. If the fallback SCSV is processed when the original error |
| 9057 | // that caused the fallback should be returned, which should be |
| 9058 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 9059 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 9060 | } |
| 9061 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9062 | // Tests that we don't fallback, even if enabled, on connection closed with |
| 9063 | // servers that implement TLS_FALLBACK_SCSV. Also ensure that the original error |
| 9064 | // code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 9065 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 9066 | SpawnedTestServer::SSLOptions ssl_options( |
| 9067 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 9068 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 9069 | // a version fallback. |
| 9070 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 9071 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 9072 | ssl_options.tls_intolerance_type = |
| 9073 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 9074 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 9075 | // connections are rejected. |
| 9076 | ssl_options.fallback_scsv_enabled = true; |
| 9077 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9078 | set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 9079 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 9080 | |
| 9081 | // The original error should be replayed on rejected fallback. |
| 9082 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 9083 | } |
| 9084 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 9085 | // Test that fallback probe connections don't cause sessions to be cached. |
| 9086 | TEST_F(HTTPSRequestTest, FallbackProbeNoCache) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9087 | SpawnedTestServer::SSLOptions ssl_options( |
| 9088 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 9089 | ssl_options.tls_intolerant = |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9090 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9091 | ssl_options.tls_intolerance_type = |
| 9092 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 9093 | ssl_options.record_resume = true; |
| 9094 | |
| 9095 | SpawnedTestServer test_server( |
| 9096 | SpawnedTestServer::TYPE_HTTPS, |
| 9097 | ssl_options, |
| 9098 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9099 | ASSERT_TRUE(test_server.Start()); |
| 9100 | |
| 9101 | SSLClientSocket::ClearSessionCache(); |
| 9102 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9103 | // Make a connection that does a probe fallback to TLSv1.1 but fails because |
| 9104 | // fallback is disabled. We don't wish a session for this connection to be |
| 9105 | // inserted locally. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9106 | { |
| 9107 | TestDelegate delegate; |
| 9108 | FallbackTestURLRequestContext context(true); |
| 9109 | |
| 9110 | context.Init(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9111 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9112 | test_server.GetURL("/"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9113 | request->Start(); |
| 9114 | |
| 9115 | base::RunLoop().Run(); |
| 9116 | |
| 9117 | EXPECT_EQ(1, delegate.response_started_count()); |
| 9118 | EXPECT_FALSE(request->status().is_success()); |
| 9119 | EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 9120 | EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION, |
| 9121 | request->status().error()); |
| 9122 | } |
| 9123 | |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9124 | // Now allow TLSv1.1 fallback connections and request the session cache log. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9125 | { |
| 9126 | TestDelegate delegate; |
| 9127 | FallbackTestURLRequestContext context(true); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9128 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_1); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9129 | |
| 9130 | context.Init(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9131 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9132 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9133 | request->Start(); |
| 9134 | |
| 9135 | base::RunLoop().Run(); |
| 9136 | |
| 9137 | EXPECT_EQ(1, delegate.response_started_count()); |
| 9138 | EXPECT_NE(0, delegate.bytes_received()); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 9139 | EXPECT_EQ( |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 9140 | SSL_CONNECTION_VERSION_TLS1_1, |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 9141 | SSLConnectionStatusToVersion(request->ssl_info().connection_status)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9142 | EXPECT_TRUE(request->ssl_info().connection_status & |
| 9143 | SSL_CONNECTION_VERSION_FALLBACK); |
| 9144 | |
| 9145 | std::vector<std::string> lines; |
| 9146 | // If no sessions were cached then the server should have seen two sessions |
| 9147 | // inserted with no lookups. |
| 9148 | AssertTwoDistinctSessionsInserted(delegate.data_received()); |
| 9149 | } |
| 9150 | } |
| 9151 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9152 | class HTTPSSessionTest : public testing::Test { |
| 9153 | public: |
| 9154 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9155 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9156 | |
| 9157 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9158 | default_context_.set_cert_verifier(&cert_verifier_); |
| 9159 | default_context_.Init(); |
| 9160 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9161 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9162 | |
| 9163 | protected: |
| 9164 | MockCertVerifier cert_verifier_; |
| 9165 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 9166 | TestURLRequestContext default_context_; |
| 9167 | }; |
| 9168 | |
| 9169 | // Tests that session resumption is not attempted if an invalid certificate |
| 9170 | // is presented. |
| 9171 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 9172 | SpawnedTestServer::SSLOptions ssl_options; |
| 9173 | ssl_options.record_resume = true; |
| 9174 | SpawnedTestServer test_server( |
| 9175 | SpawnedTestServer::TYPE_HTTPS, |
| 9176 | ssl_options, |
| 9177 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9178 | ASSERT_TRUE(test_server.Start()); |
| 9179 | |
| 9180 | SSLClientSocket::ClearSessionCache(); |
| 9181 | |
| 9182 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9183 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9184 | { |
| 9185 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9186 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9187 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9188 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9189 | r->Start(); |
| 9190 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9191 | |
| 9192 | base::RunLoop().Run(); |
| 9193 | |
| 9194 | EXPECT_EQ(1, d.response_started_count()); |
| 9195 | } |
| 9196 | |
| 9197 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 9198 | CloseAllConnections(); |
| 9199 | |
| 9200 | // Now change the certificate to be acceptable (so that the response is |
| 9201 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9202 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9203 | { |
| 9204 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9205 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9206 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9207 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9208 | r->Start(); |
| 9209 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9210 | |
| 9211 | base::RunLoop().Run(); |
| 9212 | |
| 9213 | // The response will look like; |
| 9214 | // insert abc |
| 9215 | // insert xyz |
| 9216 | // |
| 9217 | // With a newline at the end which makes the split think that there are |
| 9218 | // three lines. |
| 9219 | // |
| 9220 | // If a session was presented (eg: a bug), then the response would look |
| 9221 | // like; |
| 9222 | // insert abc |
| 9223 | // lookup abc |
| 9224 | // insert xyz |
| 9225 | |
| 9226 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9227 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 9228 | } |
| 9229 | } |
| 9230 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9231 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 9232 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 9233 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9234 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 9235 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 9236 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9237 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 9238 | // testserver. |
| 9239 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 9240 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 9241 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 9242 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 9243 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 9244 | } }; |
| 9245 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9246 | // This is the policy OID contained in the certificates that testserver |
| 9247 | // generates. |
| 9248 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 9249 | |
| 9250 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 9251 | public: |
| 9252 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9253 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9254 | ev_test_policy_( |
| 9255 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 9256 | kOCSPTestCertFingerprint, |
| 9257 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9258 | } |
| 9259 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9260 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9261 | context_.SetCTPolicyEnforcer( |
| 9262 | base::MakeUnique<AllowAnyCertCTPolicyEnforcer>()); |
| 9263 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9264 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9265 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9266 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9267 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 9268 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9269 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9270 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 9271 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9272 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9273 | EnsureNSSHttpIOInit(); |
| 9274 | #endif |
| 9275 | } |
| 9276 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9277 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9278 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9279 | // We always overwrite out_cert_status. |
| 9280 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9281 | SpawnedTestServer test_server( |
| 9282 | SpawnedTestServer::TYPE_HTTPS, |
| 9283 | ssl_options, |
| 9284 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9285 | ASSERT_TRUE(test_server.Start()); |
| 9286 | |
| 9287 | TestDelegate d; |
| 9288 | d.set_allow_certificate_errors(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9289 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9290 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9291 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9292 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9293 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9294 | |
| 9295 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9296 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9297 | } |
| 9298 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9299 | ~HTTPSOCSPTest() override { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 9300 | #if defined(USE_NSS_CERTS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9301 | ShutdownNSSHttpIO(); |
| 9302 | #endif |
| 9303 | } |
| 9304 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9305 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9306 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 9307 | public: |
| 9308 | AllowAnyCertCTPolicyEnforcer() = default; |
| 9309 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 9310 | |
| 9311 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 9312 | X509Certificate* cert, |
| 9313 | const SCTList& verified_scts, |
| 9314 | const BoundNetLog& net_log) override { |
| 9315 | return ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS; |
| 9316 | } |
| 9317 | |
| 9318 | ct::EVPolicyCompliance DoesConformToCTEVPolicy( |
| 9319 | X509Certificate* cert, |
| 9320 | const ct::EVCertsWhitelist* ev_whitelist, |
| 9321 | const SCTList& verified_scts, |
| 9322 | const BoundNetLog& net_log) override { |
| 9323 | return ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS; |
| 9324 | } |
| 9325 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9326 | // SetupContext configures the URLRequestContext that will be used for making |
| 9327 | // connetions to testserver. This can be overridden in test subclasses for |
| 9328 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9329 | virtual void SetupContext() { |
| 9330 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9331 | true /* check for EV */, true /* online revocation checking */, |
| 9332 | false /* require rev. checking for local |
| 9333 | anchors */, |
| 9334 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9335 | } |
| 9336 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9337 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9338 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9339 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9340 | }; |
| 9341 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9342 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 9343 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9344 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 9345 | // have that ability on other platforms. |
| 9346 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 9347 | #else |
| 9348 | return 0; |
| 9349 | #endif |
| 9350 | } |
| 9351 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9352 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 9353 | // operating system supports revocation checking and can distinguish between |
| 9354 | // situations where a given certificate lacks any revocation information (eg: |
| 9355 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 9356 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 9357 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 9358 | // skipped. |
| 9359 | static bool SystemSupportsHardFailRevocationChecking() { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 9360 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9361 | return true; |
| 9362 | #else |
| 9363 | return false; |
| 9364 | #endif |
| 9365 | } |
| 9366 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9367 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 9368 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 9369 | // several tests are effected because our testing EV certificate won't be |
| 9370 | // recognised as EV. |
| 9371 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9372 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 9373 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 9374 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9375 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 9376 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 9377 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 9378 | return false; |
| 9379 | #else |
| 9380 | return true; |
| 9381 | #endif |
| 9382 | } |
| 9383 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9384 | static bool SystemSupportsOCSP() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 9385 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9386 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 9387 | return false; |
| 9388 | #else |
| 9389 | return true; |
| 9390 | #endif |
| 9391 | } |
| 9392 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9393 | static bool SystemSupportsOCSPStapling() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 9394 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9395 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9396 | #else |
| 9397 | return false; |
| 9398 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9399 | } |
| 9400 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9401 | TEST_F(HTTPSOCSPTest, Valid) { |
| 9402 | if (!SystemSupportsOCSP()) { |
| 9403 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9404 | return; |
| 9405 | } |
| 9406 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9407 | SpawnedTestServer::SSLOptions ssl_options( |
| 9408 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9409 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9410 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9411 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9412 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9413 | |
| 9414 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9415 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9416 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9417 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9418 | |
| 9419 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9420 | } |
| 9421 | |
| 9422 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 9423 | if (!SystemSupportsOCSP()) { |
| 9424 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9425 | return; |
| 9426 | } |
| 9427 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9428 | SpawnedTestServer::SSLOptions ssl_options( |
| 9429 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9430 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9431 | |
| 9432 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9433 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9434 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 9435 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9436 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 9437 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9438 | #endif |
| 9439 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9440 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9441 | } |
| 9442 | |
| 9443 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 9444 | if (!SystemSupportsOCSP()) { |
| 9445 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9446 | return; |
| 9447 | } |
| 9448 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9449 | SpawnedTestServer::SSLOptions ssl_options( |
| 9450 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9451 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9452 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9453 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9454 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9455 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9456 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9457 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9458 | |
| 9459 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9460 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9461 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9462 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9463 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9464 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9465 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9466 | LOG(WARNING) |
| 9467 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9468 | return; |
| 9469 | } |
| 9470 | |
| 9471 | SpawnedTestServer::SSLOptions ssl_options( |
| 9472 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9473 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9474 | ssl_options.staple_ocsp_response = true; |
| 9475 | ssl_options.ocsp_server_unavailable = true; |
| 9476 | |
| 9477 | CertStatus cert_status; |
| 9478 | DoConnection(ssl_options, &cert_status); |
| 9479 | |
| 9480 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9481 | |
| 9482 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9483 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9484 | |
| 9485 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9486 | } |
| 9487 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9488 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 9489 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9490 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 9491 | #else |
| 9492 | #define MAYBE_RevokedStapled RevokedStapled |
| 9493 | #endif |
| 9494 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9495 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9496 | LOG(WARNING) |
| 9497 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9498 | return; |
| 9499 | } |
| 9500 | |
| 9501 | SpawnedTestServer::SSLOptions ssl_options( |
| 9502 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9503 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9504 | ssl_options.staple_ocsp_response = true; |
| 9505 | ssl_options.ocsp_server_unavailable = true; |
| 9506 | |
| 9507 | CertStatus cert_status; |
| 9508 | DoConnection(ssl_options, &cert_status); |
| 9509 | |
| 9510 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9511 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9512 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9513 | } |
| 9514 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9515 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 9516 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9517 | void SetupContext() override { |
| 9518 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9519 | false /* check for EV */, false /* online revocation checking */, |
| 9520 | true /* require rev. checking for local |
| 9521 | anchors */, |
| 9522 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9523 | } |
| 9524 | }; |
| 9525 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9526 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 9527 | if (!SystemSupportsOCSP()) { |
| 9528 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9529 | return; |
| 9530 | } |
| 9531 | |
| 9532 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 9533 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 9534 | << "revocation checking"; |
| 9535 | return; |
| 9536 | } |
| 9537 | |
| 9538 | SpawnedTestServer::SSLOptions ssl_options( |
| 9539 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9540 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 9541 | |
| 9542 | CertStatus cert_status; |
| 9543 | DoConnection(ssl_options, &cert_status); |
| 9544 | |
| 9545 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 9546 | cert_status & CERT_STATUS_REVOKED); |
| 9547 | |
| 9548 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9549 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9550 | } |
| 9551 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9552 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 9553 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9554 | void SetupContext() override { |
| 9555 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9556 | true /* check for EV */, false /* online revocation checking */, |
| 9557 | false /* require rev. checking for local |
| 9558 | anchors */, |
| 9559 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9560 | } |
| 9561 | }; |
| 9562 | |
| 9563 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 9564 | if (!SystemSupportsOCSP()) { |
| 9565 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9566 | return; |
| 9567 | } |
| 9568 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9569 | SpawnedTestServer::SSLOptions ssl_options( |
| 9570 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9571 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9572 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9573 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9574 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9575 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9576 | |
| 9577 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9578 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9579 | |
| 9580 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9581 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9582 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9583 | } |
| 9584 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 9585 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 9586 | if (!SystemSupportsOCSP()) { |
| 9587 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9588 | return; |
| 9589 | } |
| 9590 | |
| 9591 | SpawnedTestServer::SSLOptions ssl_options( |
| 9592 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9593 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9594 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9595 | |
| 9596 | CertStatus cert_status; |
| 9597 | DoConnection(ssl_options, &cert_status); |
| 9598 | |
| 9599 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 9600 | // possible to determine whether the check failed because of actual |
| 9601 | // revocation or because there was an OCSP failure. |
| 9602 | #if defined(OS_WIN) |
| 9603 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9604 | #else |
| 9605 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9606 | #endif |
| 9607 | |
| 9608 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9609 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9610 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9611 | } |
| 9612 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9613 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 9614 | if (!SystemSupportsOCSP()) { |
| 9615 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9616 | return; |
| 9617 | } |
| 9618 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9619 | SpawnedTestServer::SSLOptions ssl_options( |
| 9620 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9621 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9622 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9623 | |
| 9624 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9625 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9626 | |
| 9627 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9628 | |
| 9629 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9630 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9631 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9632 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9633 | } |
| 9634 | |
| 9635 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 9636 | if (!SystemSupportsOCSP()) { |
| 9637 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9638 | return; |
| 9639 | } |
| 9640 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9641 | SpawnedTestServer::SSLOptions ssl_options( |
| 9642 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9643 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9644 | SSLConfigService::SetCRLSet( |
| 9645 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9646 | |
| 9647 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9648 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9649 | |
| 9650 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9651 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9652 | |
| 9653 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9654 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9655 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9656 | } |
| 9657 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9658 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 9659 | if (!SystemSupportsOCSP()) { |
| 9660 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9661 | return; |
| 9662 | } |
| 9663 | |
| 9664 | SpawnedTestServer::SSLOptions ssl_options( |
| 9665 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9666 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 9667 | SSLConfigService::SetCRLSet( |
| 9668 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9669 | false, &kOCSPTestCertSPKI, ""))); |
| 9670 | |
| 9671 | CertStatus cert_status; |
| 9672 | DoConnection(ssl_options, &cert_status); |
| 9673 | |
| 9674 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 9675 | // revocation check for EV. |
| 9676 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9677 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9678 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9679 | EXPECT_FALSE( |
| 9680 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9681 | } |
| 9682 | |
| 9683 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 9684 | if (!SystemSupportsOCSP()) { |
| 9685 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9686 | return; |
| 9687 | } |
| 9688 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9689 | SpawnedTestServer::SSLOptions ssl_options( |
| 9690 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9691 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9692 | SSLConfigService::SetCRLSet( |
| 9693 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 9694 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9695 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9696 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9697 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9698 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 9699 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 9700 | // test. |
| 9701 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9702 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9703 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9704 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9705 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9706 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9707 | } |
| 9708 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9709 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 9710 | // Test that when EV verification is requested, but online revocation |
| 9711 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 9712 | // no revocation checking actually happens. |
| 9713 | if (!SystemSupportsOCSP()) { |
| 9714 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9715 | return; |
| 9716 | } |
| 9717 | |
| 9718 | // Unmark the certificate's OID as EV, which should disable revocation |
| 9719 | // checking (as per the user preference) |
| 9720 | ev_test_policy_.reset(); |
| 9721 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9722 | SpawnedTestServer::SSLOptions ssl_options( |
| 9723 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9724 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9725 | SSLConfigService::SetCRLSet( |
| 9726 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9727 | |
| 9728 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9729 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9730 | |
| 9731 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9732 | |
| 9733 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9734 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9735 | } |
| 9736 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9737 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 9738 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9739 | void SetupContext() override { |
| 9740 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9741 | false /* check for EV */, false /* online revocation checking */, |
| 9742 | false /* require rev. checking for local |
| 9743 | anchors */, |
| 9744 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9745 | } |
| 9746 | }; |
| 9747 | |
| 9748 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9749 | SpawnedTestServer::SSLOptions ssl_options( |
| 9750 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9751 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9752 | SSLConfigService::SetCRLSet( |
| 9753 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9754 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9755 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9756 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9757 | |
| 9758 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 9759 | // we don't fall back to online revocation checks. |
| 9760 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9761 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9762 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9763 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9764 | |
| 9765 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 9766 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9767 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 9768 | return; |
| 9769 | #endif |
| 9770 | |
| 9771 | SpawnedTestServer::SSLOptions ssl_options( |
| 9772 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9773 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9774 | ssl_options.cert_serial = 10; |
| 9775 | SSLConfigService::SetCRLSet( |
| 9776 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9777 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 9778 | |
| 9779 | CertStatus cert_status = 0; |
| 9780 | DoConnection(ssl_options, &cert_status); |
| 9781 | |
| 9782 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 9783 | // reflected without online revocation checking. |
| 9784 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9785 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9786 | EXPECT_FALSE( |
| 9787 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9788 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9789 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9790 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9791 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 9792 | // These tests aren't passing on Android. Either the RemoteTestServer isn't |
| 9793 | // starting up successfully, or it can't access the test files. |
| 9794 | // TODO(mmenke): Fix this. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9795 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9796 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 9797 | URLRequestTestFTP() |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9798 | : ftp_transaction_factory_(&host_resolver_), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9799 | ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
| 9800 | SpawnedTestServer::kLocalhost, |
| 9801 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9802 | // Can't use |default_context_|'s HostResolver to set up the |
| 9803 | // FTPTransactionFactory because it hasn't been created yet. |
| 9804 | default_context_.set_host_resolver(&host_resolver_); |
| 9805 | } |
| 9806 | |
| 9807 | // URLRequestTest interface: |
| 9808 | void SetUpFactory() override { |
| 9809 | // Add FTP support to the default URLRequestContext. |
| 9810 | job_factory_impl_->SetProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 9811 | "ftp", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9812 | base::WrapUnique(new FtpProtocolHandler(&ftp_transaction_factory_))); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9813 | } |
| 9814 | |
| 9815 | std::string GetTestFileContents() { |
| 9816 | base::FilePath path; |
| 9817 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 9818 | path = path.Append(kTestFilePath); |
| 9819 | path = path.AppendASCII(kFtpTestFile); |
| 9820 | std::string contents; |
| 9821 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 9822 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9823 | } |
| 9824 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9825 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9826 | MockHostResolver host_resolver_; |
| 9827 | FtpNetworkLayer ftp_transaction_factory_; |
| 9828 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9829 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9830 | }; |
| 9831 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9832 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 9833 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9834 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9835 | |
| 9836 | TestDelegate d; |
| 9837 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9838 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9839 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9840 | r->Start(); |
| 9841 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9842 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9843 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9844 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9845 | EXPECT_FALSE(r->is_pending()); |
| 9846 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 9847 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9848 | } |
| 9849 | } |
| 9850 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9851 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9852 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9853 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9854 | TestDelegate d; |
| 9855 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9856 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9857 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9858 | r->Start(); |
| 9859 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9860 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9861 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9862 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9863 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9864 | EXPECT_EQ(1, d.response_started_count()); |
| 9865 | EXPECT_FALSE(d.received_data_before_response()); |
| 9866 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9867 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9868 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9869 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9870 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9871 | } |
| 9872 | } |
| 9873 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9874 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9875 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9876 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9877 | TestDelegate d; |
| 9878 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9879 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9880 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9881 | r->Start(); |
| 9882 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9883 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9884 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9885 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9886 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9887 | EXPECT_EQ(1, d.response_started_count()); |
| 9888 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9889 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9890 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9891 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9892 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9893 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9894 | } |
| 9895 | } |
| 9896 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9897 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9898 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9899 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9900 | TestDelegate d; |
| 9901 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9902 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9903 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9904 | "chrome"), |
| 9905 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9906 | r->Start(); |
| 9907 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9908 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9909 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9910 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9911 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9912 | EXPECT_EQ(1, d.response_started_count()); |
| 9913 | EXPECT_FALSE(d.received_data_before_response()); |
| 9914 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9915 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9916 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9917 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9918 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 9919 | |
| 9920 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9921 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 9922 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9923 | } |
| 9924 | } |
| 9925 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9926 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9927 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9928 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9929 | TestDelegate d; |
| 9930 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9931 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9932 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9933 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9934 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9935 | r->Start(); |
| 9936 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9937 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9938 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9940 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9941 | EXPECT_EQ(1, d.response_started_count()); |
| 9942 | EXPECT_FALSE(d.received_data_before_response()); |
| 9943 | EXPECT_EQ(d.bytes_received(), 0); |
| 9944 | } |
| 9945 | } |
| 9946 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9947 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9948 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9949 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9950 | TestDelegate d; |
| 9951 | // Set correct login credentials. The delegate will be asked for them when |
| 9952 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9953 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9954 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9955 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9956 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9957 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9958 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9959 | r->Start(); |
| 9960 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9961 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9962 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9963 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9964 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9965 | EXPECT_EQ(1, d.response_started_count()); |
| 9966 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9967 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9968 | } |
| 9969 | } |
| 9970 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9971 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9972 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9973 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9974 | TestDelegate d; |
| 9975 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9976 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9977 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9978 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9979 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9980 | r->Start(); |
| 9981 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9982 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9983 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9984 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9985 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9986 | EXPECT_EQ(1, d.response_started_count()); |
| 9987 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9988 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9989 | } |
| 9990 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9991 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9992 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9993 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9994 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9995 | TestDelegate d; |
| 9996 | // Set correct login credentials. The delegate will be asked for them when |
| 9997 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9998 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9999 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10000 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10001 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 10002 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10003 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10004 | r->Start(); |
| 10005 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10006 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10007 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10008 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10009 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10010 | EXPECT_EQ(1, d.response_started_count()); |
| 10011 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10012 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10013 | } |
| 10014 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10015 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10016 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10017 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10018 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10019 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10020 | { |
| 10021 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10022 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10023 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 10024 | "chrome"), |
| 10025 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10026 | r->Start(); |
| 10027 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10028 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10029 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10030 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10031 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10032 | EXPECT_EQ(1, d->response_started_count()); |
| 10033 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10034 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10035 | } |
| 10036 | |
| 10037 | d.reset(new TestDelegate); |
| 10038 | { |
| 10039 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10040 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10041 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10042 | r->Start(); |
| 10043 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10044 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10045 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10046 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10047 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10048 | EXPECT_EQ(1, d->response_started_count()); |
| 10049 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10050 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10051 | } |
| 10052 | } |
| 10053 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10054 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10055 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10056 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10057 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10058 | // Set correct login credentials. The delegate will be asked for them when |
| 10059 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 10060 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10061 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10062 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10063 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 10064 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10065 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10066 | r->Start(); |
| 10067 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10068 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10069 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10070 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10071 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10072 | EXPECT_EQ(1, d->response_started_count()); |
| 10073 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10074 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10075 | } |
| 10076 | |
| 10077 | // Use a new delegate without explicit credentials. The cached ones should be |
| 10078 | // used. |
| 10079 | d.reset(new TestDelegate); |
| 10080 | { |
| 10081 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 10082 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10083 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10084 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10085 | r->Start(); |
| 10086 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10087 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10088 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10089 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10090 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10091 | EXPECT_EQ(1, d->response_started_count()); |
| 10092 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10093 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10094 | } |
| 10095 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 10096 | |
| 10097 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 10098 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 10099 | |
| 10100 | TestDelegate d; |
| 10101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 10102 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d)); |
| 10103 | req->Start(); |
| 10104 | base::RunLoop().Run(); |
| 10105 | |
| 10106 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 10107 | } |
| 10108 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10109 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 10110 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10111 | TEST_F(URLRequestTest, NetworkAccessedClearBeforeNetworkStart) { |
| 10112 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10113 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10114 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 10115 | d.set_quit_on_network_start(true); |
| 10116 | |
| 10117 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10118 | |
| 10119 | req->Start(); |
| 10120 | base::RunLoop().Run(); |
| 10121 | |
| 10122 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 10123 | EXPECT_EQ(0, d.response_started_count()); |
| 10124 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10125 | |
| 10126 | req->ResumeNetworkStart(); |
| 10127 | base::RunLoop().Run(); |
| 10128 | } |
| 10129 | |
| 10130 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 10131 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10132 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10133 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
| 10134 | |
| 10135 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10136 | |
| 10137 | req->Start(); |
| 10138 | base::RunLoop().Run(); |
| 10139 | |
| 10140 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 10141 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10142 | } |
| 10143 | |
| 10144 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 10145 | MockHostResolver host_resolver; |
| 10146 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10147 | TestURLRequestContext context(true); |
| 10148 | context.set_network_delegate(&network_delegate); |
| 10149 | context.set_host_resolver(&host_resolver); |
| 10150 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 10151 | context.Init(); |
| 10152 | |
| 10153 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10154 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10155 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 10156 | |
| 10157 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10158 | |
| 10159 | req->Start(); |
| 10160 | base::RunLoop().Run(); |
| 10161 | EXPECT_TRUE(req->response_info().network_accessed); |
| 10162 | } |
| 10163 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10164 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10165 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10166 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10167 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10168 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10169 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d)); |
| 10170 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10171 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10172 | req.get(), &default_network_delegate_, |
| 10173 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10174 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 10175 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10176 | |
| 10177 | req->Start(); |
| 10178 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10179 | base::RunLoop().RunUntilIdle(); |
| 10180 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 10181 | EXPECT_EQ(0, d.received_redirect_count()); |
| 10182 | } |
| 10183 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 10184 | } // namespace net |