[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" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 96 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 97 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 98 | #include "net/test/test_data_directory.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 99 | #include "net/test/url_request/url_request_failed_job.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 100 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 101 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 102 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 103 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 104 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 105 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 106 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 107 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 108 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 109 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 110 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 111 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 113 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 114 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 115 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 116 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 117 | #include "net/url_request/file_protocol_handler.h" |
| 118 | #include "net/url_request/url_request_file_dir_job.h" |
| 119 | #endif |
| 120 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 121 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 122 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 123 | #include "net/url_request/ftp_protocol_handler.h" |
| 124 | #endif |
| 125 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 126 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 127 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 128 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 129 | #endif |
| 130 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 131 | using net::test::IsError; |
| 132 | using net::test::IsOk; |
| 133 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 134 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 135 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 136 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 137 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 138 | namespace net { |
| 139 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 140 | namespace { |
| 141 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 142 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 143 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 144 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 145 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 146 | const base::FilePath::CharType kTestFilePath[] = |
| 147 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 148 | |
| 149 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 150 | // Test file used in most FTP tests. |
| 151 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 152 | #endif |
| 153 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 154 | // Tests load timing information in the case a fresh connection was used, with |
| 155 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 156 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 157 | int connect_timing_flags) { |
| 158 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 159 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 160 | |
| 161 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 162 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 163 | |
| 164 | EXPECT_LE(load_timing_info.request_start, |
| 165 | load_timing_info.connect_timing.connect_start); |
| 166 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 167 | connect_timing_flags); |
| 168 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 169 | load_timing_info.send_start); |
| 170 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 171 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 172 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 173 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 174 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 175 | } |
| 176 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 177 | // Same as above, but with proxy times. |
| 178 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 179 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 180 | int connect_timing_flags) { |
| 181 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 182 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 183 | |
| 184 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 185 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 186 | |
| 187 | EXPECT_LE(load_timing_info.request_start, |
| 188 | load_timing_info.proxy_resolve_start); |
| 189 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 190 | load_timing_info.proxy_resolve_end); |
| 191 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 192 | load_timing_info.connect_timing.connect_start); |
| 193 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 194 | connect_timing_flags); |
| 195 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 196 | load_timing_info.send_start); |
| 197 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 198 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 199 | } |
| 200 | |
| 201 | // Same as above, but with a reused socket and proxy times. |
| 202 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 203 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 204 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 205 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 206 | |
| 207 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 208 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 209 | |
| 210 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 211 | |
| 212 | EXPECT_LE(load_timing_info.request_start, |
| 213 | load_timing_info.proxy_resolve_start); |
| 214 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 215 | load_timing_info.proxy_resolve_end); |
| 216 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 217 | load_timing_info.send_start); |
| 218 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 219 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 220 | } |
| 221 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 222 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 223 | // Tests load timing information in the case of a cache hit, when no cache |
| 224 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 225 | base::StringPiece TestNetResourceProvider(int key) { |
| 226 | return "header"; |
| 227 | } |
| 228 | |
| 229 | void FillBuffer(char* buffer, size_t len) { |
| 230 | static bool called = false; |
| 231 | if (!called) { |
| 232 | called = true; |
| 233 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 234 | srand(seed); |
| 235 | } |
| 236 | |
| 237 | for (size_t i = 0; i < len; i++) { |
| 238 | buffer[i] = static_cast<char>(rand()); |
| 239 | if (!buffer[i]) |
| 240 | buffer[i] = 'g'; |
| 241 | } |
| 242 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 243 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 244 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 245 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 246 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 247 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 248 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 249 | |
| 250 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 251 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 252 | |
| 253 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 254 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 255 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 256 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 257 | |
| 258 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 259 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 260 | } |
| 261 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 262 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 263 | // Tests load timing in the case that there is no HTTP response. This can be |
| 264 | // used to test in the case of errors or non-HTTP requests. |
| 265 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 266 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 267 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 268 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 269 | |
| 270 | // Only the request times should be non-null. |
| 271 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 272 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 273 | |
| 274 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 275 | |
| 276 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 277 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 278 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 279 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 280 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 281 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 282 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 283 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 284 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 285 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 286 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 287 | public: |
| 288 | TestPowerMonitorSource() {} |
| 289 | ~TestPowerMonitorSource() override {} |
| 290 | |
| 291 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 292 | |
| 293 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 294 | |
| 295 | bool IsOnBatteryPowerImpl() override { return false; } |
| 296 | |
| 297 | private: |
| 298 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 299 | }; |
| 300 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 301 | // Job that allows monitoring of its priority. |
| 302 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 303 | public: |
| 304 | // The latest priority of the job is always written to |request_priority_|. |
| 305 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 306 | NetworkDelegate* network_delegate, |
| 307 | RequestPriority* request_priority) |
| 308 | : URLRequestTestJob(request, network_delegate), |
| 309 | request_priority_(request_priority) { |
| 310 | *request_priority_ = DEFAULT_PRIORITY; |
| 311 | } |
| 312 | |
| 313 | void SetPriority(RequestPriority priority) override { |
| 314 | *request_priority_ = priority; |
| 315 | URLRequestTestJob::SetPriority(priority); |
| 316 | } |
| 317 | |
| 318 | private: |
| 319 | RequestPriority* const request_priority_; |
| 320 | }; |
| 321 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 322 | // Do a case-insensitive search through |haystack| for |needle|. |
| 323 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 324 | std::string::const_iterator it = std::search( |
| 325 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 326 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 327 | return it != haystack.end(); |
| 328 | } |
| 329 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 330 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 331 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 332 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 333 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 336 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 337 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 338 | // -1 means unknown. 0 means no encryption. |
| 339 | EXPECT_GT(ssl_info.security_bits, 0); |
| 340 | |
| 341 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 342 | uint16_t cipher_suite = |
| 343 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 344 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 347 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 348 | const GURL& host_url) { |
| 349 | std::string sent_value; |
| 350 | |
| 351 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 352 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 353 | |
| 354 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 355 | EXPECT_EQ("keep-alive", sent_value); |
| 356 | } |
| 357 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 358 | // A network delegate that allows the user to choose a subset of request stages |
| 359 | // to block in. When blocking, the delegate can do one of the following: |
| 360 | // * synchronously return a pre-specified error code, or |
| 361 | // * asynchronously return that value via an automatically called callback, |
| 362 | // or |
| 363 | // * block and wait for the user to do a callback. |
| 364 | // Additionally, the user may also specify a redirect URL -- then each request |
| 365 | // with the current URL different from the redirect target will be redirected |
| 366 | // to that target, in the on-before-URL-request stage, independent of whether |
| 367 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 368 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 369 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 370 | // Stages in which the delegate can block. |
| 371 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 372 | NOT_BLOCKED = 0, |
| 373 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 374 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 375 | ON_HEADERS_RECEIVED = 1 << 2, |
| 376 | ON_AUTH_REQUIRED = 1 << 3 |
| 377 | }; |
| 378 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 379 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 380 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 381 | enum BlockMode { |
| 382 | SYNCHRONOUS, // No callback, returns specified return values. |
| 383 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 384 | // specified return codes. |
| 385 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 386 | // |auth_retval_| are ignored. In every blocking stage the |
| 387 | // message loop is quit. |
| 388 | }; |
| 389 | |
| 390 | // Creates a delegate which does not block at all. |
| 391 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 392 | |
| 393 | // For users to trigger a callback returning |response|. |
| 394 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 395 | // Only call if |block_mode_| == USER_CALLBACK. |
| 396 | void DoCallback(int response); |
| 397 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 398 | |
| 399 | // Setters. |
| 400 | void set_retval(int retval) { |
| 401 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 402 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 403 | ASSERT_NE(OK, retval); |
| 404 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 407 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 408 | // |auth_credentials_| will be passed with the response. |
| 409 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 410 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 411 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 412 | auth_retval_ = auth_retval; |
| 413 | } |
| 414 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 415 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 416 | } |
| 417 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 418 | void set_redirect_url(const GURL& url) { |
| 419 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 422 | void set_block_on(int block_on) { |
| 423 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 426 | // Allows the user to check in which state did we block. |
| 427 | Stage stage_blocked_for_callback() const { |
| 428 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 429 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 433 | void RunCallback(int response, const CompletionCallback& callback); |
| 434 | void RunAuthCallback(AuthRequiredResponse response, |
| 435 | const AuthCallback& callback); |
| 436 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 437 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 438 | int OnBeforeURLRequest(URLRequest* request, |
| 439 | const CompletionCallback& callback, |
| 440 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 441 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 442 | int OnBeforeStartTransaction(URLRequest* request, |
| 443 | const CompletionCallback& callback, |
| 444 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 445 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 446 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 447 | URLRequest* request, |
| 448 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 449 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 450 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 451 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 452 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 453 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 454 | URLRequest* request, |
| 455 | const AuthChallengeInfo& auth_info, |
| 456 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 457 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 458 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 459 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 460 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 461 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 462 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 463 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 464 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 465 | |
| 466 | // Configuration parameters, can be adjusted by public methods: |
| 467 | const BlockMode block_mode_; |
| 468 | |
| 469 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 470 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 471 | int retval_; // To be returned in non-auth stages. |
| 472 | AuthRequiredResponse auth_retval_; |
| 473 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 474 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 475 | int block_on_; // Bit mask: in which stages to block. |
| 476 | |
| 477 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 478 | // callback. |
| 479 | AuthCredentials auth_credentials_; |
| 480 | AuthCredentials* target_auth_credentials_; |
| 481 | |
| 482 | // Internal variables, not set by not the user: |
| 483 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 484 | // USER_CALLBACK). |
| 485 | Stage stage_blocked_for_callback_; |
| 486 | |
| 487 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 488 | CompletionCallback callback_; |
| 489 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 490 | |
| 491 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 492 | |
| 493 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 494 | }; |
| 495 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 496 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 497 | : block_mode_(block_mode), |
| 498 | retval_(OK), |
| 499 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 500 | block_on_(0), |
| 501 | target_auth_credentials_(NULL), |
| 502 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 503 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 507 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 508 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 509 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 510 | CompletionCallback callback = callback_; |
| 511 | Reset(); |
| 512 | RunCallback(response, callback); |
| 513 | } |
| 514 | |
| 515 | void BlockingNetworkDelegate::DoAuthCallback( |
| 516 | NetworkDelegate::AuthRequiredResponse response) { |
| 517 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 518 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 519 | AuthCallback auth_callback = auth_callback_; |
| 520 | Reset(); |
| 521 | RunAuthCallback(response, auth_callback); |
| 522 | } |
| 523 | |
| 524 | void BlockingNetworkDelegate::RunCallback(int response, |
| 525 | const CompletionCallback& callback) { |
| 526 | callback.Run(response); |
| 527 | } |
| 528 | |
| 529 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 530 | const AuthCallback& callback) { |
| 531 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 532 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 533 | *target_auth_credentials_ = auth_credentials_; |
| 534 | } |
| 535 | callback.Run(response); |
| 536 | } |
| 537 | |
| 538 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 539 | URLRequest* request, |
| 540 | const CompletionCallback& callback, |
| 541 | GURL* new_url) { |
| 542 | if (redirect_url_ == request->url()) |
| 543 | return OK; // We've already seen this request and redirected elsewhere. |
| 544 | |
| 545 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 546 | |
| 547 | if (!redirect_url_.is_empty()) |
| 548 | *new_url = redirect_url_; |
| 549 | |
| 550 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 551 | } |
| 552 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 553 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 554 | URLRequest* request, |
| 555 | const CompletionCallback& callback, |
| 556 | HttpRequestHeaders* headers) { |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 557 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 558 | |
| 559 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 560 | } |
| 561 | |
| 562 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 563 | URLRequest* request, |
| 564 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 565 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 566 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 567 | GURL* allowed_unsafe_redirect_url) { |
| 568 | TestNetworkDelegate::OnHeadersReceived(request, |
| 569 | callback, |
| 570 | original_response_headers, |
| 571 | override_response_headers, |
| 572 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 573 | |
| 574 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 575 | } |
| 576 | |
| 577 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 578 | URLRequest* request, |
| 579 | const AuthChallengeInfo& auth_info, |
| 580 | const AuthCallback& callback, |
| 581 | AuthCredentials* credentials) { |
| 582 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 583 | credentials); |
| 584 | // Check that the user has provided callback for the previous blocked stage. |
| 585 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 586 | |
| 587 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 588 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 589 | } |
| 590 | |
| 591 | target_auth_credentials_ = credentials; |
| 592 | |
| 593 | switch (block_mode_) { |
| 594 | case SYNCHRONOUS: |
| 595 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 596 | *target_auth_credentials_ = auth_credentials_; |
| 597 | return auth_retval_; |
| 598 | |
| 599 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 600 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 601 | FROM_HERE, |
| 602 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 603 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 604 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 605 | |
| 606 | case USER_CALLBACK: |
| 607 | auth_callback_ = callback; |
| 608 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 609 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 610 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 611 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 612 | } |
| 613 | NOTREACHED(); |
| 614 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 615 | } |
| 616 | |
| 617 | void BlockingNetworkDelegate::Reset() { |
| 618 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 619 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 620 | callback_.Reset(); |
| 621 | auth_callback_.Reset(); |
| 622 | } |
| 623 | |
| 624 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 625 | BlockingNetworkDelegate::Stage stage, |
| 626 | const CompletionCallback& callback) { |
| 627 | // Check that the user has provided callback for the previous blocked stage. |
| 628 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 629 | |
| 630 | if ((block_on_ & stage) == 0) { |
| 631 | return OK; |
| 632 | } |
| 633 | |
| 634 | switch (block_mode_) { |
| 635 | case SYNCHRONOUS: |
| 636 | EXPECT_NE(OK, retval_); |
| 637 | return retval_; |
| 638 | |
| 639 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 640 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 641 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 642 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 643 | return ERR_IO_PENDING; |
| 644 | |
| 645 | case USER_CALLBACK: |
| 646 | callback_ = callback; |
| 647 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 648 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 649 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | return ERR_IO_PENDING; |
| 651 | } |
| 652 | NOTREACHED(); |
| 653 | return 0; |
| 654 | } |
| 655 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 656 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 657 | public: |
| 658 | // Does not own |delegate|. |
| 659 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 660 | NetworkDelegate* delegate) |
| 661 | : TestURLRequestContext(true) { |
| 662 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 663 | set_network_delegate(delegate); |
| 664 | Init(); |
| 665 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 666 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 667 | }; |
| 668 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 669 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 670 | // URI and report to be sent. |
| 671 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 672 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 673 | public: |
| 674 | MockCertificateReportSender() {} |
| 675 | ~MockCertificateReportSender() override {} |
| 676 | |
| 677 | void Send(const GURL& report_uri, const std::string& report) override { |
| 678 | latest_report_uri_ = report_uri; |
| 679 | latest_report_ = report; |
| 680 | } |
| 681 | |
estark | f297912 | 2016-04-05 17:04:39 | [diff] [blame] | 682 | void SetErrorCallback( |
| 683 | const base::Callback<void(const GURL&, int)>& error_callback) override {} |
| 684 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 685 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 686 | const std::string& latest_report() { return latest_report_; } |
| 687 | |
| 688 | private: |
| 689 | GURL latest_report_uri_; |
| 690 | std::string latest_report_; |
| 691 | }; |
| 692 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 693 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 694 | public: |
| 695 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 696 | bool OnAreStrictSecureCookiesEnabled() const override { return true; } |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 697 | }; |
| 698 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 699 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 700 | // This is needed because after the certificate failure, the URLRequest will |
| 701 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 702 | // The partial SSLInfo does not have the OCSP information filled out. |
| 703 | class OCSPErrorTestDelegate : public TestDelegate { |
| 704 | public: |
| 705 | void OnSSLCertificateError(URLRequest* request, |
| 706 | const SSLInfo& ssl_info, |
| 707 | bool fatal) override { |
| 708 | ssl_info_ = ssl_info; |
| 709 | on_ssl_certificate_error_called_ = true; |
| 710 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 711 | } |
| 712 | |
| 713 | bool on_ssl_certificate_error_called() { |
| 714 | return on_ssl_certificate_error_called_; |
| 715 | } |
| 716 | |
| 717 | SSLInfo ssl_info() { return ssl_info_; } |
| 718 | |
| 719 | private: |
| 720 | bool on_ssl_certificate_error_called_ = false; |
| 721 | SSLInfo ssl_info_; |
| 722 | }; |
| 723 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 724 | } // namespace |
| 725 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 726 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 727 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 728 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 729 | URLRequestTest() : default_context_(true) { |
| 730 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 731 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 732 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 733 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 734 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 735 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 736 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 737 | // URLRequestJobs may post clean-up tasks on destruction. |
| 738 | base::RunLoop().RunUntilIdle(); |
| 739 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 740 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 741 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 742 | SetUpFactory(); |
| 743 | default_context_.set_job_factory(job_factory_.get()); |
| 744 | default_context_.Init(); |
| 745 | PlatformTest::SetUp(); |
| 746 | } |
| 747 | |
| 748 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 749 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 750 | "data", base::WrapUnique(new DataProtocolHandler)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 751 | #if !defined(DISABLE_FILE_SUPPORT) |
| 752 | job_factory_impl_->SetProtocolHandler( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 753 | "file", base::WrapUnique(new FileProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 754 | base::ThreadTaskRunnerHandle::Get()))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 755 | #endif |
| 756 | } |
| 757 | |
| 758 | TestNetworkDelegate* default_network_delegate() { |
| 759 | return &default_network_delegate_; |
| 760 | } |
| 761 | |
| 762 | const TestURLRequestContext& default_context() const { |
| 763 | return default_context_; |
| 764 | } |
| 765 | |
| 766 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 767 | // Adds the TestJobInterceptor to the default context. |
| 768 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 769 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 770 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 771 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 772 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 773 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 774 | } |
| 775 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 776 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 777 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 778 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 779 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 780 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 781 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 782 | }; |
| 783 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 784 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 785 | TestDelegate d; |
| 786 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 787 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 788 | GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 789 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 790 | r->Start(); |
| 791 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 792 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 793 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 794 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 795 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 796 | EXPECT_FALSE(d.received_data_before_response()); |
| 797 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 798 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 799 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 800 | |
| 801 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 802 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
| 806 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 807 | TestDelegate d; |
| 808 | { |
| 809 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 810 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 811 | GURL("data:image/png;base64," |
| 812 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 813 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 814 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 815 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 816 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 817 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 818 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 819 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 820 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 821 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 822 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 823 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 824 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 825 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 826 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 827 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 828 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 829 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 830 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 831 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 832 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 833 | r->Start(); |
| 834 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 835 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 836 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 837 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 838 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 839 | EXPECT_FALSE(d.received_data_before_response()); |
| 840 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 841 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 842 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 843 | |
| 844 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 845 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 849 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 850 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 851 | base::FilePath app_path; |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 852 | |
| 853 | #if defined(OS_ANDROID) |
| 854 | // Android devices are not guaranteed to be able to read /proc/self/exe |
| 855 | // Use /etc/hosts instead |
| 856 | app_path = base::FilePath("/etc/hosts"); |
| 857 | #else |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 858 | PathService::Get(base::FILE_EXE, &app_path); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 859 | #endif // OS_ANDROID |
| 860 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 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] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 867 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 868 | r->Start(); |
| 869 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 870 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 871 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 872 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 873 | int64_t file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 874 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 875 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 876 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 877 | EXPECT_EQ(1, d.response_started_count()); |
| 878 | EXPECT_FALSE(d.received_data_before_response()); |
| 879 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 880 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 881 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 882 | |
| 883 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 884 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 888 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 889 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 890 | PathService::Get(base::FILE_EXE, &app_path); |
| 891 | GURL app_url = FilePathToFileURL(app_path); |
| 892 | |
| 893 | TestDelegate d; |
| 894 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 895 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 896 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 897 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 898 | r->Start(); |
| 899 | EXPECT_TRUE(r->is_pending()); |
| 900 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 901 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 902 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 903 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 904 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 905 | } |
| 906 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 907 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 908 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 909 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 910 | FillBuffer(buffer.get(), buffer_size); |
| 911 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 912 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 913 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 914 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 915 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 916 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 917 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 918 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 919 | |
| 920 | const size_t first_byte_position = 500; |
| 921 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 922 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 923 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 924 | buffer.get() + last_byte_position + 1); |
| 925 | |
| 926 | TestDelegate d; |
| 927 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 928 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 929 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 930 | |
| 931 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 932 | headers.SetHeader( |
| 933 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 934 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 935 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 936 | r->SetExtraRequestHeaders(headers); |
| 937 | r->Start(); |
| 938 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 939 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 940 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 941 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 942 | EXPECT_EQ(1, d.response_started_count()); |
| 943 | EXPECT_FALSE(d.received_data_before_response()); |
| 944 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 945 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 946 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 947 | } |
| 948 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 949 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 953 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 954 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 955 | FillBuffer(buffer.get(), buffer_size); |
| 956 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 957 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 958 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 959 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 960 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 961 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 962 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 963 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | |
| 965 | const size_t first_byte_position = 500; |
| 966 | const size_t last_byte_position = buffer_size - 1; |
| 967 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 968 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 969 | buffer.get() + last_byte_position + 1); |
| 970 | |
| 971 | TestDelegate d; |
| 972 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 973 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 974 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | |
| 976 | HttpRequestHeaders headers; |
| 977 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 978 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 979 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 980 | r->SetExtraRequestHeaders(headers); |
| 981 | r->Start(); |
| 982 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 983 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 984 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 985 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 986 | EXPECT_EQ(1, d.response_started_count()); |
| 987 | EXPECT_FALSE(d.received_data_before_response()); |
| 988 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 989 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 990 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 991 | } |
| 992 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 993 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 997 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 998 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 999 | FillBuffer(buffer.get(), buffer_size); |
| 1000 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1001 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1002 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1003 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1004 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1005 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 1006 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 1007 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1008 | |
| 1009 | TestDelegate d; |
| 1010 | { |
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(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1013 | |
| 1014 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1015 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1016 | r->SetExtraRequestHeaders(headers); |
| 1017 | r->Start(); |
| 1018 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1019 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1020 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1021 | EXPECT_TRUE(d.request_failed()); |
| 1022 | } |
| 1023 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 1024 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1025 | } |
| 1026 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1027 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 1028 | base::ScopedTempDir temp_dir; |
| 1029 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 1030 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 1031 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1032 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 1033 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1034 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1035 | |
| 1036 | { |
| 1037 | TestDelegate d; |
| 1038 | TestNetworkDelegate network_delegate; |
| 1039 | network_delegate.set_can_access_files(true); |
| 1040 | default_context_.set_network_delegate(&network_delegate); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1041 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1042 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1043 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1044 | base::RunLoop().Run(); |
| 1045 | EXPECT_FALSE(d.request_failed()); |
| 1046 | EXPECT_EQ(test_data, d.data_received()); |
| 1047 | } |
| 1048 | |
| 1049 | { |
| 1050 | TestDelegate d; |
| 1051 | TestNetworkDelegate network_delegate; |
| 1052 | network_delegate.set_can_access_files(false); |
| 1053 | default_context_.set_network_delegate(&network_delegate); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1054 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1055 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1056 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1057 | base::RunLoop().Run(); |
| 1058 | EXPECT_TRUE(d.request_failed()); |
| 1059 | EXPECT_EQ("", d.data_received()); |
| 1060 | } |
| 1061 | } |
| 1062 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1063 | |
| 1064 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1065 | // Put in mock resource provider. |
| 1066 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1067 | |
| 1068 | TestDelegate d; |
| 1069 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1070 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1071 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1072 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1073 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1074 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1075 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1076 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1077 | req->Start(); |
| 1078 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1079 | |
| 1080 | d.set_cancel_in_received_data_pending(true); |
| 1081 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1082 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Take out mock resource provider. |
| 1086 | NetModule::SetResourceProvider(NULL); |
| 1087 | } |
| 1088 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1089 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1090 | // Verify the general sanity of the the output of the file: |
| 1091 | // directory lister by checking for the output of a known existing |
| 1092 | // file. |
| 1093 | const char sentinel_name[] = "filedir-sentinel"; |
| 1094 | |
| 1095 | base::FilePath path; |
| 1096 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1097 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1098 | |
| 1099 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1100 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1101 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1102 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1103 | base::RunLoop().Run(); |
| 1104 | |
| 1105 | // Generate entry for the sentinel file. |
| 1106 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1107 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1108 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1109 | EXPECT_GT(info.size, 0); |
| 1110 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1111 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1112 | std::string(sentinel_name), |
| 1113 | false /* is_dir */, |
| 1114 | info.size, |
| 1115 | info.last_modified); |
| 1116 | |
| 1117 | ASSERT_LT(0, d.bytes_received()); |
| 1118 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1119 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1120 | // Check for the entry generated for the "sentinel" file. |
| 1121 | const std::string& data = d.data_received(); |
| 1122 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1123 | } |
| 1124 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1125 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1126 | // There is an implicit redirect when loading a file path that matches a |
| 1127 | // directory and does not end with a slash. Ensure that following such |
| 1128 | // redirects does not crash. See http://crbug.com/18686. |
| 1129 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1130 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1131 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1132 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1133 | |
| 1134 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1135 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1136 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1137 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1138 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1139 | |
| 1140 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1141 | ASSERT_LT(0, d.bytes_received()); |
| 1142 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1143 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | #if defined(OS_WIN) |
| 1147 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1148 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1149 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1150 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1151 | default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1152 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1153 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1154 | |
| 1155 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1156 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1157 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1158 | #endif // defined(OS_WIN) |
| 1159 | |
| 1160 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1161 | |
| 1162 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1163 | TestDelegate d; |
| 1164 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1165 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1166 | GURL("invalid url"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1167 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1168 | r->Start(); |
| 1169 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1170 | |
| 1171 | base::RunLoop().Run(); |
| 1172 | EXPECT_TRUE(d.request_failed()); |
| 1173 | } |
| 1174 | } |
| 1175 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1176 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1177 | TestURLRequestContext context; |
| 1178 | TestNetworkDelegate network_delegate; |
| 1179 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1180 | context.set_network_delegate(&network_delegate); |
| 1181 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1182 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1183 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1184 | req->SetReferrer("https://somewhere.com/"); |
| 1185 | |
| 1186 | req->Start(); |
| 1187 | base::RunLoop().Run(); |
| 1188 | EXPECT_TRUE(d.request_failed()); |
| 1189 | } |
| 1190 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1191 | #if defined(OS_WIN) |
| 1192 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1193 | base::FilePath app_path; |
| 1194 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1195 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1196 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1197 | |
| 1198 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1199 | |
| 1200 | base::win::ScopedCOMInitializer com_initializer; |
| 1201 | |
| 1202 | // Temporarily create a shortcut for test |
| 1203 | { |
| 1204 | base::win::ScopedComPtr<IShellLink> shell; |
| 1205 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1206 | CLSCTX_INPROC_SERVER))); |
| 1207 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1208 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1209 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1210 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1211 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1212 | } |
| 1213 | |
| 1214 | TestDelegate d; |
| 1215 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1216 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1217 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1218 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1219 | r->Start(); |
| 1220 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1221 | |
| 1222 | base::RunLoop().Run(); |
| 1223 | |
| 1224 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1225 | GetFileAttributesEx(app_path.value().c_str(), |
| 1226 | GetFileExInfoStandard, &data); |
| 1227 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1228 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1229 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1230 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1231 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1232 | DWORD read_size; |
| 1233 | BOOL result; |
| 1234 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1235 | &read_size, NULL); |
| 1236 | std::string content(buffer.get(), read_size); |
| 1237 | CloseHandle(file); |
| 1238 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1239 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1240 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1241 | EXPECT_EQ(content, d.data_received()); |
| 1242 | } |
| 1243 | |
| 1244 | // Clean the shortcut |
| 1245 | DeleteFile(lnk_path.c_str()); |
| 1246 | } |
| 1247 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1248 | |
| 1249 | // Custom URLRequestJobs for use with interceptor tests |
| 1250 | class RestartTestJob : public URLRequestTestJob { |
| 1251 | public: |
| 1252 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1253 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1254 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1255 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1256 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1257 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1258 | }; |
| 1259 | |
| 1260 | class CancelTestJob : public URLRequestTestJob { |
| 1261 | public: |
| 1262 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1263 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1264 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1265 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1266 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1267 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1268 | }; |
| 1269 | |
| 1270 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1271 | public: |
| 1272 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1273 | NetworkDelegate* network_delegate) |
| 1274 | : URLRequestTestJob(request, network_delegate, true) { |
| 1275 | } |
| 1276 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1277 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1278 | request_->Cancel(); |
| 1279 | this->NotifyRestartRequired(); |
| 1280 | } |
| 1281 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1282 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1283 | }; |
| 1284 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1285 | // An Interceptor for use with interceptor tests. |
| 1286 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1287 | public: |
| 1288 | // Static getters for canned response header and data strings. |
| 1289 | static std::string ok_data() { |
| 1290 | return URLRequestTestJob::test_data_1(); |
| 1291 | } |
| 1292 | |
| 1293 | static std::string ok_headers() { |
| 1294 | return URLRequestTestJob::test_headers(); |
| 1295 | } |
| 1296 | |
| 1297 | static std::string redirect_data() { |
| 1298 | return std::string(); |
| 1299 | } |
| 1300 | |
| 1301 | static std::string redirect_headers() { |
| 1302 | return URLRequestTestJob::test_redirect_headers(); |
| 1303 | } |
| 1304 | |
| 1305 | static std::string error_data() { |
| 1306 | return std::string("ohhh nooooo mr. bill!"); |
| 1307 | } |
| 1308 | |
| 1309 | static std::string error_headers() { |
| 1310 | return URLRequestTestJob::test_error_headers(); |
| 1311 | } |
| 1312 | |
| 1313 | MockURLRequestInterceptor() |
| 1314 | : intercept_main_request_(false), restart_main_request_(false), |
| 1315 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1316 | simulate_main_network_error_(false), |
| 1317 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1318 | intercept_final_response_(false), cancel_final_request_(false), |
| 1319 | use_url_request_http_job_(false), |
| 1320 | did_intercept_main_(false), did_restart_main_(false), |
| 1321 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1322 | did_simulate_error_main_(false), |
| 1323 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1324 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1325 | } |
| 1326 | |
| 1327 | ~MockURLRequestInterceptor() override { |
| 1328 | } |
| 1329 | |
| 1330 | // URLRequestInterceptor implementation: |
| 1331 | URLRequestJob* MaybeInterceptRequest( |
| 1332 | URLRequest* request, |
| 1333 | NetworkDelegate* network_delegate) const override { |
| 1334 | if (restart_main_request_) { |
| 1335 | restart_main_request_ = false; |
| 1336 | did_restart_main_ = true; |
| 1337 | return new RestartTestJob(request, network_delegate); |
| 1338 | } |
| 1339 | if (cancel_main_request_) { |
| 1340 | cancel_main_request_ = false; |
| 1341 | did_cancel_main_ = true; |
| 1342 | return new CancelTestJob(request, network_delegate); |
| 1343 | } |
| 1344 | if (cancel_then_restart_main_request_) { |
| 1345 | cancel_then_restart_main_request_ = false; |
| 1346 | did_cancel_then_restart_main_ = true; |
| 1347 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1348 | } |
| 1349 | if (simulate_main_network_error_) { |
| 1350 | simulate_main_network_error_ = false; |
| 1351 | did_simulate_error_main_ = true; |
| 1352 | if (use_url_request_http_job_) { |
| 1353 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1354 | } |
| 1355 | // This job will result in error since the requested URL is not one of the |
| 1356 | // URLs supported by these tests. |
| 1357 | return new URLRequestTestJob(request, network_delegate, true); |
| 1358 | } |
| 1359 | if (!intercept_main_request_) |
| 1360 | return nullptr; |
| 1361 | intercept_main_request_ = false; |
| 1362 | did_intercept_main_ = true; |
| 1363 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1364 | network_delegate, |
| 1365 | main_headers_, |
| 1366 | main_data_, |
| 1367 | true); |
| 1368 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1369 | return job; |
| 1370 | } |
| 1371 | |
| 1372 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1373 | NetworkDelegate* network_delegate, |
| 1374 | const GURL& location) const override { |
| 1375 | if (cancel_redirect_request_) { |
| 1376 | cancel_redirect_request_ = false; |
| 1377 | did_cancel_redirect_ = true; |
| 1378 | return new CancelTestJob(request, network_delegate); |
| 1379 | } |
| 1380 | if (!intercept_redirect_) |
| 1381 | return nullptr; |
| 1382 | intercept_redirect_ = false; |
| 1383 | did_intercept_redirect_ = true; |
| 1384 | if (use_url_request_http_job_) { |
| 1385 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1386 | } |
| 1387 | return new URLRequestTestJob(request, |
| 1388 | network_delegate, |
| 1389 | redirect_headers_, |
| 1390 | redirect_data_, |
| 1391 | true); |
| 1392 | } |
| 1393 | |
| 1394 | URLRequestJob* MaybeInterceptResponse( |
| 1395 | URLRequest* request, |
| 1396 | NetworkDelegate* network_delegate) const override { |
| 1397 | if (cancel_final_request_) { |
| 1398 | cancel_final_request_ = false; |
| 1399 | did_cancel_final_ = true; |
| 1400 | return new CancelTestJob(request, network_delegate); |
| 1401 | } |
| 1402 | if (!intercept_final_response_) |
| 1403 | return nullptr; |
| 1404 | intercept_final_response_ = false; |
| 1405 | did_intercept_final_ = true; |
| 1406 | if (use_url_request_http_job_) { |
| 1407 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1408 | } |
| 1409 | return new URLRequestTestJob(request, |
| 1410 | network_delegate, |
| 1411 | final_headers_, |
| 1412 | final_data_, |
| 1413 | true); |
| 1414 | } |
| 1415 | |
| 1416 | void set_intercept_main_request(bool intercept_main_request) { |
| 1417 | intercept_main_request_ = intercept_main_request; |
| 1418 | } |
| 1419 | |
| 1420 | void set_main_headers(const std::string& main_headers) { |
| 1421 | main_headers_ = main_headers; |
| 1422 | } |
| 1423 | |
| 1424 | void set_main_data(const std::string& main_data) { |
| 1425 | main_data_ = main_data; |
| 1426 | } |
| 1427 | |
| 1428 | void set_main_request_load_timing_info( |
| 1429 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1430 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1431 | } |
| 1432 | |
| 1433 | void set_restart_main_request(bool restart_main_request) { |
| 1434 | restart_main_request_ = restart_main_request; |
| 1435 | } |
| 1436 | |
| 1437 | void set_cancel_main_request(bool cancel_main_request) { |
| 1438 | cancel_main_request_ = cancel_main_request; |
| 1439 | } |
| 1440 | |
| 1441 | void set_cancel_then_restart_main_request( |
| 1442 | bool cancel_then_restart_main_request) { |
| 1443 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1444 | } |
| 1445 | |
| 1446 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1447 | simulate_main_network_error_ = simulate_main_network_error; |
| 1448 | } |
| 1449 | |
| 1450 | void set_intercept_redirect(bool intercept_redirect) { |
| 1451 | intercept_redirect_ = intercept_redirect; |
| 1452 | } |
| 1453 | |
| 1454 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1455 | redirect_headers_ = redirect_headers; |
| 1456 | } |
| 1457 | |
| 1458 | void set_redirect_data(const std::string& redirect_data) { |
| 1459 | redirect_data_ = redirect_data; |
| 1460 | } |
| 1461 | |
| 1462 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1463 | cancel_redirect_request_ = cancel_redirect_request; |
| 1464 | } |
| 1465 | |
| 1466 | void set_intercept_final_response(bool intercept_final_response) { |
| 1467 | intercept_final_response_ = intercept_final_response; |
| 1468 | } |
| 1469 | |
| 1470 | void set_final_headers(const std::string& final_headers) { |
| 1471 | final_headers_ = final_headers; |
| 1472 | } |
| 1473 | |
| 1474 | void set_final_data(const std::string& final_data) { |
| 1475 | final_data_ = final_data; |
| 1476 | } |
| 1477 | |
| 1478 | void set_cancel_final_request(bool cancel_final_request) { |
| 1479 | cancel_final_request_ = cancel_final_request; |
| 1480 | } |
| 1481 | |
| 1482 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1483 | use_url_request_http_job_ = use_url_request_http_job; |
| 1484 | } |
| 1485 | |
| 1486 | bool did_intercept_main() const { |
| 1487 | return did_intercept_main_; |
| 1488 | } |
| 1489 | |
| 1490 | bool did_restart_main() const { |
| 1491 | return did_restart_main_; |
| 1492 | } |
| 1493 | |
| 1494 | bool did_cancel_main() const { |
| 1495 | return did_cancel_main_; |
| 1496 | } |
| 1497 | |
| 1498 | bool did_cancel_then_restart_main() const { |
| 1499 | return did_cancel_then_restart_main_; |
| 1500 | } |
| 1501 | |
| 1502 | bool did_simulate_error_main() const { |
| 1503 | return did_simulate_error_main_; |
| 1504 | } |
| 1505 | |
| 1506 | bool did_intercept_redirect() const { |
| 1507 | return did_intercept_redirect_; |
| 1508 | } |
| 1509 | |
| 1510 | bool did_cancel_redirect() const { |
| 1511 | return did_cancel_redirect_; |
| 1512 | } |
| 1513 | |
| 1514 | bool did_intercept_final() const { |
| 1515 | return did_intercept_final_; |
| 1516 | } |
| 1517 | |
| 1518 | bool did_cancel_final() const { |
| 1519 | return did_cancel_final_; |
| 1520 | } |
| 1521 | |
| 1522 | private: |
| 1523 | // Indicate whether to intercept the main request, and if so specify the |
| 1524 | // response to return and the LoadTimingInfo to use. |
| 1525 | mutable bool intercept_main_request_; |
| 1526 | mutable std::string main_headers_; |
| 1527 | mutable std::string main_data_; |
| 1528 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1529 | |
| 1530 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1531 | mutable bool restart_main_request_; |
| 1532 | mutable bool cancel_main_request_; |
| 1533 | mutable bool cancel_then_restart_main_request_; |
| 1534 | mutable bool simulate_main_network_error_; |
| 1535 | |
| 1536 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1537 | // return. |
| 1538 | mutable bool intercept_redirect_; |
| 1539 | mutable std::string redirect_headers_; |
| 1540 | mutable std::string redirect_data_; |
| 1541 | |
| 1542 | // Cancel the request within MaybeInterceptRedirect. |
| 1543 | mutable bool cancel_redirect_request_; |
| 1544 | |
| 1545 | // Indicate whether to intercept the final response, and if so specify the |
| 1546 | // response to return. |
| 1547 | mutable bool intercept_final_response_; |
| 1548 | mutable std::string final_headers_; |
| 1549 | mutable std::string final_data_; |
| 1550 | |
| 1551 | // Cancel the final request within MaybeInterceptResponse. |
| 1552 | mutable bool cancel_final_request_; |
| 1553 | |
| 1554 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1555 | mutable bool use_url_request_http_job_; |
| 1556 | |
| 1557 | // These indicate if the interceptor did something or not. |
| 1558 | mutable bool did_intercept_main_; |
| 1559 | mutable bool did_restart_main_; |
| 1560 | mutable bool did_cancel_main_; |
| 1561 | mutable bool did_cancel_then_restart_main_; |
| 1562 | mutable bool did_simulate_error_main_; |
| 1563 | mutable bool did_intercept_redirect_; |
| 1564 | mutable bool did_cancel_redirect_; |
| 1565 | mutable bool did_intercept_final_; |
| 1566 | mutable bool did_cancel_final_; |
| 1567 | }; |
| 1568 | |
| 1569 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1570 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1571 | public: |
| 1572 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1573 | } |
| 1574 | |
| 1575 | ~URLRequestInterceptorTest() override { |
| 1576 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1577 | base::RunLoop().RunUntilIdle(); |
| 1578 | } |
| 1579 | |
| 1580 | void SetUpFactory() override { |
| 1581 | interceptor_ = new MockURLRequestInterceptor(); |
| 1582 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1583 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | MockURLRequestInterceptor* interceptor() const { |
| 1587 | return interceptor_; |
| 1588 | } |
| 1589 | |
| 1590 | private: |
| 1591 | MockURLRequestInterceptor* interceptor_; |
| 1592 | }; |
| 1593 | |
| 1594 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1595 | // Intercept the main request and respond with a simple response. |
| 1596 | interceptor()->set_intercept_main_request(true); |
| 1597 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1598 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1599 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1600 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1601 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1602 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1603 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1604 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1605 | req->SetUserData(nullptr, user_data0); |
| 1606 | req->SetUserData(&user_data1, user_data1); |
| 1607 | req->SetUserData(&user_data2, user_data2); |
| 1608 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1609 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1610 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1611 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1612 | // Make sure we can retrieve our specific user data. |
| 1613 | EXPECT_EQ(user_data0, req->GetUserData(nullptr)); |
| 1614 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1615 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1616 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1617 | // Check that we got one good response. |
| 1618 | EXPECT_TRUE(req->status().is_success()); |
| 1619 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1620 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1621 | EXPECT_EQ(1, d.response_started_count()); |
| 1622 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1623 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1624 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1625 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1626 | // Intercept the main request and respond with a redirect. |
| 1627 | interceptor()->set_intercept_main_request(true); |
| 1628 | interceptor()->set_main_headers( |
| 1629 | MockURLRequestInterceptor::redirect_headers()); |
| 1630 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1631 | |
| 1632 | // Intercept that redirect and respond with a final OK response. |
| 1633 | interceptor()->set_intercept_redirect(true); |
| 1634 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1635 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1636 | |
| 1637 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1638 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1639 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1640 | req->set_method("GET"); |
| 1641 | req->Start(); |
| 1642 | base::RunLoop().Run(); |
| 1643 | |
| 1644 | // Check that the interceptor got called as expected. |
| 1645 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1646 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1647 | |
| 1648 | // Check that we got one good response. |
| 1649 | EXPECT_TRUE(req->status().is_success()); |
| 1650 | if (req->status().is_success()) |
| 1651 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1652 | |
| 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, InterceptServerError) { |
| 1659 | // Intercept the main request to generate a server error response. |
| 1660 | interceptor()->set_intercept_main_request(true); |
| 1661 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1662 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1663 | |
| 1664 | // Intercept that error and respond with an OK response. |
| 1665 | interceptor()->set_intercept_final_response(true); |
| 1666 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1667 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1668 | |
| 1669 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1670 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1671 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1672 | req->set_method("GET"); |
| 1673 | req->Start(); |
| 1674 | base::RunLoop().Run(); |
| 1675 | |
| 1676 | // Check that the interceptor got called as expected. |
| 1677 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1678 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1679 | |
| 1680 | // Check that we got one good response. |
| 1681 | EXPECT_TRUE(req->status().is_success()); |
| 1682 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1683 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1684 | EXPECT_EQ(1, d.response_started_count()); |
| 1685 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1686 | } |
| 1687 | |
| 1688 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1689 | // Intercept the main request to simulate a network error. |
| 1690 | interceptor()->set_simulate_main_network_error(true); |
| 1691 | |
| 1692 | // Intercept that error and respond with an OK response. |
| 1693 | interceptor()->set_intercept_final_response(true); |
| 1694 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1695 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1696 | |
| 1697 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1698 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1699 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1700 | req->set_method("GET"); |
| 1701 | req->Start(); |
| 1702 | base::RunLoop().Run(); |
| 1703 | |
| 1704 | // Check that the interceptor got called as expected. |
| 1705 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1706 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1707 | |
| 1708 | // Check that we received one good response. |
| 1709 | EXPECT_TRUE(req->status().is_success()); |
| 1710 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 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, InterceptRestartRequired) { |
| 1717 | // Restart the main request. |
| 1718 | interceptor()->set_restart_main_request(true); |
| 1719 | |
| 1720 | // then intercept the new main request and respond with an OK response |
| 1721 | interceptor()->set_intercept_main_request(true); |
| 1722 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1723 | interceptor()->set_main_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_restart_main()); |
| 1734 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1735 | |
| 1736 | // Check that we received one good response. |
| 1737 | EXPECT_TRUE(req->status().is_success()); |
| 1738 | if (req->status().is_success()) |
| 1739 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1740 | |
| 1741 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1742 | EXPECT_EQ(1, d.response_started_count()); |
| 1743 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1744 | } |
| 1745 | |
| 1746 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1747 | // Intercept the main request and cancel from within the restarted job. |
| 1748 | interceptor()->set_cancel_main_request(true); |
| 1749 | |
| 1750 | // Set up to intercept the final response and override it with an OK response. |
| 1751 | interceptor()->set_intercept_final_response(true); |
| 1752 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1753 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1754 | |
| 1755 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1756 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1757 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1758 | req->set_method("GET"); |
| 1759 | req->Start(); |
| 1760 | base::RunLoop().Run(); |
| 1761 | |
| 1762 | // Check that the interceptor got called as expected. |
| 1763 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1764 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1765 | |
| 1766 | // Check that we see a canceled request. |
| 1767 | EXPECT_FALSE(req->status().is_success()); |
| 1768 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1769 | } |
| 1770 | |
| 1771 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1772 | // Intercept the main request and respond with a redirect. |
| 1773 | interceptor()->set_intercept_main_request(true); |
| 1774 | interceptor()->set_main_headers( |
| 1775 | MockURLRequestInterceptor::redirect_headers()); |
| 1776 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1777 | |
| 1778 | // Intercept the redirect and cancel from within that job. |
| 1779 | interceptor()->set_cancel_redirect_request(true); |
| 1780 | |
| 1781 | // Set up to intercept the final response and override it with an OK response. |
| 1782 | interceptor()->set_intercept_final_response(true); |
| 1783 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1784 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1785 | |
| 1786 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1787 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1788 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1789 | req->set_method("GET"); |
| 1790 | req->Start(); |
| 1791 | base::RunLoop().Run(); |
| 1792 | |
| 1793 | // Check that the interceptor got called as expected. |
| 1794 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1795 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 1796 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1797 | |
| 1798 | // Check that we see a canceled request. |
| 1799 | EXPECT_FALSE(req->status().is_success()); |
| 1800 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1801 | } |
| 1802 | |
| 1803 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 1804 | // Intercept the main request to simulate a network error. |
| 1805 | interceptor()->set_simulate_main_network_error(true); |
| 1806 | |
| 1807 | // Set up to intercept final the response and cancel from within that job. |
| 1808 | interceptor()->set_cancel_final_request(true); |
| 1809 | |
| 1810 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1811 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1812 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1813 | req->set_method("GET"); |
| 1814 | req->Start(); |
| 1815 | base::RunLoop().Run(); |
| 1816 | |
| 1817 | // Check that the interceptor got called as expected. |
| 1818 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1819 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 1820 | |
| 1821 | // Check that we see a canceled request. |
| 1822 | EXPECT_FALSE(req->status().is_success()); |
| 1823 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1824 | } |
| 1825 | |
| 1826 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 1827 | // Intercept the main request and cancel then restart from within that job. |
| 1828 | interceptor()->set_cancel_then_restart_main_request(true); |
| 1829 | |
| 1830 | // Set up to intercept the final response and override it with an OK response. |
| 1831 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 1832 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1833 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1834 | |
| 1835 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1836 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1837 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1838 | req->set_method("GET"); |
| 1839 | req->Start(); |
| 1840 | base::RunLoop().Run(); |
| 1841 | |
| 1842 | // Check that the interceptor got called as expected. |
| 1843 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 1844 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1845 | |
| 1846 | // Check that we see a canceled request. |
| 1847 | EXPECT_FALSE(req->status().is_success()); |
| 1848 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1852 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1853 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1854 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1855 | int connect_time_flags, |
| 1856 | bool used_proxy) { |
| 1857 | LoadTimingInfo load_timing; |
| 1858 | load_timing.socket_log_id = 1; |
| 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 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1866 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1867 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1868 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1869 | } |
| 1870 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1871 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1872 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1873 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1874 | } |
| 1875 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1876 | |
| 1877 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1878 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1879 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1880 | return load_timing; |
| 1881 | } |
| 1882 | |
| 1883 | // Same as above, but in the case of a reused socket. |
| 1884 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1885 | bool used_proxy) { |
| 1886 | LoadTimingInfo load_timing; |
| 1887 | load_timing.socket_log_id = 1; |
| 1888 | load_timing.socket_reused = true; |
| 1889 | |
| 1890 | if (used_proxy) { |
| 1891 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1892 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1893 | } |
| 1894 | |
| 1895 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1896 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1897 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1898 | return load_timing; |
| 1899 | } |
| 1900 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1901 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 1902 | const LoadTimingInfo& job_load_timing, |
| 1903 | const URLRequestContext& context, |
| 1904 | MockURLRequestInterceptor* interceptor) { |
| 1905 | interceptor->set_intercept_main_request(true); |
| 1906 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 1907 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1908 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1909 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1910 | req->Start(); |
| 1911 | base::RunLoop().Run(); |
| 1912 | |
| 1913 | LoadTimingInfo resulting_load_timing; |
| 1914 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 1915 | |
| 1916 | // None of these should be modified by the URLRequest. |
| 1917 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1918 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1919 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1920 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1921 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1922 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 1923 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 1924 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1925 | |
| 1926 | return resulting_load_timing; |
| 1927 | } |
| 1928 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1929 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1930 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1931 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1932 | LoadTimingInfo job_load_timing = |
| 1933 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1934 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1935 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1936 | RunURLRequestInterceptorLoadTimingTest( |
| 1937 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1938 | |
| 1939 | // Nothing should have been changed by the URLRequest. |
| 1940 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1941 | load_timing_result.proxy_resolve_start); |
| 1942 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1943 | load_timing_result.proxy_resolve_end); |
| 1944 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1945 | load_timing_result.connect_timing.dns_start); |
| 1946 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1947 | load_timing_result.connect_timing.dns_end); |
| 1948 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1949 | load_timing_result.connect_timing.connect_start); |
| 1950 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1951 | load_timing_result.connect_timing.connect_end); |
| 1952 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1953 | load_timing_result.connect_timing.ssl_start); |
| 1954 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1955 | load_timing_result.connect_timing.ssl_end); |
| 1956 | |
| 1957 | // Redundant sanity check. |
| 1958 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1959 | } |
| 1960 | |
| 1961 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1962 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1963 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1964 | LoadTimingInfo job_load_timing = |
| 1965 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1966 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1967 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1968 | RunURLRequestInterceptorLoadTimingTest( |
| 1969 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1970 | |
| 1971 | // Nothing should have been changed by the URLRequest. |
| 1972 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1973 | load_timing_result.proxy_resolve_start); |
| 1974 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1975 | load_timing_result.proxy_resolve_end); |
| 1976 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1977 | load_timing_result.connect_timing.dns_start); |
| 1978 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1979 | load_timing_result.connect_timing.dns_end); |
| 1980 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1981 | load_timing_result.connect_timing.connect_start); |
| 1982 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1983 | load_timing_result.connect_timing.connect_end); |
| 1984 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1985 | load_timing_result.connect_timing.ssl_start); |
| 1986 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1987 | load_timing_result.connect_timing.ssl_end); |
| 1988 | |
| 1989 | // Redundant sanity check. |
| 1990 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1991 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1992 | } |
| 1993 | |
| 1994 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1995 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1996 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1997 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1998 | // |
| 1999 | // 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] | 2000 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2001 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2002 | LoadTimingInfo job_load_timing = |
| 2003 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2004 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2005 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2006 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2007 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2008 | job_load_timing.connect_timing.connect_start = |
| 2009 | now - base::TimeDelta::FromDays(2); |
| 2010 | job_load_timing.connect_timing.connect_end = |
| 2011 | now - base::TimeDelta::FromDays(1); |
| 2012 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2013 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2014 | RunURLRequestInterceptorLoadTimingTest( |
| 2015 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2016 | |
| 2017 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2018 | // request_start. |
| 2019 | EXPECT_EQ(load_timing_result.request_start, |
| 2020 | load_timing_result.proxy_resolve_start); |
| 2021 | EXPECT_EQ(load_timing_result.request_start, |
| 2022 | load_timing_result.proxy_resolve_end); |
| 2023 | EXPECT_EQ(load_timing_result.request_start, |
| 2024 | load_timing_result.connect_timing.dns_start); |
| 2025 | EXPECT_EQ(load_timing_result.request_start, |
| 2026 | load_timing_result.connect_timing.dns_end); |
| 2027 | EXPECT_EQ(load_timing_result.request_start, |
| 2028 | load_timing_result.connect_timing.connect_start); |
| 2029 | EXPECT_EQ(load_timing_result.request_start, |
| 2030 | load_timing_result.connect_timing.connect_end); |
| 2031 | |
| 2032 | // Other times should have been left null. |
| 2033 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2034 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2035 | } |
| 2036 | |
| 2037 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2038 | TEST_F(URLRequestInterceptorTest, |
| 2039 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2040 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2041 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2042 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2043 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 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 | // Proxy times and connect times should all be replaced with request_start. |
| 2050 | EXPECT_EQ(load_timing_result.request_start, |
| 2051 | load_timing_result.proxy_resolve_start); |
| 2052 | EXPECT_EQ(load_timing_result.request_start, |
| 2053 | load_timing_result.proxy_resolve_end); |
| 2054 | |
| 2055 | // Other times should have been left null. |
| 2056 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2057 | } |
| 2058 | |
| 2059 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2060 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2061 | // not considered reused in this test (May be a preconnect). |
| 2062 | // |
| 2063 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2064 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2065 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2066 | LoadTimingInfo job_load_timing = |
| 2067 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2068 | job_load_timing.connect_timing.connect_start = |
| 2069 | now - base::TimeDelta::FromDays(1); |
| 2070 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2071 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2072 | job_load_timing.connect_timing.connect_end = |
| 2073 | now - base::TimeDelta::FromDays(4); |
| 2074 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2075 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2076 | RunURLRequestInterceptorLoadTimingTest( |
| 2077 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2078 | |
| 2079 | // Connect times, and SSL times should be replaced with request_start. |
| 2080 | EXPECT_EQ(load_timing_result.request_start, |
| 2081 | load_timing_result.connect_timing.connect_start); |
| 2082 | EXPECT_EQ(load_timing_result.request_start, |
| 2083 | load_timing_result.connect_timing.ssl_start); |
| 2084 | EXPECT_EQ(load_timing_result.request_start, |
| 2085 | load_timing_result.connect_timing.ssl_end); |
| 2086 | EXPECT_EQ(load_timing_result.request_start, |
| 2087 | load_timing_result.connect_timing.connect_end); |
| 2088 | |
| 2089 | // Other times should have been left null. |
| 2090 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2091 | } |
| 2092 | |
| 2093 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2094 | // |request_start|, due to reusing a connected socket in the case that there |
| 2095 | // are also proxy times. The connected socket is not considered reused in this |
| 2096 | // test (May be a preconnect). |
| 2097 | // |
| 2098 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2099 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2100 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2101 | LoadTimingInfo job_load_timing = |
| 2102 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2103 | job_load_timing.connect_timing.connect_start = |
| 2104 | now - base::TimeDelta::FromDays(1); |
| 2105 | job_load_timing.connect_timing.connect_end = |
| 2106 | now - base::TimeDelta::FromDays(2); |
| 2107 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2108 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2109 | RunURLRequestInterceptorLoadTimingTest( |
| 2110 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2111 | |
| 2112 | // Connect times should be replaced with proxy_resolve_end. |
| 2113 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2114 | load_timing_result.connect_timing.connect_start); |
| 2115 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2116 | load_timing_result.connect_timing.connect_end); |
| 2117 | |
| 2118 | // Other times should have been left null. |
| 2119 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2120 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2121 | } |
| 2122 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2123 | // Check that two different URL requests have different identifiers. |
| 2124 | TEST_F(URLRequestTest, Identifiers) { |
| 2125 | TestDelegate d; |
| 2126 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2127 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2128 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2129 | std::unique_ptr<URLRequest> other_req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2130 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2131 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2132 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2133 | } |
| 2134 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2135 | #if defined(OS_IOS) |
| 2136 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2137 | // the network delegate. crbug.com/496743 |
| 2138 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2139 | #else |
| 2140 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2141 | #endif |
| 2142 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2143 | MockHostResolver host_resolver; |
| 2144 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2145 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2146 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2147 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2148 | |
| 2149 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2150 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2151 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2152 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2153 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2154 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2155 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2156 | |
| 2157 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2158 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2159 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2160 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 2161 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2162 | EXPECT_THAT(req->status().error(), IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2163 | |
| 2164 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2165 | EXPECT_THAT(network_delegate.last_error(), |
| 2166 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2167 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2168 | } |
| 2169 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2170 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2171 | // content is empty. |
| 2172 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2173 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2174 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2175 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2176 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2177 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2178 | EXPECT_EQ("", d.data_received()); |
| 2179 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2180 | } |
| 2181 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2182 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2183 | // correctly, both before and after start. |
| 2184 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2185 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2186 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2187 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2188 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2189 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2190 | req->SetPriority(LOW); |
| 2191 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2192 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2193 | req->Start(); |
| 2194 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2195 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2196 | req->SetPriority(MEDIUM); |
| 2197 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2201 | // Start on it. |
| 2202 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2203 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2204 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2205 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2206 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2207 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2208 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2209 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2210 | req.get(), &default_network_delegate_, &job_priority)); |
| 2211 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2212 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2213 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2214 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2215 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2216 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2217 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | // Make sure that URLRequest passes on its priority updates to its |
| 2221 | // job. |
| 2222 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2223 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2224 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2225 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2226 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2227 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2228 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2229 | req.get(), &default_network_delegate_, &job_priority)); |
| 2230 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2231 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2232 | req->SetPriority(LOW); |
| 2233 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2234 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2235 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2236 | req->SetPriority(MEDIUM); |
| 2237 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2238 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2239 | } |
| 2240 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2241 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2242 | // is MAXIMUM_PRIORITY. |
| 2243 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2244 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2245 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2246 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2247 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2248 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2249 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2250 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2251 | req.get(), &default_network_delegate_, &job_priority)); |
| 2252 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2253 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2254 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2255 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2256 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2257 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2258 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2259 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2260 | req->Start(); |
| 2261 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2262 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2263 | } |
| 2264 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2265 | namespace { |
| 2266 | |
| 2267 | // Less verbose way of running a simple testserver for the tests below. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2268 | class LocalHttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2269 | public: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2270 | explicit LocalHttpTestServer(const base::FilePath& document_root) { |
| 2271 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2272 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2273 | |
| 2274 | LocalHttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
| 2275 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2276 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2277 | } // namespace |
| 2278 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2279 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2280 | LocalHttpTestServer test_server; |
| 2281 | ASSERT_TRUE(test_server.Start()); |
| 2282 | |
| 2283 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2284 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2285 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2286 | |
| 2287 | // Set up a cookie. |
| 2288 | { |
| 2289 | TestNetworkDelegate network_delegate; |
| 2290 | context.set_network_delegate(&network_delegate); |
| 2291 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2292 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2293 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2294 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2295 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2296 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2297 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2298 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2299 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2300 | } |
| 2301 | |
| 2302 | // Verify that the cookie is set. |
| 2303 | { |
| 2304 | TestNetworkDelegate network_delegate; |
| 2305 | context.set_network_delegate(&network_delegate); |
| 2306 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2307 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2308 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2309 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2310 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2311 | |
| 2312 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2313 | != std::string::npos); |
| 2314 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2315 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2316 | } |
| 2317 | } |
| 2318 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2319 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2320 | LocalHttpTestServer test_server; |
| 2321 | ASSERT_TRUE(test_server.Start()); |
| 2322 | |
| 2323 | // Set up a cookie. |
| 2324 | { |
| 2325 | TestNetworkDelegate network_delegate; |
| 2326 | default_context_.set_network_delegate(&network_delegate); |
| 2327 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2328 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2329 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2330 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2331 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2332 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2333 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2334 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2335 | } |
| 2336 | |
| 2337 | // Verify that the cookie is set. |
| 2338 | { |
| 2339 | TestNetworkDelegate network_delegate; |
| 2340 | default_context_.set_network_delegate(&network_delegate); |
| 2341 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2342 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2343 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2344 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2345 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2346 | |
| 2347 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2348 | != std::string::npos); |
| 2349 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2350 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2351 | } |
| 2352 | |
| 2353 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2354 | { |
| 2355 | TestNetworkDelegate network_delegate; |
| 2356 | default_context_.set_network_delegate(&network_delegate); |
| 2357 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2358 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2359 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2360 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2361 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2362 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2363 | |
| 2364 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2365 | == std::string::npos); |
| 2366 | |
| 2367 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2368 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2369 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2374 | LocalHttpTestServer test_server; |
| 2375 | ASSERT_TRUE(test_server.Start()); |
| 2376 | |
| 2377 | // Set up a cookie. |
| 2378 | { |
| 2379 | TestNetworkDelegate network_delegate; |
| 2380 | default_context_.set_network_delegate(&network_delegate); |
| 2381 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2382 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2383 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2384 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2385 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2386 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2387 | |
| 2388 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2389 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2390 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2391 | } |
| 2392 | |
| 2393 | // Try to set-up another cookie and update the previous cookie. |
| 2394 | { |
| 2395 | TestNetworkDelegate network_delegate; |
| 2396 | default_context_.set_network_delegate(&network_delegate); |
| 2397 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2398 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2399 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2400 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2401 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2402 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2403 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2404 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2405 | |
| 2406 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2407 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2408 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2409 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2410 | } |
| 2411 | |
| 2412 | // Verify the cookies weren't saved or updated. |
| 2413 | { |
| 2414 | TestNetworkDelegate network_delegate; |
| 2415 | default_context_.set_network_delegate(&network_delegate); |
| 2416 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2417 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2418 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2419 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2420 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2421 | |
| 2422 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2423 | == std::string::npos); |
| 2424 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2425 | != std::string::npos); |
| 2426 | |
| 2427 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2428 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2429 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2434 | LocalHttpTestServer test_server; |
| 2435 | ASSERT_TRUE(test_server.Start()); |
| 2436 | |
| 2437 | // Set up a cookie. |
| 2438 | { |
| 2439 | TestNetworkDelegate network_delegate; |
| 2440 | default_context_.set_network_delegate(&network_delegate); |
| 2441 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2442 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2443 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2444 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2446 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2447 | |
| 2448 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2449 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2450 | } |
| 2451 | |
| 2452 | // Verify that the cookie is set. |
| 2453 | { |
| 2454 | TestNetworkDelegate network_delegate; |
| 2455 | default_context_.set_network_delegate(&network_delegate); |
| 2456 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2457 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2458 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2459 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2460 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2461 | |
| 2462 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2463 | != std::string::npos); |
| 2464 | |
| 2465 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2466 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2467 | } |
| 2468 | |
| 2469 | // Verify that the cookie isn't sent. |
| 2470 | { |
| 2471 | TestNetworkDelegate network_delegate; |
| 2472 | default_context_.set_network_delegate(&network_delegate); |
| 2473 | TestDelegate d; |
| 2474 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2475 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2476 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2477 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2478 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2479 | |
| 2480 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2481 | == std::string::npos); |
| 2482 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2483 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2484 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2485 | } |
| 2486 | } |
| 2487 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2488 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2489 | #if defined(OS_IOS) |
| 2490 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2491 | #else |
| 2492 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2493 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2494 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2495 | LocalHttpTestServer test_server; |
| 2496 | ASSERT_TRUE(test_server.Start()); |
| 2497 | |
| 2498 | // Set up a cookie. |
| 2499 | { |
| 2500 | TestNetworkDelegate network_delegate; |
| 2501 | default_context_.set_network_delegate(&network_delegate); |
| 2502 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2503 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2504 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2505 | &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_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2510 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2511 | } |
| 2512 | |
| 2513 | // Try to set-up another cookie and update the previous cookie. |
| 2514 | { |
| 2515 | TestNetworkDelegate network_delegate; |
| 2516 | default_context_.set_network_delegate(&network_delegate); |
| 2517 | TestDelegate d; |
| 2518 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2519 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2520 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2521 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2522 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2523 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2524 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2525 | |
| 2526 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2527 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2528 | } |
| 2529 | |
| 2530 | // Verify the cookies weren't saved or updated. |
| 2531 | { |
| 2532 | TestNetworkDelegate network_delegate; |
| 2533 | default_context_.set_network_delegate(&network_delegate); |
| 2534 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2535 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2536 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2537 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2538 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2539 | |
| 2540 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2541 | == std::string::npos); |
| 2542 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2543 | != std::string::npos); |
| 2544 | |
| 2545 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2546 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2551 | LocalHttpTestServer test_server; |
| 2552 | ASSERT_TRUE(test_server.Start()); |
| 2553 | |
| 2554 | // Set up an empty cookie. |
| 2555 | { |
| 2556 | TestNetworkDelegate network_delegate; |
| 2557 | default_context_.set_network_delegate(&network_delegate); |
| 2558 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2559 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2560 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2561 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2562 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2563 | |
| 2564 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2565 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2566 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2571 | LocalHttpTestServer test_server; |
| 2572 | ASSERT_TRUE(test_server.Start()); |
| 2573 | |
| 2574 | // Set up a cookie. |
| 2575 | { |
| 2576 | TestNetworkDelegate network_delegate; |
| 2577 | default_context_.set_network_delegate(&network_delegate); |
| 2578 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2579 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2580 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2581 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2582 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2583 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2584 | |
| 2585 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2586 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2587 | } |
| 2588 | |
| 2589 | // Verify that the cookie is set. |
| 2590 | { |
| 2591 | TestNetworkDelegate network_delegate; |
| 2592 | default_context_.set_network_delegate(&network_delegate); |
| 2593 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2594 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2595 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2596 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2597 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2598 | |
| 2599 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2600 | != std::string::npos); |
| 2601 | |
| 2602 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2603 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2604 | } |
| 2605 | |
| 2606 | // Verify that the cookie isn't sent. |
| 2607 | { |
| 2608 | TestNetworkDelegate network_delegate; |
| 2609 | default_context_.set_network_delegate(&network_delegate); |
| 2610 | TestDelegate d; |
| 2611 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2612 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2613 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2614 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2615 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2616 | |
| 2617 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2618 | == std::string::npos); |
| 2619 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2620 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2621 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2626 | LocalHttpTestServer test_server; |
| 2627 | ASSERT_TRUE(test_server.Start()); |
| 2628 | |
| 2629 | // Set up a cookie. |
| 2630 | { |
| 2631 | TestNetworkDelegate network_delegate; |
| 2632 | default_context_.set_network_delegate(&network_delegate); |
| 2633 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2634 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2635 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2636 | &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_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2641 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2642 | } |
| 2643 | |
| 2644 | // Try to set-up another cookie and update the previous cookie. |
| 2645 | { |
| 2646 | TestNetworkDelegate network_delegate; |
| 2647 | default_context_.set_network_delegate(&network_delegate); |
| 2648 | TestDelegate d; |
| 2649 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2650 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2651 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2652 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2653 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2654 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2655 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2656 | |
| 2657 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2658 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2659 | } |
| 2660 | |
| 2661 | // Verify the cookies weren't saved or updated. |
| 2662 | { |
| 2663 | TestNetworkDelegate network_delegate; |
| 2664 | default_context_.set_network_delegate(&network_delegate); |
| 2665 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2666 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2667 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2668 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2669 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2670 | |
| 2671 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2672 | == std::string::npos); |
| 2673 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2674 | != std::string::npos); |
| 2675 | |
| 2676 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2677 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2678 | } |
| 2679 | } |
| 2680 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2681 | TEST_F(URLRequestTest, SameSiteCookies) { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2682 | LocalHttpTestServer test_server; |
| 2683 | ASSERT_TRUE(test_server.Start()); |
| 2684 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2685 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2686 | default_context_.set_network_delegate(&network_delegate); |
| 2687 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2688 | const std::string kHost = "example.test"; |
| 2689 | const std::string kSubHost = "subdomain.example.test"; |
| 2690 | const std::string kCrossHost = "cross-origin.test"; |
| 2691 | |
| 2692 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2693 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2694 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2695 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2696 | test_server.GetURL(kHost, |
| 2697 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2698 | "LaxSameSiteCookie=1;SameSite=Lax"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2699 | DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2700 | req->Start(); |
| 2701 | base::RunLoop().Run(); |
| 2702 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2703 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2704 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2705 | } |
| 2706 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2707 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2708 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2709 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2710 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2711 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2712 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2713 | req->set_initiator(url::Origin(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2714 | req->Start(); |
| 2715 | base::RunLoop().Run(); |
| 2716 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2717 | EXPECT_NE(std::string::npos, |
| 2718 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2719 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2720 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2721 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2722 | } |
| 2723 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2724 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2725 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2726 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2727 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2728 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2729 | req->set_first_party_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 2730 | req->set_initiator(url::Origin(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2731 | req->Start(); |
| 2732 | base::RunLoop().Run(); |
| 2733 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2734 | EXPECT_NE(std::string::npos, |
| 2735 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2736 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2737 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2738 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2739 | } |
| 2740 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2741 | // Verify that neither cookie is not sent for cross-site requests. |
| 2742 | { |
| 2743 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2744 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2745 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2746 | req->set_first_party_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 2747 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 2748 | req->Start(); |
| 2749 | base::RunLoop().Run(); |
| 2750 | |
| 2751 | EXPECT_EQ(std::string::npos, |
| 2752 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2753 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 2754 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2755 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2756 | } |
| 2757 | |
| 2758 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2759 | // method is "safe". |
| 2760 | { |
| 2761 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2762 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2763 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2764 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2765 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
| 2766 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2767 | req->Start(); |
| 2768 | base::RunLoop().Run(); |
| 2769 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2770 | EXPECT_EQ(std::string::npos, |
| 2771 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2772 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2773 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2774 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2775 | } |
| 2776 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2777 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2778 | // method is unsafe (e.g. POST). |
| 2779 | { |
| 2780 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2781 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2782 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2783 | req->set_first_party_for_cookies(test_server.GetURL(kHost, "/")); |
| 2784 | req->set_initiator(url::Origin(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2785 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2786 | req->Start(); |
| 2787 | base::RunLoop().Run(); |
| 2788 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2789 | EXPECT_EQ(std::string::npos, |
| 2790 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2791 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2792 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2793 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2794 | } |
| 2795 | } |
| 2796 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2797 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2798 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2799 | EmbeddedTestServer http_server; |
| 2800 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2801 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2802 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2803 | https_server.AddDefaultHandlers( |
| 2804 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2805 | ASSERT_TRUE(http_server.Start()); |
| 2806 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2807 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2808 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2809 | TestURLRequestContext context(true); |
| 2810 | context.set_network_delegate(&network_delegate); |
| 2811 | context.Init(); |
| 2812 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2813 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2814 | { |
| 2815 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2816 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2817 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2818 | DEFAULT_PRIORITY, &d)); |
| 2819 | req->Start(); |
| 2820 | base::RunLoop().Run(); |
| 2821 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2822 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2823 | } |
| 2824 | |
| 2825 | // Verify that the cookie is not set. |
| 2826 | { |
| 2827 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2828 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2829 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2830 | req->Start(); |
| 2831 | base::RunLoop().Run(); |
| 2832 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2833 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 2834 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2835 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2836 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2837 | } |
| 2838 | } |
| 2839 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2840 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2841 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2842 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2843 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2844 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2845 | |
| 2846 | TestNetworkDelegate network_delegate; |
| 2847 | TestURLRequestContext context(true); |
| 2848 | context.set_network_delegate(&network_delegate); |
| 2849 | context.Init(); |
| 2850 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2851 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2852 | { |
| 2853 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2854 | std::unique_ptr<URLRequest> req( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2855 | context.CreateRequest(https_server.GetURL("/set-cookie?__Secure-foo=1"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2856 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2857 | req->Start(); |
| 2858 | base::RunLoop().Run(); |
| 2859 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2860 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2861 | } |
| 2862 | |
| 2863 | // Verify that the cookie is not set. |
| 2864 | { |
| 2865 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2866 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2867 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2868 | req->Start(); |
| 2869 | base::RunLoop().Run(); |
| 2870 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2871 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2872 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2873 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2874 | } |
| 2875 | } |
| 2876 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2877 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2878 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2879 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2880 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2881 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2882 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2883 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2884 | TestURLRequestContext context(true); |
| 2885 | context.set_network_delegate(&network_delegate); |
| 2886 | context.Init(); |
| 2887 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 2888 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2889 | { |
| 2890 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2891 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2892 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2893 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2894 | req->Start(); |
| 2895 | base::RunLoop().Run(); |
| 2896 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2897 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2898 | } |
| 2899 | |
| 2900 | // Verify that the cookie is set. |
| 2901 | { |
| 2902 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2903 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2904 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2905 | req->Start(); |
| 2906 | base::RunLoop().Run(); |
| 2907 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2908 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 2909 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2910 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 2915 | // cookies are enabled. |
| 2916 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 2917 | EmbeddedTestServer http_server; |
| 2918 | http_server.AddDefaultHandlers( |
| 2919 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2920 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2921 | https_server.AddDefaultHandlers( |
| 2922 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2923 | ASSERT_TRUE(http_server.Start()); |
| 2924 | ASSERT_TRUE(https_server.Start()); |
| 2925 | |
| 2926 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2927 | TestURLRequestContext context(true); |
| 2928 | context.set_network_delegate(&network_delegate); |
| 2929 | context.Init(); |
| 2930 | |
| 2931 | // Try to set a Secure cookie, with experimental features enabled. |
| 2932 | { |
| 2933 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2934 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2935 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
| 2936 | DEFAULT_PRIORITY, &d)); |
| 2937 | req->Start(); |
| 2938 | base::RunLoop().Run(); |
| 2939 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2940 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2941 | } |
| 2942 | |
| 2943 | // Verify that the cookie is not set. |
| 2944 | { |
| 2945 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2946 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2947 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2948 | req->Start(); |
| 2949 | base::RunLoop().Run(); |
| 2950 | |
| 2951 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 2952 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2953 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 2958 | // cookies are enabled. |
| 2959 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 2960 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2961 | https_server.AddDefaultHandlers( |
| 2962 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2963 | ASSERT_TRUE(https_server.Start()); |
| 2964 | |
| 2965 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2966 | TestURLRequestContext context(true); |
| 2967 | context.set_network_delegate(&network_delegate); |
| 2968 | context.Init(); |
| 2969 | |
| 2970 | // Try to set a Secure cookie, with experimental features enabled. |
| 2971 | { |
| 2972 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2973 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2974 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
| 2975 | DEFAULT_PRIORITY, &d)); |
| 2976 | req->Start(); |
| 2977 | base::RunLoop().Run(); |
| 2978 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2979 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2980 | } |
| 2981 | |
| 2982 | // Verify that the cookie is not set. |
| 2983 | { |
| 2984 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2985 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2986 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2987 | req->Start(); |
| 2988 | base::RunLoop().Run(); |
| 2989 | |
| 2990 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2991 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2992 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2993 | } |
| 2994 | } |
| 2995 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 2996 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 2997 | // rather than a spawned test server because the connection used to talk to |
| 2998 | // the test server is affected by entering suspend mode on Android. |
| 2999 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3000 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3001 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3002 | |
| 3003 | URLRequestFailedJob::AddUrlHandler(); |
| 3004 | |
| 3005 | TestDelegate d; |
| 3006 | // Request that just hangs. |
| 3007 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3008 | std::unique_ptr<URLRequest> r( |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3009 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| 3010 | r->Start(); |
| 3011 | |
| 3012 | power_monitor_source->Suspend(); |
| 3013 | // Wait for the suspend notification to cause the request to fail. |
| 3014 | base::RunLoop().Run(); |
| 3015 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3016 | EXPECT_TRUE(d.request_failed()); |
| 3017 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3018 | |
| 3019 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3020 | |
| 3021 | // Shouldn't be needed, but just in case. |
| 3022 | power_monitor_source->Resume(); |
| 3023 | } |
| 3024 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3025 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3026 | // value for the |fixed_date| argument given to the constructor. |
| 3027 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3028 | public: |
| 3029 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3030 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3031 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3032 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3033 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3034 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3035 | URLRequest* request, |
| 3036 | const CompletionCallback& callback, |
| 3037 | const HttpResponseHeaders* original_response_headers, |
| 3038 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3039 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3040 | |
| 3041 | private: |
| 3042 | std::string fixed_date_; |
| 3043 | |
| 3044 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3045 | }; |
| 3046 | |
| 3047 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3048 | URLRequest* request, |
| 3049 | const CompletionCallback& callback, |
| 3050 | const HttpResponseHeaders* original_response_headers, |
| 3051 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3052 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3053 | HttpResponseHeaders* new_response_headers = |
| 3054 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3055 | |
| 3056 | new_response_headers->RemoveHeader("Date"); |
| 3057 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3058 | |
| 3059 | *override_response_headers = new_response_headers; |
| 3060 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3061 | callback, |
| 3062 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3063 | override_response_headers, |
| 3064 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | // Test that cookie expiration times are adjusted for server/client clock |
| 3068 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3069 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3070 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 3071 | LocalHttpTestServer test_server; |
| 3072 | ASSERT_TRUE(test_server.Start()); |
| 3073 | |
| 3074 | // Set up an expired cookie. |
| 3075 | { |
| 3076 | TestNetworkDelegate network_delegate; |
| 3077 | default_context_.set_network_delegate(&network_delegate); |
| 3078 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3079 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3080 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3081 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3082 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3083 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3084 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3085 | } |
| 3086 | // Verify that the cookie is not set. |
| 3087 | { |
| 3088 | TestNetworkDelegate network_delegate; |
| 3089 | default_context_.set_network_delegate(&network_delegate); |
| 3090 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3091 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3092 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3093 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3094 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3095 | |
| 3096 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3097 | } |
| 3098 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3099 | { |
| 3100 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3101 | default_context_.set_network_delegate(&network_delegate); |
| 3102 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3103 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3104 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3105 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3106 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3107 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3108 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3109 | } |
| 3110 | // Verify that the cookie is set. |
| 3111 | { |
| 3112 | TestNetworkDelegate network_delegate; |
| 3113 | default_context_.set_network_delegate(&network_delegate); |
| 3114 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3115 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3116 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3117 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3118 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3119 | |
| 3120 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3125 | // Check that it is impossible to change the referrer in the extra headers of |
| 3126 | // an URLRequest. |
| 3127 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 3128 | LocalHttpTestServer test_server; |
| 3129 | ASSERT_TRUE(test_server.Start()); |
| 3130 | |
| 3131 | // If extra headers contain referer and the request contains a referer, |
| 3132 | // only the latter shall be respected. |
| 3133 | { |
| 3134 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3135 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3136 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3137 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3138 | |
| 3139 | HttpRequestHeaders headers; |
| 3140 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3141 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3142 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3143 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3144 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3145 | |
| 3146 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3147 | } |
| 3148 | |
| 3149 | // If extra headers contain a referer but the request does not, no referer |
| 3150 | // shall be sent in the header. |
| 3151 | { |
| 3152 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3153 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3154 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3155 | |
| 3156 | HttpRequestHeaders headers; |
| 3157 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3158 | req->SetExtraRequestHeaders(headers); |
| 3159 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3160 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3161 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3162 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3163 | |
| 3164 | EXPECT_EQ("None", d.data_received()); |
| 3165 | } |
| 3166 | } |
| 3167 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3168 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3169 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3170 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3171 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3172 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3173 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3174 | // |request_method| is the method to use for the initial request. |
| 3175 | // |redirect_method| is the method that is expected to be used for the second |
| 3176 | // request, after redirection. |
| 3177 | // If |include_data| is true, data is uploaded with the request. The |
| 3178 | // response body is expected to match it exactly, if and only if |
| 3179 | // |request_method| == |redirect_method|. |
| 3180 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3181 | const std::string& request_method, |
| 3182 | const std::string& redirect_method, |
| 3183 | bool include_data) { |
| 3184 | static const char kData[] = "hello world"; |
| 3185 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3186 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3187 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3188 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3189 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3190 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3191 | HttpRequestHeaders headers; |
| 3192 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3193 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3194 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3195 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3196 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3197 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3198 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3199 | EXPECT_EQ(redirect_method, req->method()); |
| 3200 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3201 | EXPECT_THAT(req->status().error(), IsOk()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3202 | if (include_data) { |
| 3203 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3204 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3205 | HttpRequestHeaders::kContentLength)); |
| 3206 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3207 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3208 | EXPECT_EQ(kData, d.data_received()); |
| 3209 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3210 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3211 | HttpRequestHeaders::kContentLength)); |
| 3212 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3213 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3214 | EXPECT_NE(kData, d.data_received()); |
| 3215 | } |
| 3216 | } |
| 3217 | if (HasFailure()) |
| 3218 | LOG(WARNING) << "Request method was: " << request_method; |
| 3219 | } |
| 3220 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3221 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3222 | // |request_method| is the method to use for the initial request. |
| 3223 | // |redirect_method| is the method that is expected to be used for the second |
| 3224 | // request, after redirection. |
| 3225 | // |origin_value| is the expected value for the Origin header after |
| 3226 | // redirection. If empty, expects that there will be no Origin header. |
| 3227 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3228 | const std::string& request_method, |
| 3229 | const std::string& redirect_method, |
| 3230 | const std::string& origin_value) { |
| 3231 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3232 | std::unique_ptr<URLRequest> req( |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3233 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3234 | req->set_method(request_method); |
| 3235 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3236 | redirect_url.GetOrigin().spec(), false); |
| 3237 | req->Start(); |
| 3238 | |
| 3239 | base::RunLoop().Run(); |
| 3240 | |
| 3241 | EXPECT_EQ(redirect_method, req->method()); |
| 3242 | // Note that there is no check for request success here because, for |
| 3243 | // purposes of testing, the request very well may fail. For example, if the |
| 3244 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3245 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3246 | // request would fail. However, that's fine, as long as the request headers |
| 3247 | // are in order and pass the checks below. |
| 3248 | if (origin_value.empty()) { |
| 3249 | EXPECT_FALSE( |
| 3250 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3251 | } else { |
| 3252 | std::string origin_header; |
| 3253 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3254 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 3255 | EXPECT_EQ(origin_value, origin_header); |
| 3256 | } |
| 3257 | } |
| 3258 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3259 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3260 | const int kMsgSize = 20000; // multiple of 10 |
| 3261 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3262 | char* uploadBytes = new char[kMsgSize+1]; |
| 3263 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3264 | char marker = 'a'; |
| 3265 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3266 | memcpy(ptr, "----------", 10); |
| 3267 | ptr += 10; |
| 3268 | if (idx % 100 == 0) { |
| 3269 | ptr--; |
| 3270 | *ptr++ = marker; |
| 3271 | if (++marker > 'z') |
| 3272 | marker = 'a'; |
| 3273 | } |
| 3274 | } |
| 3275 | uploadBytes[kMsgSize] = '\0'; |
| 3276 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3277 | for (int i = 0; i < kIterations; ++i) { |
| 3278 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3279 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3280 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3281 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3282 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3283 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3284 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3285 | r->Start(); |
| 3286 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3287 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3288 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3289 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3290 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3291 | << "request failed: " << r->status().status() |
| 3292 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3293 | |
| 3294 | EXPECT_FALSE(d.received_data_before_response()); |
| 3295 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3296 | } |
| 3297 | delete[] uploadBytes; |
| 3298 | } |
| 3299 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3300 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3301 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3302 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3303 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3304 | base::IntToString(num_cookies)), |
| 3305 | DEFAULT_PRIORITY, &d)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3306 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3307 | r->Start(); |
| 3308 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3309 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3310 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3311 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3312 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3313 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3314 | if (!is_success) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3315 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3316 | |
| 3317 | return is_success; |
| 3318 | } |
| 3319 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3320 | LocalHttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3321 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3322 | private: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3323 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3324 | }; |
| 3325 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3326 | namespace { |
| 3327 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3328 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3329 | const test_server::HttpRequest& request) { |
| 3330 | if (request.headers.find("Host") == request.headers.end() || |
| 3331 | request.headers.at("Host") != "www.redirect.com" || |
| 3332 | request.method != test_server::METHOD_CONNECT) { |
| 3333 | return nullptr; |
| 3334 | } |
| 3335 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3336 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3337 | new test_server::BasicHttpResponse); |
| 3338 | http_response->set_code(HTTP_FOUND); |
| 3339 | http_response->AddCustomHeader("Location", |
| 3340 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 3341 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | } // namespace |
| 3345 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3346 | class TestSSLConfigService : public SSLConfigService { |
| 3347 | public: |
| 3348 | TestSSLConfigService(bool ev_enabled, |
| 3349 | bool online_rev_checking, |
| 3350 | bool rev_checking_required_local_anchors, |
| 3351 | bool token_binding_enabled) |
| 3352 | : ev_enabled_(ev_enabled), |
| 3353 | online_rev_checking_(online_rev_checking), |
| 3354 | rev_checking_required_local_anchors_( |
| 3355 | rev_checking_required_local_anchors), |
| 3356 | token_binding_enabled_(token_binding_enabled), |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 3357 | min_version_(kDefaultSSLVersionMin) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3358 | |
| 3359 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 3360 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3361 | // SSLConfigService: |
| 3362 | void GetSSLConfig(SSLConfig* config) override { |
| 3363 | *config = SSLConfig(); |
| 3364 | config->rev_checking_enabled = online_rev_checking_; |
| 3365 | config->verify_ev_cert = ev_enabled_; |
| 3366 | config->rev_checking_required_local_anchors = |
| 3367 | rev_checking_required_local_anchors_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3368 | if (min_version_) { |
| 3369 | config->version_min = min_version_; |
| 3370 | } |
| 3371 | if (token_binding_enabled_) { |
| 3372 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | protected: |
| 3377 | ~TestSSLConfigService() override {} |
| 3378 | |
| 3379 | private: |
| 3380 | const bool ev_enabled_; |
| 3381 | const bool online_rev_checking_; |
| 3382 | const bool rev_checking_required_local_anchors_; |
| 3383 | const bool token_binding_enabled_; |
| 3384 | uint16_t min_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3385 | }; |
| 3386 | |
| 3387 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 3388 | #if !defined(OS_IOS) |
| 3389 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 3390 | public: |
| 3391 | void SetUp() override { |
| 3392 | default_context_.set_ssl_config_service( |
| 3393 | new TestSSLConfigService(false, false, false, true)); |
| 3394 | channel_id_service_.reset(new ChannelIDService( |
| 3395 | new DefaultChannelIDStore(NULL), base::ThreadTaskRunnerHandle::Get())); |
| 3396 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 3397 | URLRequestTestHTTP::SetUp(); |
| 3398 | } |
| 3399 | |
| 3400 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3401 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3402 | }; |
| 3403 | |
| 3404 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 3405 | SpawnedTestServer::SSLOptions ssl_options; |
| 3406 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3407 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3408 | ssl_options, |
| 3409 | base::FilePath(kTestFilePath)); |
| 3410 | ASSERT_TRUE(https_test_server.Start()); |
| 3411 | |
| 3412 | TestDelegate d; |
| 3413 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3414 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3415 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d)); |
| 3416 | r->Start(); |
| 3417 | EXPECT_TRUE(r->is_pending()); |
| 3418 | |
| 3419 | base::RunLoop().Run(); |
| 3420 | |
| 3421 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3422 | |
| 3423 | HttpRequestHeaders headers; |
| 3424 | std::string token_binding_header, token_binding_message; |
| 3425 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3426 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3427 | &token_binding_header)); |
| 3428 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3429 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3430 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3431 | std::vector<TokenBinding> token_bindings; |
| 3432 | ASSERT_TRUE( |
| 3433 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3434 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3435 | |
| 3436 | EXPECT_GT(d.bytes_received(), 0); |
| 3437 | std::string ekm = d.data_received(); |
| 3438 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3439 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3440 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3441 | token_bindings[0].signature, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3442 | } |
| 3443 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3444 | |
| 3445 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 3446 | SpawnedTestServer::SSLOptions ssl_options; |
| 3447 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3448 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3449 | ssl_options, |
| 3450 | base::FilePath(kTestFilePath)); |
| 3451 | ASSERT_TRUE(https_test_server.Start()); |
| 3452 | |
| 3453 | TestDelegate d; |
| 3454 | { |
| 3455 | GURL redirect_url = |
| 3456 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3457 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3458 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3459 | r->Start(); |
| 3460 | EXPECT_TRUE(r->is_pending()); |
| 3461 | |
| 3462 | base::RunLoop().Run(); |
| 3463 | |
| 3464 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3465 | |
| 3466 | HttpRequestHeaders headers; |
| 3467 | std::string token_binding_header, token_binding_message; |
| 3468 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3469 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3470 | &token_binding_header)); |
| 3471 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3472 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3473 | &token_binding_message)); |
| 3474 | std::vector<TokenBinding> token_bindings; |
| 3475 | ASSERT_TRUE( |
| 3476 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3477 | ASSERT_EQ(2ull, token_bindings.size()); |
| 3478 | |
| 3479 | EXPECT_GT(d.bytes_received(), 0); |
| 3480 | std::string ekm = d.data_received(); |
| 3481 | |
| 3482 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3483 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3484 | token_bindings[0].signature, ekm)); |
| 3485 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
| 3486 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[1].ec_point, |
| 3487 | token_bindings[1].signature, ekm)); |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 3492 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 3493 | #if !defined(OS_ANDROID) |
| 3494 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
| 3495 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, |
| 3496 | SpawnedTestServer::kLocalhost, |
| 3497 | base::FilePath()); |
| 3498 | ASSERT_TRUE(http_server.Start()); |
| 3499 | SpawnedTestServer::SSLOptions ssl_options; |
| 3500 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3501 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3502 | ssl_options, |
| 3503 | base::FilePath(kTestFilePath)); |
| 3504 | ASSERT_TRUE(https_test_server.Start()); |
| 3505 | |
| 3506 | TestDelegate d; |
| 3507 | { |
| 3508 | GURL redirect_url = http_server.GetURL( |
| 3509 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3510 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3511 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3512 | r->Start(); |
| 3513 | EXPECT_TRUE(r->is_pending()); |
| 3514 | |
| 3515 | base::RunLoop().Run(); |
| 3516 | |
| 3517 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3518 | |
| 3519 | HttpRequestHeaders headers; |
| 3520 | std::string token_binding_header, token_binding_message; |
| 3521 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3522 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3523 | &token_binding_header)); |
| 3524 | EXPECT_TRUE(base::Base64UrlDecode( |
| 3525 | token_binding_header, base::Base64UrlDecodePolicy::REQUIRE_PADDING, |
| 3526 | &token_binding_message)); |
| 3527 | std::vector<TokenBinding> token_bindings; |
| 3528 | ASSERT_TRUE( |
| 3529 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 3530 | ASSERT_EQ(1ull, token_bindings.size()); |
| 3531 | |
| 3532 | EXPECT_GT(d.bytes_received(), 0); |
| 3533 | std::string ekm = d.data_received(); |
| 3534 | |
| 3535 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
| 3536 | EXPECT_TRUE(VerifyEKMSignature(token_bindings[0].ec_point, |
| 3537 | token_bindings[0].signature, ekm)); |
| 3538 | } |
| 3539 | } |
| 3540 | |
| 3541 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 3542 | // Include-Referred-Token-Binding-ID) to an https url on a server that does not |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3543 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 3544 | // the redirect. |
| 3545 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 3546 | SpawnedTestServer::SSLOptions ssl_options; |
| 3547 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3548 | ssl_options, |
| 3549 | base::FilePath(kTestFilePath)); |
| 3550 | ASSERT_TRUE(https_test_server.Start()); |
| 3551 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 3552 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 3553 | ssl_options, |
| 3554 | base::FilePath(kTestFilePath)); |
| 3555 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 3556 | |
| 3557 | TestDelegate d; |
| 3558 | { |
| 3559 | GURL redirect_url = token_binding_test_server.GetURL( |
| 3560 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3561 | std::unique_ptr<URLRequest> r( |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 3562 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3563 | r->Start(); |
| 3564 | EXPECT_TRUE(r->is_pending()); |
| 3565 | |
| 3566 | base::RunLoop().Run(); |
| 3567 | |
| 3568 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3569 | |
| 3570 | HttpRequestHeaders headers; |
| 3571 | std::string token_binding_header, token_binding_message; |
| 3572 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 3573 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 3574 | &token_binding_header)); |
| 3575 | } |
| 3576 | } |
| 3577 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 3578 | #endif // !defined(OS_IOS) |
| 3579 | |
| 3580 | // 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] | 3581 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3582 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3583 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3584 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3585 | http_test_server()->RegisterRequestHandler( |
| 3586 | base::Bind(&HandleRedirectConnect)); |
| 3587 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3588 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3589 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3590 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3591 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3592 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3593 | TestDelegate d; |
| 3594 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3595 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3596 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3597 | r->Start(); |
| 3598 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3599 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3600 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3601 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3602 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3603 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3604 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3605 | EXPECT_THAT(r->status().error(), IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3606 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3607 | // We should not have followed the redirect. |
| 3608 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3609 | } |
| 3610 | } |
| 3611 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3612 | // This is the same as the previous test, but checks that the network delegate |
| 3613 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3614 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3615 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3616 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3617 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3618 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3619 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3620 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3621 | TestDelegate d; |
| 3622 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3623 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3624 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3625 | r->Start(); |
| 3626 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3627 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3628 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3629 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3630 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3631 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3632 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3633 | EXPECT_THAT(r->status().error(), IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3634 | EXPECT_EQ(1, d.response_started_count()); |
| 3635 | // We should not have followed the redirect. |
| 3636 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3637 | |
| 3638 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3639 | EXPECT_THAT(network_delegate.last_error(), |
| 3640 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3641 | } |
| 3642 | } |
| 3643 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3644 | // Tests that we can block and asynchronously return OK in various stages. |
| 3645 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 3646 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 3647 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3648 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3649 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 3650 | }; |
| 3651 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 3652 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3653 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3654 | |
| 3655 | TestDelegate d; |
| 3656 | BlockingNetworkDelegate network_delegate( |
| 3657 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3658 | network_delegate.set_block_on( |
| 3659 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 3660 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 3661 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3662 | |
| 3663 | TestURLRequestContext context(true); |
| 3664 | context.set_network_delegate(&network_delegate); |
| 3665 | context.Init(); |
| 3666 | |
| 3667 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3668 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3669 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3671 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3672 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3673 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3674 | EXPECT_EQ(blocking_stages[i], |
| 3675 | network_delegate.stage_blocked_for_callback()); |
| 3676 | network_delegate.DoCallback(OK); |
| 3677 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3678 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3679 | EXPECT_EQ(200, r->GetResponseCode()); |
| 3680 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3681 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3682 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3683 | } |
| 3684 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3685 | } |
| 3686 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3687 | // Tests that the network delegate can block and cancel a request. |
| 3688 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3689 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3690 | |
| 3691 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3692 | BlockingNetworkDelegate network_delegate( |
| 3693 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3694 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 3695 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3696 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3697 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3698 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3699 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3700 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3701 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3702 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3703 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3704 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3705 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3706 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3707 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3708 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3709 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3710 | EXPECT_THAT(r->status().error(), IsError(ERR_EMPTY_RESPONSE)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3711 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3712 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3713 | } |
| 3714 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3715 | } |
| 3716 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3717 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 3718 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 3719 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 3720 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 3721 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 3722 | BlockingNetworkDelegate::Stage stage, |
| 3723 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3724 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3725 | BlockingNetworkDelegate network_delegate(block_mode); |
| 3726 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 3727 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3728 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3729 | TestURLRequestContext context(true); |
| 3730 | context.set_network_delegate(&network_delegate); |
| 3731 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3732 | |
| 3733 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3734 | std::unique_ptr<URLRequest> r( |
| 3735 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3737 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3738 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3739 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3740 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3741 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3742 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3743 | EXPECT_THAT(r->status().error(), IsError(ERR_BLOCKED_BY_CLIENT)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3744 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3745 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3746 | } |
| 3747 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3748 | } |
| 3749 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3750 | // The following 3 tests check that the network delegate can cancel a request |
| 3751 | // synchronously in various stages of the request. |
| 3752 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
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::SYNCHRONOUS, |
| 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, NetworkDelegateCancelRequestSynchronously2) { |
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::SYNCHRONOUS, |
| 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, NetworkDelegateCancelRequestSynchronously3) { |
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::SYNCHRONOUS, |
| 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 | |
| 3773 | // The following 3 tests check that the network delegate can cancel a request |
| 3774 | // asynchronously in various stages of the request. |
| 3775 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3776 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3777 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3778 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3779 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3783 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3784 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3785 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3786 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3787 | } |
| 3788 | |
| 3789 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3790 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3791 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3792 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3793 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3794 | } |
| 3795 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3796 | // Tests that the network delegate can block and redirect a request to a new |
| 3797 | // URL. |
| 3798 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3799 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3800 | |
| 3801 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3802 | BlockingNetworkDelegate network_delegate( |
| 3803 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3804 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3805 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3806 | network_delegate.set_redirect_url(redirect_url); |
| 3807 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3808 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3809 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3810 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3811 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3812 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3813 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3814 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3815 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3816 | // Quit after hitting the redirect, so can check the headers. |
| 3817 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3818 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3819 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3820 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3821 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3822 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3823 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3824 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3825 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3826 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3827 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3828 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3829 | |
| 3830 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3831 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3832 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3833 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3834 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3835 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3836 | // through an untunneled proxy. |
| 3837 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3838 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3839 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3840 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3841 | EXPECT_EQ(0, r->status().error()); |
| 3842 | EXPECT_EQ(redirect_url, r->url()); |
| 3843 | EXPECT_EQ(original_url, r->original_url()); |
| 3844 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3845 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3846 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3847 | } |
| 3848 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3849 | } |
| 3850 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3851 | // Tests that the network delegate can block and redirect a request to a new |
| 3852 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 3853 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3854 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3855 | |
| 3856 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3857 | BlockingNetworkDelegate network_delegate( |
| 3858 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3859 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3860 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3861 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3862 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3863 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3864 | |
| 3865 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3866 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3867 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3868 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3869 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3870 | // Quit after hitting the redirect, so can check the headers. |
| 3871 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3872 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3873 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3874 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3875 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3876 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3877 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3878 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3879 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3880 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3881 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3882 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3883 | |
| 3884 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3885 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3886 | base::RunLoop().Run(); |
| 3887 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3888 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3889 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3890 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3891 | // through an untunneled proxy. |
| 3892 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3893 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3894 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3895 | EXPECT_EQ(0, r->status().error()); |
| 3896 | EXPECT_EQ(redirect_url, r->url()); |
| 3897 | EXPECT_EQ(original_url, r->original_url()); |
| 3898 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3899 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3900 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3901 | } |
| 3902 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3903 | } |
| 3904 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3905 | // Tests that redirects caused by the network delegate preserve POST data. |
| 3906 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3907 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3908 | |
| 3909 | const char kData[] = "hello world"; |
| 3910 | |
| 3911 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3912 | BlockingNetworkDelegate network_delegate( |
| 3913 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3914 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3915 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3916 | network_delegate.set_redirect_url(redirect_url); |
| 3917 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3918 | TestURLRequestContext context(true); |
| 3919 | context.set_network_delegate(&network_delegate); |
| 3920 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3921 | |
| 3922 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3923 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3924 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3925 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3926 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3927 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3928 | HttpRequestHeaders headers; |
| 3929 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3930 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3931 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3932 | |
| 3933 | // Quit after hitting the redirect, so can check the headers. |
| 3934 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3935 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3936 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3937 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3938 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3939 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3940 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3941 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3942 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3943 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3944 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3945 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3946 | |
| 3947 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3948 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3949 | base::RunLoop().Run(); |
| 3950 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3951 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3952 | EXPECT_EQ(0, r->status().error()); |
| 3953 | EXPECT_EQ(redirect_url, r->url()); |
| 3954 | EXPECT_EQ(original_url, r->original_url()); |
| 3955 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3956 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3957 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3958 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3959 | EXPECT_EQ(kData, d.data_received()); |
| 3960 | } |
| 3961 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3962 | } |
| 3963 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3964 | // Tests that the network delegate can block and redirect a request to a new |
| 3965 | // URL during OnHeadersReceived. |
| 3966 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3967 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3968 | |
| 3969 | TestDelegate d; |
| 3970 | BlockingNetworkDelegate network_delegate( |
| 3971 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3972 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3973 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3974 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3975 | |
| 3976 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3977 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3978 | |
| 3979 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3980 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3981 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3982 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3983 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3984 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3985 | base::RunLoop().Run(); |
| 3986 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3987 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3988 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 3989 | // before_send_headers_with_proxy_count only increments for headers sent |
| 3990 | // through an untunneled proxy. |
| 3991 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3992 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3993 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3994 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3995 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3996 | EXPECT_EQ(redirect_url, r->url()); |
| 3997 | EXPECT_EQ(original_url, r->original_url()); |
| 3998 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3999 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4000 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4001 | } |
| 4002 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4003 | } |
| 4004 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4005 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4006 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4007 | // handle the challenge, and is passing the buck along to the |
| 4008 | // URLRequest::Delegate. |
| 4009 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4010 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4011 | |
| 4012 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4013 | BlockingNetworkDelegate network_delegate( |
| 4014 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4015 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4016 | TestURLRequestContext context(true); |
| 4017 | context.set_network_delegate(&network_delegate); |
| 4018 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4019 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4020 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4021 | |
| 4022 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4023 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4024 | std::unique_ptr<URLRequest> r( |
| 4025 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4026 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4027 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4028 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4029 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4030 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4031 | EXPECT_EQ(0, r->status().error()); |
| 4032 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4033 | EXPECT_TRUE(d.auth_required_called()); |
| 4034 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4035 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4036 | } |
| 4037 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4038 | } |
| 4039 | |
| 4040 | TEST_F(URLRequestTestHTTP, |
| 4041 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4042 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4043 | |
| 4044 | TestDelegate d; |
| 4045 | BlockingNetworkDelegate network_delegate( |
| 4046 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4047 | |
| 4048 | TestURLRequestContext context(true); |
| 4049 | context.set_network_delegate(&network_delegate); |
| 4050 | context.Init(); |
| 4051 | |
| 4052 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4053 | |
| 4054 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4055 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4056 | std::unique_ptr<URLRequest> r( |
| 4057 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4058 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4059 | |
| 4060 | { |
| 4061 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4062 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4063 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 4064 | } |
| 4065 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4066 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4067 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4068 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4069 | EXPECT_EQ(0, r->status().error()); |
| 4070 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4071 | EXPECT_TRUE(d.auth_required_called()); |
| 4072 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4073 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4074 | } |
| 4075 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4076 | } |
| 4077 | |
| 4078 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4079 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4080 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4081 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4082 | |
| 4083 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4084 | BlockingNetworkDelegate network_delegate( |
| 4085 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4086 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4087 | network_delegate.set_auth_retval( |
| 4088 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4089 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4090 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4091 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4092 | TestURLRequestContext context(true); |
| 4093 | context.set_network_delegate(&network_delegate); |
| 4094 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4095 | |
| 4096 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4097 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4098 | std::unique_ptr<URLRequest> r( |
| 4099 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4100 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4101 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4102 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4103 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4104 | EXPECT_EQ(0, r->status().error()); |
| 4105 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4106 | EXPECT_FALSE(d.auth_required_called()); |
| 4107 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4108 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4109 | } |
| 4110 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4111 | } |
| 4112 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4113 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4114 | // headers (for the first or second request) when called at the proper times. |
| 4115 | TEST_F(URLRequestTestHTTP, |
| 4116 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4117 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4118 | |
| 4119 | TestDelegate d; |
| 4120 | BlockingNetworkDelegate network_delegate( |
| 4121 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4122 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4123 | network_delegate.set_auth_retval( |
| 4124 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4125 | |
| 4126 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4127 | |
| 4128 | TestURLRequestContext context(true); |
| 4129 | context.set_network_delegate(&network_delegate); |
| 4130 | context.Init(); |
| 4131 | |
| 4132 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4133 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4134 | std::unique_ptr<URLRequest> r( |
| 4135 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4136 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4137 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4138 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4139 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4140 | EXPECT_EQ(0, r->status().error()); |
| 4141 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4142 | EXPECT_FALSE(d.auth_required_called()); |
| 4143 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4144 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4145 | |
| 4146 | { |
| 4147 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4148 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4149 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4150 | } |
| 4151 | } |
| 4152 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4153 | } |
| 4154 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4155 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4156 | // by cancelling authentication. |
| 4157 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4158 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4159 | |
| 4160 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4161 | BlockingNetworkDelegate network_delegate( |
| 4162 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4163 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4164 | network_delegate.set_auth_retval( |
| 4165 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4166 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4167 | TestURLRequestContext context(true); |
| 4168 | context.set_network_delegate(&network_delegate); |
| 4169 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4170 | |
| 4171 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4172 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4173 | std::unique_ptr<URLRequest> r( |
| 4174 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4175 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4176 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4177 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4178 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4179 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4180 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4181 | EXPECT_FALSE(d.auth_required_called()); |
| 4182 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4183 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4184 | } |
| 4185 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4186 | } |
| 4187 | |
| 4188 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4189 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4190 | // to handle the challenge, and is passing the buck along to the |
| 4191 | // URLRequest::Delegate. |
| 4192 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4193 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4194 | |
| 4195 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4196 | BlockingNetworkDelegate network_delegate( |
| 4197 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4198 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4199 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4200 | TestURLRequestContext context(true); |
| 4201 | context.set_network_delegate(&network_delegate); |
| 4202 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4203 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4204 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4205 | |
| 4206 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4207 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4208 | std::unique_ptr<URLRequest> r( |
| 4209 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4210 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4211 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4212 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4213 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4214 | EXPECT_EQ(0, r->status().error()); |
| 4215 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4216 | EXPECT_TRUE(d.auth_required_called()); |
| 4217 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4218 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4219 | } |
| 4220 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4221 | } |
| 4222 | |
| 4223 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4224 | // by setting credentials. |
| 4225 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4226 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4227 | |
| 4228 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4229 | BlockingNetworkDelegate network_delegate( |
| 4230 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4231 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4232 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4233 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4234 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4235 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4236 | network_delegate.set_auth_credentials(auth_credentials); |
| 4237 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4238 | TestURLRequestContext context(true); |
| 4239 | context.set_network_delegate(&network_delegate); |
| 4240 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4241 | |
| 4242 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4243 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4244 | std::unique_ptr<URLRequest> r( |
| 4245 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4246 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4247 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4248 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4249 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4250 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4251 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4252 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4253 | EXPECT_FALSE(d.auth_required_called()); |
| 4254 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4255 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4256 | } |
| 4257 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4258 | } |
| 4259 | |
| 4260 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4261 | // by cancelling authentication. |
| 4262 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4263 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4264 | |
| 4265 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4266 | BlockingNetworkDelegate network_delegate( |
| 4267 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4268 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4269 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4270 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4271 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4272 | TestURLRequestContext context(true); |
| 4273 | context.set_network_delegate(&network_delegate); |
| 4274 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4275 | |
| 4276 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4277 | GURL url(http_test_server()->GetURL("/auth-basic")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4278 | std::unique_ptr<URLRequest> r( |
| 4279 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4280 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4281 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4282 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4283 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4284 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4285 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4286 | EXPECT_FALSE(d.auth_required_called()); |
| 4287 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4288 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4289 | } |
| 4290 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4291 | } |
| 4292 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4293 | // Tests that we can handle when a network request was canceled while we were |
| 4294 | // waiting for the network delegate. |
| 4295 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4296 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4297 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4298 | |
| 4299 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4300 | BlockingNetworkDelegate network_delegate( |
| 4301 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4302 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4303 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4304 | TestURLRequestContext context(true); |
| 4305 | context.set_network_delegate(&network_delegate); |
| 4306 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4307 | |
| 4308 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4309 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4310 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4311 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4312 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4313 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4314 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4315 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4316 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4317 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4318 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4319 | // Ensure that network delegate is notified. |
| 4320 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4321 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4322 | EXPECT_THAT(r->status().error(), IsError(ERR_ABORTED)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4323 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4324 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4325 | } |
| 4326 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4327 | } |
| 4328 | |
| 4329 | // Tests that we can handle when a network request was canceled while we were |
| 4330 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4331 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4332 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4333 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4334 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4335 | |
| 4336 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4337 | BlockingNetworkDelegate network_delegate( |
| 4338 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4339 | network_delegate.set_block_on( |
| 4340 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4341 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4342 | TestURLRequestContext context(true); |
| 4343 | context.set_network_delegate(&network_delegate); |
| 4344 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4345 | |
| 4346 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4347 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4348 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4349 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4350 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4351 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4352 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4353 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4354 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4355 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4356 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4357 | // Ensure that network delegate is notified. |
| 4358 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4359 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4360 | EXPECT_THAT(r->status().error(), IsError(ERR_ABORTED)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4361 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4362 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4363 | } |
| 4364 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4365 | } |
| 4366 | |
| 4367 | // Tests that we can handle when a network request was canceled while we were |
| 4368 | // waiting for the network delegate. |
| 4369 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4370 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4371 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4372 | |
| 4373 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4374 | BlockingNetworkDelegate network_delegate( |
| 4375 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4376 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4377 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4378 | TestURLRequestContext context(true); |
| 4379 | context.set_network_delegate(&network_delegate); |
| 4380 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4381 | |
| 4382 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4383 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4384 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4385 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4386 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4387 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4388 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4389 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4390 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4391 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4392 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4393 | // Ensure that network delegate is notified. |
| 4394 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4395 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4396 | EXPECT_THAT(r->status().error(), IsError(ERR_ABORTED)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4397 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4398 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4399 | } |
| 4400 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4401 | } |
| 4402 | |
| 4403 | // Tests that we can handle when a network request was canceled while we were |
| 4404 | // waiting for the network delegate. |
| 4405 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4406 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4407 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4408 | |
| 4409 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4410 | BlockingNetworkDelegate network_delegate( |
| 4411 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4412 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4413 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4414 | TestURLRequestContext context(true); |
| 4415 | context.set_network_delegate(&network_delegate); |
| 4416 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4417 | |
| 4418 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4419 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4420 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4421 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4422 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4423 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4424 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4425 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4426 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4427 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4428 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4429 | // Ensure that network delegate is notified. |
| 4430 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4431 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4432 | EXPECT_THAT(r->status().error(), IsError(ERR_ABORTED)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4433 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4434 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4435 | } |
| 4436 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4437 | } |
| 4438 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4439 | namespace { |
| 4440 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4441 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4442 | const test_server::HttpRequest& request) { |
| 4443 | if (request.headers.find("Host") == request.headers.end() || |
| 4444 | request.headers.at("Host") != "www.server-auth.com" || |
| 4445 | request.method != test_server::METHOD_CONNECT) { |
| 4446 | return nullptr; |
| 4447 | } |
| 4448 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4449 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4450 | new test_server::BasicHttpResponse); |
| 4451 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4452 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4453 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4454 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | } // namespace |
| 4458 | |
| 4459 | // 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] | 4460 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4461 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4462 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4463 | http_test_server()->RegisterRequestHandler( |
| 4464 | base::Bind(&HandleServerAuthConnect)); |
| 4465 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4466 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4467 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4468 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4469 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4470 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4471 | TestDelegate d; |
| 4472 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4473 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4474 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4476 | r->Start(); |
| 4477 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4478 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4479 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4480 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4481 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4482 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4483 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4484 | EXPECT_THAT(r->status().error(), IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4485 | } |
| 4486 | } |
| 4487 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4488 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4489 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4490 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4491 | TestDelegate d; |
| 4492 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4493 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4494 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4495 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4496 | r->Start(); |
| 4497 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4498 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4499 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4500 | |
| 4501 | EXPECT_EQ(1, d.response_started_count()); |
| 4502 | EXPECT_FALSE(d.received_data_before_response()); |
| 4503 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4504 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4505 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4506 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4507 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4508 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4509 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4510 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4511 | } |
| 4512 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4513 | // This test has the server send a large number of cookies to the client. |
| 4514 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4515 | // search is used to estimate that maximum number of cookies that are accepted |
| 4516 | // by the browser. Beyond the maximum number, the request will fail with |
| 4517 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4518 | #if defined(OS_WIN) |
| 4519 | // http://crbug.com/177916 |
| 4520 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4521 | #else |
| 4522 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4523 | #endif // defined(OS_WIN) |
| 4524 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4525 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4526 | |
| 4527 | int lower_bound = 0; |
| 4528 | int upper_bound = 1; |
| 4529 | |
| 4530 | // Double the number of cookies until the response header limits are |
| 4531 | // exceeded. |
| 4532 | while (DoManyCookiesRequest(upper_bound)) { |
| 4533 | lower_bound = upper_bound; |
| 4534 | upper_bound *= 2; |
| 4535 | ASSERT_LT(upper_bound, 1000000); |
| 4536 | } |
| 4537 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4538 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4539 | if (tolerance < 2) |
| 4540 | tolerance = 2; |
| 4541 | |
| 4542 | // Perform a binary search to find the highest possible number of cookies, |
| 4543 | // within the desired tolerance. |
| 4544 | while (upper_bound - lower_bound >= tolerance) { |
| 4545 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4546 | |
| 4547 | if (DoManyCookiesRequest(num_cookies)) |
| 4548 | lower_bound = num_cookies; |
| 4549 | else |
| 4550 | upper_bound = num_cookies; |
| 4551 | } |
| 4552 | // Success: the test did not crash. |
| 4553 | } |
| 4554 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4555 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4556 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4557 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4558 | TestDelegate d; |
| 4559 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4560 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4561 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4562 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4563 | r->Start(); |
| 4564 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4565 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4566 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4567 | |
| 4568 | EXPECT_EQ(1, d.response_started_count()); |
| 4569 | EXPECT_FALSE(d.received_data_before_response()); |
| 4570 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4571 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4572 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4573 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4574 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4575 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4576 | } |
| 4577 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4578 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4579 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4580 | |
| 4581 | TestDelegate d; |
| 4582 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4583 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4584 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4585 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4586 | |
| 4587 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4588 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4590 | r->Start(); |
| 4591 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4592 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4593 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4594 | |
| 4595 | EXPECT_EQ(1, d.response_started_count()); |
| 4596 | EXPECT_FALSE(d.received_data_before_response()); |
| 4597 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4598 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4599 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4600 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4601 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4602 | |
| 4603 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4604 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4605 | } |
| 4606 | } |
| 4607 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4608 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4609 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4610 | |
| 4611 | TestDelegate d; |
| 4612 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4613 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4614 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4615 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4616 | r->Start(); |
| 4617 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4618 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4619 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4620 | |
| 4621 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4622 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4623 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4624 | |
| 4625 | EXPECT_EQ(1, d.response_started_count()); |
| 4626 | EXPECT_FALSE(d.received_data_before_response()); |
| 4627 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4628 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4629 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4630 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4631 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4635 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 4636 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4637 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4638 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 4639 | SpawnedTestServer::kLocalhost, |
| 4640 | base::FilePath(kTestFilePath)); |
| 4641 | |
| 4642 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4643 | |
| 4644 | // Parameter that specifies the Content-Length field in the response: |
| 4645 | // C - Compressed length. |
| 4646 | // U - Uncompressed length. |
| 4647 | // L - Large length (larger than both C & U). |
| 4648 | // M - Medium length (between C & U). |
| 4649 | // S - Small length (smaller than both C & U). |
| 4650 | const char test_parameters[] = "CULMS"; |
| 4651 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 4652 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 4653 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4654 | // S has too little data, but we seem to accept it. |
| 4655 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 4656 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4657 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 4658 | base::FilePath file_path; |
| 4659 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 4660 | file_path = file_path.Append(kTestFilePath); |
| 4661 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 4662 | std::string expected_content; |
| 4663 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 4664 | |
| 4665 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4666 | TestDelegate d; |
| 4667 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4668 | std::string test_file = base::StringPrintf( |
| 4669 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4670 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4671 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4672 | TestURLRequestContext context(true); |
| 4673 | context.set_network_delegate(&network_delegate); |
| 4674 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4675 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4676 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4677 | test_server.GetURL(test_file), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4678 | r->Start(); |
| 4679 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4680 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4681 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4682 | |
| 4683 | EXPECT_EQ(1, d.response_started_count()); |
| 4684 | EXPECT_FALSE(d.received_data_before_response()); |
| 4685 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4686 | << " status = " << r->status().status() |
| 4687 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4688 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4689 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4690 | << " Parameter = \"" << test_file << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 4691 | if (test_parameters[i] == 'S') { |
| 4692 | // When content length is smaller than both compressed length and |
| 4693 | // uncompressed length, HttpStreamParser might not read the full |
| 4694 | // response body. |
| 4695 | continue; |
| 4696 | } |
| 4697 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4698 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4699 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4700 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4701 | << " Parameter = \"" << test_file << "\""; |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4706 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4707 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4708 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4709 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4710 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4711 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4712 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4713 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4714 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4715 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4716 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4717 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4718 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4719 | |
| 4720 | EXPECT_EQ(1, d.response_started_count()); |
| 4721 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4722 | EXPECT_EQ(destination_url, req->url()); |
| 4723 | EXPECT_EQ(original_url, req->original_url()); |
| 4724 | ASSERT_EQ(2U, req->url_chain().size()); |
| 4725 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4726 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4727 | |
| 4728 | LoadTimingInfo load_timing_info_before_redirect; |
| 4729 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 4730 | &load_timing_info_before_redirect)); |
| 4731 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 4732 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 4733 | |
| 4734 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4735 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4736 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4737 | |
| 4738 | // Check that a new socket was used on redirect, since the server does not |
| 4739 | // supposed keep-alive sockets, and that the times before the redirect are |
| 4740 | // before the ones recorded for the second request. |
| 4741 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 4742 | load_timing_info.socket_log_id); |
| 4743 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 4744 | load_timing_info.connect_timing.connect_start); |
| 4745 | } |
| 4746 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4747 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4748 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4749 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4750 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4751 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4752 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 4753 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 4754 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4755 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4756 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4757 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4758 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4759 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4760 | |
| 4761 | EXPECT_EQ(1, d.response_started_count()); |
| 4762 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4763 | EXPECT_EQ(destination_url, req->url()); |
| 4764 | EXPECT_EQ(original_url, req->original_url()); |
| 4765 | ASSERT_EQ(3U, req->url_chain().size()); |
| 4766 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4767 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 4768 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4769 | } |
| 4770 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4771 | // First and second pieces of information logged by delegates to URLRequests. |
| 4772 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 4773 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 4774 | |
| 4775 | // Logs delegate information to a URLRequest. The first string is logged |
| 4776 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 4777 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 4778 | // another asynchronous call is used to clear the delegate information |
| 4779 | // before calling a callback. The object then deletes itself. |
| 4780 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 4781 | public: |
| 4782 | typedef base::Callback<void()> Callback; |
| 4783 | |
| 4784 | // Each time delegate information is added to the URLRequest, the resulting |
| 4785 | // load state is checked. The expected load state after each request is |
| 4786 | // passed in as an argument. |
| 4787 | static void Run(URLRequest* url_request, |
| 4788 | LoadState expected_first_load_state, |
| 4789 | LoadState expected_second_load_state, |
| 4790 | LoadState expected_third_load_state, |
| 4791 | const Callback& callback) { |
| 4792 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 4793 | url_request, |
| 4794 | expected_first_load_state, |
| 4795 | expected_second_load_state, |
| 4796 | expected_third_load_state, |
| 4797 | callback); |
| 4798 | logger->Start(); |
| 4799 | } |
| 4800 | |
| 4801 | // Checks that the log entries, starting with log_position, contain the |
| 4802 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 4803 | // recorded. Returns the index of entry after the expected number of |
| 4804 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4805 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4806 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4807 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 4808 | if (log_position + 3 >= entries.size()) { |
| 4809 | ADD_FAILURE() << "Not enough log entries"; |
| 4810 | return entries.size(); |
| 4811 | } |
| 4812 | std::string delegate_info; |
| 4813 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4814 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4815 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4816 | &delegate_info)); |
| 4817 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 4818 | |
| 4819 | ++log_position; |
| 4820 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4821 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4822 | |
| 4823 | ++log_position; |
| 4824 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4825 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4826 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4827 | &delegate_info)); |
| 4828 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 4829 | |
| 4830 | ++log_position; |
| 4831 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4832 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4833 | |
| 4834 | return log_position + 1; |
| 4835 | } |
| 4836 | |
| 4837 | private: |
| 4838 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 4839 | |
| 4840 | AsyncDelegateLogger(URLRequest* url_request, |
| 4841 | LoadState expected_first_load_state, |
| 4842 | LoadState expected_second_load_state, |
| 4843 | LoadState expected_third_load_state, |
| 4844 | const Callback& callback) |
| 4845 | : url_request_(url_request), |
| 4846 | expected_first_load_state_(expected_first_load_state), |
| 4847 | expected_second_load_state_(expected_second_load_state), |
| 4848 | expected_third_load_state_(expected_third_load_state), |
| 4849 | callback_(callback) { |
| 4850 | } |
| 4851 | |
| 4852 | ~AsyncDelegateLogger() {} |
| 4853 | |
| 4854 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4855 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4856 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4857 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 4858 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4859 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4860 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4864 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4865 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4866 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 4867 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 4868 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4869 | } else { |
| 4870 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4871 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4872 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4873 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4877 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4878 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4879 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 4880 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4881 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4882 | callback_.Run(); |
| 4883 | } |
| 4884 | |
| 4885 | URLRequest* url_request_; |
| 4886 | const int expected_first_load_state_; |
| 4887 | const int expected_second_load_state_; |
| 4888 | const int expected_third_load_state_; |
| 4889 | const Callback callback_; |
| 4890 | |
| 4891 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 4892 | }; |
| 4893 | |
| 4894 | // NetworkDelegate that logs delegate information before a request is started, |
| 4895 | // before headers are sent, when headers are read, and when auth information |
| 4896 | // is requested. Uses AsyncDelegateLogger. |
| 4897 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 4898 | public: |
| 4899 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4900 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4901 | |
| 4902 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4903 | int OnBeforeURLRequest(URLRequest* request, |
| 4904 | const CompletionCallback& callback, |
| 4905 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4906 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 4907 | return RunCallbackAsynchronously(request, callback); |
| 4908 | } |
| 4909 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4910 | int OnBeforeStartTransaction(URLRequest* request, |
| 4911 | const CompletionCallback& callback, |
| 4912 | HttpRequestHeaders* headers) override { |
| 4913 | TestNetworkDelegate::OnBeforeStartTransaction(request, callback, headers); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4914 | return RunCallbackAsynchronously(request, callback); |
| 4915 | } |
| 4916 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4917 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4918 | URLRequest* request, |
| 4919 | const CompletionCallback& callback, |
| 4920 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4921 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4922 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4923 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4924 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4925 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4926 | override_response_headers, |
| 4927 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4928 | return RunCallbackAsynchronously(request, callback); |
| 4929 | } |
| 4930 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4931 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4932 | URLRequest* request, |
| 4933 | const AuthChallengeInfo& auth_info, |
| 4934 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4935 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4936 | AsyncDelegateLogger::Run( |
| 4937 | request, |
| 4938 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4939 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4940 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4941 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4942 | callback, credentials)); |
| 4943 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4944 | } |
| 4945 | |
| 4946 | private: |
| 4947 | static int RunCallbackAsynchronously( |
| 4948 | URLRequest* request, |
| 4949 | const CompletionCallback& callback) { |
| 4950 | AsyncDelegateLogger::Run( |
| 4951 | request, |
| 4952 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4953 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4954 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4955 | base::Bind(callback, OK)); |
| 4956 | return ERR_IO_PENDING; |
| 4957 | } |
| 4958 | |
| 4959 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4960 | AuthCredentials* credentials) { |
| 4961 | *credentials = AuthCredentials(kUser, kSecret); |
| 4962 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4963 | } |
| 4964 | |
| 4965 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4966 | }; |
| 4967 | |
| 4968 | // URLRequest::Delegate that logs delegate information when the headers |
| 4969 | // are received, when each read completes, and during redirects. Uses |
| 4970 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4971 | // |
| 4972 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4973 | // advancing to the next step in most cases, as well as cancellation. |
| 4974 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4975 | public: |
| 4976 | enum CancelStage { |
| 4977 | NO_CANCEL = 0, |
| 4978 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4979 | CANCEL_ON_RESPONSE_STARTED, |
| 4980 | CANCEL_ON_READ_COMPLETED |
| 4981 | }; |
| 4982 | |
| 4983 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4984 | : cancel_stage_(cancel_stage) { |
| 4985 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4986 | set_cancel_in_received_redirect(true); |
| 4987 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4988 | set_cancel_in_response_started(true); |
| 4989 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4990 | set_cancel_in_received_data(true); |
| 4991 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4992 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4993 | |
| 4994 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4995 | void OnReceivedRedirect(URLRequest* request, |
| 4996 | const RedirectInfo& redirect_info, |
| 4997 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4998 | *defer_redirect = true; |
| 4999 | AsyncDelegateLogger::Run( |
| 5000 | request, |
| 5001 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5002 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5003 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5004 | base::Bind( |
| 5005 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5006 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5007 | } |
| 5008 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5009 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5010 | AsyncDelegateLogger::Run( |
| 5011 | request, |
| 5012 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5013 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5014 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5015 | base::Bind( |
| 5016 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5017 | base::Unretained(this), request)); |
| 5018 | } |
| 5019 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5020 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5021 | AsyncDelegateLogger::Run( |
| 5022 | request, |
| 5023 | LOAD_STATE_IDLE, |
| 5024 | LOAD_STATE_IDLE, |
| 5025 | LOAD_STATE_IDLE, |
| 5026 | base::Bind( |
| 5027 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5028 | base::Unretained(this), request, bytes_read)); |
| 5029 | } |
| 5030 | |
| 5031 | private: |
| 5032 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5033 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5034 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5035 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5036 | // FollowDeferredRedirect should not be called after cancellation. |
| 5037 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5038 | return; |
| 5039 | if (!defer_redirect) |
| 5040 | request->FollowDeferredRedirect(); |
| 5041 | } |
| 5042 | |
| 5043 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 5044 | // The parent class continues the request. |
| 5045 | TestDelegate::OnResponseStarted(request); |
| 5046 | } |
| 5047 | |
| 5048 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5049 | // The parent class continues the request. |
| 5050 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5051 | } |
| 5052 | |
| 5053 | const CancelStage cancel_stage_; |
| 5054 | |
| 5055 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5056 | }; |
| 5057 | |
| 5058 | // Tests handling of delegate info before a request starts. |
| 5059 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5060 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5061 | |
| 5062 | TestDelegate request_delegate; |
| 5063 | TestURLRequestContext context(true); |
| 5064 | context.set_network_delegate(NULL); |
| 5065 | context.set_net_log(&net_log_); |
| 5066 | context.Init(); |
| 5067 | |
| 5068 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5069 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5070 | context.CreateRequest(http_test_server()->GetURL("/defaultresponse"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5071 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5072 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5073 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5074 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5075 | |
| 5076 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5077 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5078 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5079 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5080 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5081 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5082 | |
| 5083 | base::RunLoop().Run(); |
| 5084 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5085 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5086 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5087 | } |
| 5088 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5089 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5090 | net_log_.GetEntries(&entries); |
| 5091 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 5092 | entries, |
| 5093 | 0, |
| 5094 | NetLog::TYPE_DELEGATE_INFO, |
| 5095 | NetLog::PHASE_BEGIN); |
| 5096 | |
| 5097 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5098 | |
| 5099 | // Nothing else should add any delegate info to the request. |
| 5100 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5101 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5102 | } |
| 5103 | |
| 5104 | // Tests handling of delegate info from a network delegate. |
| 5105 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5106 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5107 | |
| 5108 | TestDelegate request_delegate; |
| 5109 | AsyncLoggingNetworkDelegate network_delegate; |
| 5110 | TestURLRequestContext context(true); |
| 5111 | context.set_network_delegate(&network_delegate); |
| 5112 | context.set_net_log(&net_log_); |
| 5113 | context.Init(); |
| 5114 | |
| 5115 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5116 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5117 | context.CreateRequest(http_test_server()->GetURL("/simple.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5118 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5119 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5120 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5121 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5122 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5123 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5124 | base::RunLoop().Run(); |
| 5125 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5126 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5127 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5128 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5129 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5130 | } |
| 5131 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5132 | |
| 5133 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5134 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5135 | net_log_.GetEntries(&entries); |
| 5136 | for (size_t i = 0; i < 3; ++i) { |
| 5137 | log_position = ExpectLogContainsSomewhereAfter( |
| 5138 | entries, |
| 5139 | log_position + 1, |
| 5140 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5141 | NetLog::PHASE_BEGIN); |
| 5142 | |
| 5143 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5144 | log_position + 1); |
| 5145 | |
| 5146 | ASSERT_LT(log_position, entries.size()); |
| 5147 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5148 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5149 | } |
| 5150 | |
| 5151 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5152 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5153 | } |
| 5154 | |
| 5155 | // Tests handling of delegate info from a network delegate in the case of an |
| 5156 | // HTTP redirect. |
| 5157 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5158 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5159 | |
| 5160 | TestDelegate request_delegate; |
| 5161 | AsyncLoggingNetworkDelegate network_delegate; |
| 5162 | TestURLRequestContext context(true); |
| 5163 | context.set_network_delegate(&network_delegate); |
| 5164 | context.set_net_log(&net_log_); |
| 5165 | context.Init(); |
| 5166 | |
| 5167 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5168 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5169 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5170 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5171 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5172 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5173 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5174 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5175 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5176 | base::RunLoop().Run(); |
| 5177 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5178 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5179 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5180 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5181 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5182 | } |
| 5183 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5184 | |
| 5185 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5186 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5187 | net_log_.GetEntries(&entries); |
| 5188 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5189 | // OnBeforeStartTransaction, and OnHeadersReceived. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5190 | for (size_t i = 0; i < 3; ++i) { |
| 5191 | log_position = ExpectLogContainsSomewhereAfter( |
| 5192 | entries, |
| 5193 | log_position + 1, |
| 5194 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5195 | NetLog::PHASE_BEGIN); |
| 5196 | |
| 5197 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5198 | log_position + 1); |
| 5199 | |
| 5200 | ASSERT_LT(log_position, entries.size()); |
| 5201 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5202 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5203 | } |
| 5204 | |
| 5205 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5206 | log_position = ExpectLogContainsSomewhereAfter( |
| 5207 | entries, |
| 5208 | log_position + 1, |
| 5209 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5210 | NetLog::PHASE_BEGIN); |
| 5211 | |
| 5212 | // The NetworkDelegate logged information in the same three events as before. |
| 5213 | for (size_t i = 0; i < 3; ++i) { |
| 5214 | log_position = ExpectLogContainsSomewhereAfter( |
| 5215 | entries, |
| 5216 | log_position + 1, |
| 5217 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5218 | NetLog::PHASE_BEGIN); |
| 5219 | |
| 5220 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5221 | log_position + 1); |
| 5222 | |
| 5223 | ASSERT_LT(log_position, entries.size()); |
| 5224 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5225 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5226 | } |
| 5227 | |
| 5228 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5229 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5230 | } |
| 5231 | |
| 5232 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5233 | // AUTH. |
| 5234 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5235 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5236 | |
| 5237 | TestDelegate request_delegate; |
| 5238 | AsyncLoggingNetworkDelegate network_delegate; |
| 5239 | TestURLRequestContext context(true); |
| 5240 | context.set_network_delegate(&network_delegate); |
| 5241 | context.set_net_log(&net_log_); |
| 5242 | context.Init(); |
| 5243 | |
| 5244 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5245 | std::unique_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5246 | context.CreateRequest(http_test_server()->GetURL("/auth-basic"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5247 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5248 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5249 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5250 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5251 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5252 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5253 | base::RunLoop().Run(); |
| 5254 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5255 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5256 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5257 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5258 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5259 | } |
| 5260 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5261 | |
| 5262 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5263 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5264 | net_log_.GetEntries(&entries); |
| 5265 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5266 | // OnBeforeStartTransaction, OnHeadersReceived, OnAuthRequired, and then again |
| 5267 | // in |
| 5268 | // OnBeforeURLRequest and OnBeforeStartTransaction. |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5269 | for (size_t i = 0; i < 6; ++i) { |
| 5270 | log_position = ExpectLogContainsSomewhereAfter( |
| 5271 | entries, |
| 5272 | log_position + 1, |
| 5273 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5274 | NetLog::PHASE_BEGIN); |
| 5275 | |
| 5276 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5277 | log_position + 1); |
| 5278 | |
| 5279 | ASSERT_LT(log_position, entries.size()); |
| 5280 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5281 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5282 | } |
| 5283 | |
| 5284 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5285 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5286 | } |
| 5287 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5288 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5289 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5290 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5291 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5292 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 5293 | SpawnedTestServer::kLocalhost, |
| 5294 | base::FilePath(kTestFilePath)); |
| 5295 | |
| 5296 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5297 | |
| 5298 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5299 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5300 | TestURLRequestContext context(true); |
| 5301 | context.set_network_delegate(NULL); |
| 5302 | context.set_net_log(&net_log_); |
| 5303 | context.Init(); |
| 5304 | |
| 5305 | { |
| 5306 | // A chunked response with delays between chunks is used to make sure that |
| 5307 | // attempts by the URLRequest delegate to log information while reading the |
| 5308 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5309 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5310 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5311 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5312 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5313 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5314 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5315 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5316 | base::RunLoop().Run(); |
| 5317 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5318 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5319 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5320 | } |
| 5321 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5322 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5323 | net_log_.GetEntries(&entries); |
| 5324 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5325 | size_t log_position = 0; |
| 5326 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5327 | // The delegate info should only have been logged on header complete. Other |
| 5328 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5329 | log_position = |
| 5330 | ExpectLogContainsSomewhereAfter(entries, |
| 5331 | log_position + 1, |
| 5332 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5333 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5334 | |
| 5335 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5336 | log_position + 1); |
| 5337 | |
| 5338 | ASSERT_LT(log_position, entries.size()); |
| 5339 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5340 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5341 | |
| 5342 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5343 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5344 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5345 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5346 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5347 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5348 | |
| 5349 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5350 | // an HTTP redirect. |
| 5351 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5352 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5353 | |
| 5354 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5355 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5356 | TestURLRequestContext context(true); |
| 5357 | context.set_network_delegate(NULL); |
| 5358 | context.set_net_log(&net_log_); |
| 5359 | context.Init(); |
| 5360 | |
| 5361 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5362 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5363 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5364 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5365 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5366 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5367 | base::RunLoop().Run(); |
| 5368 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5369 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5370 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5371 | } |
| 5372 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5373 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5374 | net_log_.GetEntries(&entries); |
| 5375 | |
| 5376 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5377 | // OnResponseStarted. |
| 5378 | size_t log_position = 0; |
| 5379 | for (int i = 0; i < 2; ++i) { |
| 5380 | log_position = ExpectLogContainsSomewhereAfter( |
| 5381 | entries, |
| 5382 | log_position, |
| 5383 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5384 | NetLog::PHASE_BEGIN); |
| 5385 | |
| 5386 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5387 | log_position + 1); |
| 5388 | |
| 5389 | ASSERT_LT(log_position, entries.size()); |
| 5390 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5391 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5392 | } |
| 5393 | |
| 5394 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5395 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5396 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5397 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5398 | } |
| 5399 | |
| 5400 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5401 | // an HTTP redirect, with cancellation at various points. |
| 5402 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5403 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5404 | |
| 5405 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5406 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5407 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5408 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5409 | }; |
| 5410 | |
| 5411 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5412 | ++test_case) { |
| 5413 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 5414 | TestURLRequestContext context(true); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5415 | TestNetLog net_log; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5416 | context.set_network_delegate(NULL); |
| 5417 | context.set_net_log(&net_log); |
| 5418 | context.Init(); |
| 5419 | |
| 5420 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5421 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5422 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5423 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5424 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5425 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5426 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5427 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5428 | } |
| 5429 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5430 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5431 | net_log.GetEntries(&entries); |
| 5432 | |
| 5433 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5434 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5435 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5436 | // still currently supported in that call. |
| 5437 | size_t log_position = 0; |
| 5438 | for (int i = 0; i < 2; ++i) { |
| 5439 | log_position = ExpectLogContainsSomewhereAfter( |
| 5440 | entries, |
| 5441 | log_position, |
| 5442 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5443 | NetLog::PHASE_BEGIN); |
| 5444 | |
| 5445 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5446 | log_position + 1); |
| 5447 | |
| 5448 | ASSERT_LT(log_position, entries.size()); |
| 5449 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5450 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5451 | } |
| 5452 | |
| 5453 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5454 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5455 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5456 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5457 | } |
| 5458 | } |
| 5459 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5460 | namespace { |
| 5461 | |
| 5462 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5463 | const char kExtraValue[] = "fubar"; |
| 5464 | |
| 5465 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5466 | void OnReceivedRedirect(URLRequest* request, |
| 5467 | const RedirectInfo& redirect_info, |
| 5468 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5469 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5470 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5471 | } |
| 5472 | }; |
| 5473 | |
| 5474 | } // namespace |
| 5475 | |
| 5476 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5477 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5478 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5479 | GURL destination_url = |
| 5480 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5481 | GURL original_url = |
| 5482 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5483 | RedirectWithAdditionalHeadersDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5484 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5485 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5486 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5487 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5488 | |
| 5489 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5490 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5491 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5492 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5493 | EXPECT_FALSE(req->is_pending()); |
| 5494 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5495 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5496 | } |
| 5497 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5498 | namespace { |
| 5499 | |
| 5500 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5501 | |
| 5502 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5503 | void OnReceivedRedirect(URLRequest* request, |
| 5504 | const RedirectInfo& redirect_info, |
| 5505 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5506 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5507 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5508 | } |
| 5509 | }; |
| 5510 | |
| 5511 | } // namespace |
| 5512 | |
| 5513 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5514 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5515 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5516 | GURL destination_url = http_test_server()->GetURL( |
| 5517 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5518 | GURL original_url = |
| 5519 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5520 | RedirectWithHeaderRemovalDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5521 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5522 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5523 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5524 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5525 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5526 | |
| 5527 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5528 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5529 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5530 | EXPECT_FALSE(req->is_pending()); |
| 5531 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5532 | EXPECT_EQ("None", d.data_received()); |
| 5533 | } |
| 5534 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5535 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 5536 | TestDelegate d; |
| 5537 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5538 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5539 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5541 | r->Start(); |
| 5542 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5543 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5544 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5545 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5546 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5547 | |
| 5548 | // We expect to receive OnResponseStarted even though the request has been |
| 5549 | // cancelled. |
| 5550 | EXPECT_EQ(1, d.response_started_count()); |
| 5551 | EXPECT_EQ(0, d.bytes_received()); |
| 5552 | EXPECT_FALSE(d.received_data_before_response()); |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5557 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5558 | |
| 5559 | TestDelegate d; |
| 5560 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5561 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5562 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5563 | |
| 5564 | d.set_cancel_in_response_started(true); |
| 5565 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5566 | r->Start(); |
| 5567 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5568 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5569 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5570 | |
| 5571 | EXPECT_EQ(1, d.response_started_count()); |
| 5572 | EXPECT_EQ(0, d.bytes_received()); |
| 5573 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5574 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5575 | } |
| 5576 | } |
| 5577 | |
| 5578 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5579 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5580 | |
| 5581 | TestDelegate d; |
| 5582 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5583 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5584 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5585 | |
| 5586 | d.set_cancel_in_received_data(true); |
| 5587 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5588 | r->Start(); |
| 5589 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5590 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5591 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5592 | |
| 5593 | EXPECT_EQ(1, d.response_started_count()); |
| 5594 | // There is no guarantee about how much data was received |
| 5595 | // before the cancel was issued. It could have been 0 bytes, |
| 5596 | // or it could have been all the bytes. |
| 5597 | // EXPECT_EQ(0, d.bytes_received()); |
| 5598 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5599 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5604 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5605 | |
| 5606 | TestDelegate d; |
| 5607 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5608 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5609 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5610 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5611 | r->Start(); |
| 5612 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5613 | |
| 5614 | // The request will be implicitly canceled when it is destroyed. The |
| 5615 | // test delegate must not post a quit message when this happens because |
| 5616 | // this test doesn't actually have a message loop. The quit message would |
| 5617 | // get put on this thread's message queue and the next test would exit |
| 5618 | // early, causing problems. |
| 5619 | d.set_quit_on_complete(false); |
| 5620 | } |
| 5621 | // expect things to just cleanup properly. |
| 5622 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 5623 | // 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] | 5624 | // message loop |
| 5625 | EXPECT_FALSE(d.received_data_before_response()); |
| 5626 | EXPECT_EQ(0, d.bytes_received()); |
| 5627 | } |
| 5628 | |
| 5629 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5630 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5631 | |
| 5632 | // populate cache |
| 5633 | { |
| 5634 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5635 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5636 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5637 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5638 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5639 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | // cancel read from cache (see bug 990242) |
| 5643 | { |
| 5644 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5645 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5646 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5647 | r->Start(); |
| 5648 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5649 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5650 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5651 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5652 | EXPECT_EQ(1, d.response_started_count()); |
| 5653 | EXPECT_EQ(0, d.bytes_received()); |
| 5654 | EXPECT_FALSE(d.received_data_before_response()); |
| 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5659 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5660 | HTTPUploadDataOperationTest("POST"); |
| 5661 | } |
| 5662 | |
| 5663 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5664 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5665 | HTTPUploadDataOperationTest("PUT"); |
| 5666 | } |
| 5667 | |
| 5668 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5669 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5670 | |
| 5671 | TestDelegate d; |
| 5672 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5673 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5674 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5675 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5676 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5677 | r->Start(); |
| 5678 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5679 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5680 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5681 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5682 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5683 | << "request failed: " << r->status().status() |
| 5684 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5685 | |
| 5686 | EXPECT_FALSE(d.received_data_before_response()); |
| 5687 | EXPECT_TRUE(d.data_received().empty()); |
| 5688 | } |
| 5689 | } |
| 5690 | |
| 5691 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5692 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5693 | |
| 5694 | TestDelegate d; |
| 5695 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5696 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5697 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5698 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5699 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5700 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5701 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 5702 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5703 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5704 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[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 path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5707 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 5708 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5709 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5710 | element_readers.push_back(base::WrapUnique(new UploadFileElementReader( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5711 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
| 5712 | std::numeric_limits<uint64_t>::max(), base::Time()))); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5713 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5714 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5715 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5716 | r->Start(); |
| 5717 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5718 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5719 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5720 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5721 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5722 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 5723 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 5724 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5725 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5726 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 5727 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5728 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5729 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5730 | << "request failed: " << r->status().status() |
| 5731 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5732 | |
| 5733 | EXPECT_FALSE(d.received_data_before_response()); |
| 5734 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5735 | EXPECT_EQ(size, d.bytes_received()); |
| 5736 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5737 | } |
| 5738 | } |
| 5739 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5740 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5741 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5742 | |
| 5743 | TestDelegate d; |
| 5744 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5745 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5746 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5747 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5748 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5749 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5750 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5751 | element_readers.push_back(base::WrapUnique(new UploadFileElementReader( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5752 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5753 | base::FilePath(FILE_PATH_LITERAL( |
| 5754 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 5755 | 0, std::numeric_limits<uint64_t>::max(), base::Time()))); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5756 | r->set_upload(base::WrapUnique<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5757 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5758 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5759 | r->Start(); |
| 5760 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5761 | |
| 5762 | base::RunLoop().Run(); |
| 5763 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5764 | EXPECT_TRUE(d.request_failed()); |
| 5765 | EXPECT_FALSE(d.received_data_before_response()); |
| 5766 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5767 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5768 | EXPECT_THAT(r->status().error(), IsError(ERR_FILE_NOT_FOUND)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5769 | } |
| 5770 | } |
| 5771 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5772 | namespace { |
| 5773 | |
| 5774 | // Adds a standard set of data to an upload for chunked upload integration |
| 5775 | // tests. |
| 5776 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 5777 | writer->AppendData("a", 1, false); |
| 5778 | writer->AppendData("bcd", 3, false); |
| 5779 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 5780 | writer->AppendData("\r\n\r\n", 4, false); |
| 5781 | writer->AppendData("0", 1, false); |
| 5782 | writer->AppendData("2323", 4, true); |
| 5783 | } |
| 5784 | |
| 5785 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 5786 | // the server. |
| 5787 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 5788 | // This should match the chunks sent by AddChunksToUpload(). |
| 5789 | const std::string expected_data = |
| 5790 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 5791 | |
| 5792 | ASSERT_EQ(1, d->response_started_count()) |
| 5793 | << "request failed: " << r->status().status() |
| 5794 | << ", os error: " << r->status().error(); |
| 5795 | |
| 5796 | EXPECT_FALSE(d->received_data_before_response()); |
| 5797 | |
| 5798 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 5799 | EXPECT_EQ(expected_data, d->data_received()); |
| 5800 | } |
| 5801 | |
| 5802 | } // namespace |
| 5803 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5804 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5805 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5806 | |
| 5807 | TestDelegate d; |
| 5808 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5809 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5810 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5811 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5812 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5813 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5814 | upload_data_stream->CreateWriter(); |
| 5815 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5816 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5817 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5818 | r->Start(); |
| 5819 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5820 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5821 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5822 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5823 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5824 | } |
| 5825 | } |
| 5826 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5827 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5828 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5829 | |
| 5830 | TestDelegate d; |
| 5831 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5832 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5833 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5834 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5835 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5836 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5837 | upload_data_stream->CreateWriter(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5838 | r->set_upload(base::WrapUnique(upload_data_stream.release())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5839 | r->set_method("POST"); |
| 5840 | r->Start(); |
| 5841 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5842 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5843 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5844 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5845 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5846 | } |
| 5847 | } |
| 5848 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5849 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5850 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5851 | |
| 5852 | TestDelegate d; |
| 5853 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5854 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5855 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5856 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5857 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5858 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5859 | upload_data_stream->CreateWriter(); |
| 5860 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5861 | r->set_method("POST"); |
| 5862 | r->Start(); |
| 5863 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5864 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5865 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 5866 | AddDataToUpload(writer.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5867 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5868 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5869 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5874 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5875 | |
| 5876 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5877 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5878 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5879 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5880 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5881 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5882 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5883 | |
| 5884 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5885 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5886 | |
| 5887 | std::string header; |
| 5888 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 5889 | EXPECT_EQ("private", header); |
| 5890 | |
| 5891 | header.clear(); |
| 5892 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 5893 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 5894 | |
| 5895 | // The response has two "X-Multiple-Entries" headers. |
| 5896 | // This verfies our output has them concatenated together. |
| 5897 | header.clear(); |
| 5898 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 5899 | EXPECT_EQ("a, b", header); |
| 5900 | } |
| 5901 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5902 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 5903 | // security state. (see http://crbug.com/550977). |
| 5904 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5905 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5906 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5907 | https_test_server.SetSSLConfig( |
| 5908 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5909 | https_test_server.ServeFilesFromSourceDirectory( |
| 5910 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5911 | ASSERT_TRUE(https_test_server.Start()); |
| 5912 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5913 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5914 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5915 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5916 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5917 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5918 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5919 | |
| 5920 | TransportSecurityState* security_state = |
| 5921 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5922 | TransportSecurityState::STSState sts_state; |
| 5923 | TransportSecurityState::PKPState pkp_state; |
| 5924 | EXPECT_TRUE( |
| 5925 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 5926 | EXPECT_FALSE( |
| 5927 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 5928 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 5929 | sts_state.upgrade_mode); |
| 5930 | EXPECT_TRUE(sts_state.include_subdomains); |
| 5931 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5932 | #if defined(OS_ANDROID) |
| 5933 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5934 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5935 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5936 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5937 | } |
| 5938 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5939 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5940 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5941 | https_test_server.ServeFilesFromSourceDirectory( |
| 5942 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5943 | ASSERT_TRUE(https_test_server.Start()); |
| 5944 | // Make sure this test fails if the test server is changed to not |
| 5945 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5946 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 5947 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5948 | |
| 5949 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5950 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5951 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5952 | request->Start(); |
| 5953 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5954 | TransportSecurityState* security_state = |
| 5955 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5956 | TransportSecurityState::STSState sts_state; |
| 5957 | EXPECT_FALSE( |
| 5958 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5959 | } |
| 5960 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5961 | namespace { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 5962 | const char kExpectCTStaticHostname[] = "preloaded-expect-ct.badssl.com"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5963 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 5964 | } // namespace |
| 5965 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5966 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5967 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 5968 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5969 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5970 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5971 | https_test_server.SetSSLConfig( |
| 5972 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5973 | https_test_server.ServeFilesFromSourceDirectory( |
| 5974 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5975 | ASSERT_TRUE(https_test_server.Start()); |
| 5976 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5977 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5978 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5979 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5980 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5981 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5982 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5983 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5984 | TransportSecurityState* security_state = |
| 5985 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5986 | TransportSecurityState::STSState sts_state; |
| 5987 | TransportSecurityState::PKPState pkp_state; |
| 5988 | EXPECT_FALSE( |
| 5989 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 5990 | EXPECT_TRUE( |
| 5991 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 5992 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 5993 | sts_state.upgrade_mode); |
| 5994 | EXPECT_FALSE(sts_state.include_subdomains); |
| 5995 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 5996 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5997 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5998 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5999 | } |
| 6000 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6001 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6002 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6003 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6004 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6005 | https_test_server.SetSSLConfig( |
| 6006 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6007 | https_test_server.ServeFilesFromSourceDirectory( |
| 6008 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6009 | ASSERT_TRUE(https_test_server.Start()); |
| 6010 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6011 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6012 | |
| 6013 | // Set up a pin for |test_server_hostname|. |
| 6014 | TransportSecurityState security_state; |
| 6015 | const base::Time current_time(base::Time::Now()); |
| 6016 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6017 | HashValueVector hashes; |
| 6018 | HashValue hash1; |
| 6019 | HashValue hash2; |
| 6020 | // The values here don't matter, as long as they are different from |
| 6021 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6022 | ASSERT_TRUE( |
| 6023 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6024 | ASSERT_TRUE( |
| 6025 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6026 | hashes.push_back(hash1); |
| 6027 | hashes.push_back(hash2); |
| 6028 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6029 | false, /* include subdomains */ |
| 6030 | hashes, report_uri); |
| 6031 | |
| 6032 | MockCertificateReportSender mock_report_sender; |
| 6033 | security_state.SetReportSender(&mock_report_sender); |
| 6034 | |
| 6035 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6036 | // set pin. |
| 6037 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6038 | ASSERT_TRUE(cert); |
| 6039 | |
| 6040 | MockCertVerifier cert_verifier; |
| 6041 | CertVerifyResult verify_result; |
| 6042 | verify_result.verified_cert = cert; |
| 6043 | verify_result.is_issued_by_known_root = true; |
| 6044 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6045 | ASSERT_TRUE( |
| 6046 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6047 | verify_result.public_key_hashes.push_back(hash3); |
| 6048 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6049 | |
| 6050 | TestNetworkDelegate network_delegate; |
| 6051 | TestURLRequestContext context(true); |
| 6052 | context.set_transport_security_state(&security_state); |
| 6053 | context.set_network_delegate(&network_delegate); |
| 6054 | context.set_cert_verifier(&cert_verifier); |
| 6055 | context.Init(); |
| 6056 | |
| 6057 | // Now send a request to trigger the violation. |
| 6058 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6059 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6060 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6061 | violating_request->Start(); |
| 6062 | base::RunLoop().Run(); |
| 6063 | |
| 6064 | // Check that a report was sent. |
| 6065 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6066 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6067 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6068 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6069 | ASSERT_TRUE(value); |
| 6070 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 6071 | base::DictionaryValue* report_dict; |
| 6072 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6073 | std::string report_hostname; |
| 6074 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6075 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6076 | } |
| 6077 | |
| 6078 | // Tests that reports get sent on requests with |
| 6079 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6080 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6081 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6082 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6083 | https_test_server.SetSSLConfig( |
| 6084 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6085 | https_test_server.ServeFilesFromSourceDirectory( |
| 6086 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6087 | ASSERT_TRUE(https_test_server.Start()); |
| 6088 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6089 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6090 | |
| 6091 | TransportSecurityState security_state; |
| 6092 | MockCertificateReportSender mock_report_sender; |
| 6093 | security_state.SetReportSender(&mock_report_sender); |
| 6094 | |
| 6095 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6096 | // header. |
| 6097 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6098 | ASSERT_TRUE(cert); |
| 6099 | |
| 6100 | MockCertVerifier cert_verifier; |
| 6101 | CertVerifyResult verify_result; |
| 6102 | verify_result.verified_cert = cert; |
| 6103 | verify_result.is_issued_by_known_root = true; |
| 6104 | HashValue hash; |
| 6105 | // This value doesn't matter, as long as it is different from the pins |
| 6106 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6107 | ASSERT_TRUE( |
| 6108 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6109 | verify_result.public_key_hashes.push_back(hash); |
| 6110 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6111 | |
| 6112 | TestNetworkDelegate network_delegate; |
| 6113 | TestURLRequestContext context(true); |
| 6114 | context.set_transport_security_state(&security_state); |
| 6115 | context.set_network_delegate(&network_delegate); |
| 6116 | context.set_cert_verifier(&cert_verifier); |
| 6117 | context.Init(); |
| 6118 | |
| 6119 | // Now send a request to trigger the violation. |
| 6120 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6121 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6122 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6123 | DEFAULT_PRIORITY, &d)); |
| 6124 | violating_request->Start(); |
| 6125 | base::RunLoop().Run(); |
| 6126 | |
| 6127 | // Check that a report was sent. |
| 6128 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6129 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6130 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6131 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6132 | ASSERT_TRUE(value); |
| 6133 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 6134 | base::DictionaryValue* report_dict; |
| 6135 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6136 | std::string report_hostname; |
| 6137 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6138 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6139 | } |
| 6140 | |
| 6141 | // Tests that reports do not get sent on requests with |
| 6142 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6143 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6144 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6145 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6146 | https_test_server.SetSSLConfig( |
| 6147 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6148 | https_test_server.ServeFilesFromSourceDirectory( |
| 6149 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6150 | ASSERT_TRUE(https_test_server.Start()); |
| 6151 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6152 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6153 | |
| 6154 | TransportSecurityState security_state; |
| 6155 | MockCertificateReportSender mock_report_sender; |
| 6156 | security_state.SetReportSender(&mock_report_sender); |
| 6157 | |
| 6158 | TestNetworkDelegate network_delegate; |
| 6159 | MockCertVerifier mock_cert_verifier; |
| 6160 | TestURLRequestContext context(true); |
| 6161 | context.set_transport_security_state(&security_state); |
| 6162 | context.set_network_delegate(&network_delegate); |
| 6163 | context.set_cert_verifier(&mock_cert_verifier); |
| 6164 | mock_cert_verifier.set_default_result(OK); |
| 6165 | context.Init(); |
| 6166 | |
| 6167 | // Now send a request that does not trigger the violation. |
| 6168 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6169 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6170 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6171 | DEFAULT_PRIORITY, &d)); |
| 6172 | request->Start(); |
| 6173 | base::RunLoop().Run(); |
| 6174 | |
| 6175 | // Check that a report was not sent. |
| 6176 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6177 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6178 | } |
| 6179 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6180 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6181 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6182 | https_test_server.ServeFilesFromSourceDirectory( |
| 6183 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6184 | ASSERT_TRUE(https_test_server.Start()); |
| 6185 | // Make sure this test fails if the test server is changed to not |
| 6186 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6187 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6188 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6189 | |
| 6190 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6191 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6192 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6193 | request->Start(); |
| 6194 | base::RunLoop().Run(); |
| 6195 | |
| 6196 | TransportSecurityState* security_state = |
| 6197 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6198 | TransportSecurityState::PKPState pkp_state; |
| 6199 | EXPECT_FALSE( |
| 6200 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6201 | } |
| 6202 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6203 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6204 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6205 | https_test_server.SetSSLConfig( |
| 6206 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6207 | https_test_server.ServeFilesFromSourceDirectory( |
| 6208 | base::FilePath(kTestFilePath)); |
| 6209 | ASSERT_TRUE(https_test_server.Start()); |
| 6210 | |
| 6211 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6212 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6213 | ASSERT_TRUE(cert); |
| 6214 | |
| 6215 | MockCertVerifier cert_verifier; |
| 6216 | CertVerifyResult verify_result; |
| 6217 | verify_result.verified_cert = cert; |
| 6218 | verify_result.is_issued_by_known_root = false; |
| 6219 | HashValue hash; |
| 6220 | ASSERT_TRUE( |
| 6221 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6222 | verify_result.public_key_hashes.push_back(hash); |
| 6223 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6224 | cert_verifier.set_default_result(OK); |
| 6225 | |
| 6226 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 6227 | |
| 6228 | // Set up HPKP |
| 6229 | base::Time current_time = base::Time::Now(); |
| 6230 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 6231 | HashValue pin; |
| 6232 | ASSERT_TRUE( |
| 6233 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 6234 | HashValueVector hashes; |
| 6235 | hashes.push_back(pin); |
| 6236 | GURL report_uri(kHPKPReportUri); |
| 6237 | TransportSecurityState security_state; |
| 6238 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6239 | false, /* include subdomains */ |
| 6240 | hashes, report_uri); |
| 6241 | |
| 6242 | TestNetworkDelegate network_delegate; |
| 6243 | TestURLRequestContext context(true); |
| 6244 | context.set_transport_security_state(&security_state); |
| 6245 | context.set_network_delegate(&network_delegate); |
| 6246 | context.set_cert_verifier(&cert_verifier); |
| 6247 | context.Init(); |
| 6248 | |
| 6249 | TestDelegate d; |
| 6250 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6251 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
| 6252 | request->Start(); |
| 6253 | base::RunLoop().Run(); |
| 6254 | |
| 6255 | TransportSecurityState::PKPState pkp_state; |
| 6256 | EXPECT_TRUE( |
| 6257 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6258 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6259 | } |
| 6260 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6261 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6262 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6263 | https_test_server.SetSSLConfig( |
| 6264 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6265 | https_test_server.ServeFilesFromSourceDirectory( |
| 6266 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6267 | ASSERT_TRUE(https_test_server.Start()); |
| 6268 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6269 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6270 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6271 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6272 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6273 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
| 6274 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6275 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6276 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6277 | |
| 6278 | // We should have set parameters from the first header, not the second. |
| 6279 | TransportSecurityState* security_state = |
| 6280 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6281 | TransportSecurityState::STSState sts_state; |
| 6282 | EXPECT_TRUE( |
| 6283 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6284 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6285 | sts_state.upgrade_mode); |
| 6286 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6287 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6288 | } |
| 6289 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6290 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6291 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6292 | https_test_server.SetSSLConfig( |
| 6293 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6294 | https_test_server.ServeFilesFromSourceDirectory( |
| 6295 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6296 | ASSERT_TRUE(https_test_server.Start()); |
| 6297 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6298 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6299 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6300 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6301 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6302 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
| 6303 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6304 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6305 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6306 | |
| 6307 | // We should have set parameters from the first header, not the second. |
| 6308 | TransportSecurityState* security_state = |
| 6309 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6310 | TransportSecurityState::STSState sts_state; |
| 6311 | TransportSecurityState::PKPState pkp_state; |
| 6312 | EXPECT_TRUE( |
| 6313 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6314 | EXPECT_TRUE( |
| 6315 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6316 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6317 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6318 | #if defined(OS_ANDROID) |
| 6319 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6320 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6321 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6322 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6323 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6324 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6325 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6326 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6327 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6328 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6329 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6330 | } |
| 6331 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6332 | // Tests that when multiple HPKP headers are present, asserting different |
| 6333 | // policies, that only the first such policy is processed. |
| 6334 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6335 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6336 | https_test_server.SetSSLConfig( |
| 6337 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6338 | https_test_server.ServeFilesFromSourceDirectory( |
| 6339 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6340 | ASSERT_TRUE(https_test_server.Start()); |
| 6341 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6342 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6343 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6344 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6345 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6346 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6347 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6348 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6349 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6350 | |
| 6351 | TransportSecurityState* security_state = |
| 6352 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6353 | TransportSecurityState::STSState sts_state; |
| 6354 | TransportSecurityState::PKPState pkp_state; |
| 6355 | EXPECT_TRUE( |
| 6356 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6357 | EXPECT_TRUE( |
| 6358 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6359 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6360 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6361 | #if defined(OS_ANDROID) |
| 6362 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6363 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6364 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6365 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6366 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6367 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6368 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6369 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6370 | } |
| 6371 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6372 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6373 | // called. |
| 6374 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6375 | public: |
| 6376 | MockExpectCTReporter() : num_failures_(0) {} |
| 6377 | ~MockExpectCTReporter() override {} |
| 6378 | |
| 6379 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6380 | const GURL& report_uri, |
| 6381 | const net::SSLInfo& ssl_info) override { |
| 6382 | num_failures_++; |
| 6383 | } |
| 6384 | |
| 6385 | uint32_t num_failures() { return num_failures_; } |
| 6386 | |
| 6387 | private: |
| 6388 | uint32_t num_failures_; |
| 6389 | }; |
| 6390 | |
| 6391 | // A CTVerifier that returns net::OK for every certificate. |
| 6392 | class MockCTVerifier : public CTVerifier { |
| 6393 | public: |
| 6394 | MockCTVerifier() {} |
| 6395 | ~MockCTVerifier() override {} |
| 6396 | |
| 6397 | int Verify(X509Certificate* cert, |
| 6398 | const std::string& stapled_ocsp_response, |
| 6399 | const std::string& sct_list_from_tls_extension, |
| 6400 | ct::CTVerifyResult* result, |
| 6401 | const BoundNetLog& net_log) override { |
| 6402 | return net::OK; |
| 6403 | } |
| 6404 | |
| 6405 | void SetObserver(Observer* observer) override {} |
| 6406 | }; |
| 6407 | |
| 6408 | // A CTPolicyEnforcer that returns a default CertPolicyCompliance value |
| 6409 | // for every certificate. |
| 6410 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6411 | public: |
| 6412 | MockCTPolicyEnforcer() |
| 6413 | : default_result_( |
| 6414 | ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS) {} |
| 6415 | ~MockCTPolicyEnforcer() override {} |
| 6416 | |
| 6417 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 6418 | X509Certificate* cert, |
| 6419 | const SCTList& verified_scts, |
| 6420 | const BoundNetLog& net_log) override { |
| 6421 | return default_result_; |
| 6422 | } |
| 6423 | |
| 6424 | void set_default_result(ct::CertPolicyCompliance default_result) { |
| 6425 | default_result_ = default_result; |
| 6426 | } |
| 6427 | |
| 6428 | private: |
| 6429 | ct::CertPolicyCompliance default_result_; |
| 6430 | }; |
| 6431 | |
| 6432 | // Tests that Expect CT headers are processed correctly. |
| 6433 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6434 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6435 | https_test_server.SetSSLConfig( |
| 6436 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6437 | https_test_server.ServeFilesFromSourceDirectory( |
| 6438 | base::FilePath(kTestFilePath)); |
| 6439 | ASSERT_TRUE(https_test_server.Start()); |
| 6440 | |
| 6441 | MockExpectCTReporter reporter; |
| 6442 | TransportSecurityState transport_security_state; |
| 6443 | transport_security_state.enable_static_expect_ct_ = true; |
| 6444 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6445 | |
| 6446 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6447 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6448 | ASSERT_TRUE(cert); |
| 6449 | MockCertVerifier cert_verifier; |
| 6450 | CertVerifyResult verify_result; |
| 6451 | verify_result.verified_cert = cert; |
| 6452 | verify_result.is_issued_by_known_root = true; |
| 6453 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6454 | |
| 6455 | // Set up a MockCTVerifier and MockCTPolicyEnforcer to trigger an Expect CT |
| 6456 | // violation. |
| 6457 | MockCTVerifier ct_verifier; |
| 6458 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6459 | ct_policy_enforcer.set_default_result( |
| 6460 | ct::CertPolicyCompliance::CERT_POLICY_NOT_ENOUGH_SCTS); |
| 6461 | |
| 6462 | TestNetworkDelegate network_delegate; |
| 6463 | // Use a MockHostResolver (which by default maps all hosts to |
| 6464 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6465 | // CT preload list. |
| 6466 | MockHostResolver host_resolver; |
| 6467 | TestURLRequestContext context(true); |
| 6468 | context.set_host_resolver(&host_resolver); |
| 6469 | context.set_transport_security_state(&transport_security_state); |
| 6470 | context.set_network_delegate(&network_delegate); |
| 6471 | context.set_cert_verifier(&cert_verifier); |
| 6472 | context.set_cert_transparency_verifier(&ct_verifier); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 6473 | context.set_ct_policy_enforcer(std::move(&ct_policy_enforcer)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6474 | context.Init(); |
| 6475 | |
| 6476 | // Now send a request to trigger the violation. |
| 6477 | TestDelegate d; |
| 6478 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6479 | GURL::Replacements replace_host; |
| 6480 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6481 | url = url.ReplaceComponents(replace_host); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6482 | std::unique_ptr<URLRequest> violating_request( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6483 | context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| 6484 | violating_request->Start(); |
| 6485 | base::RunLoop().Run(); |
| 6486 | |
| 6487 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6488 | } |
| 6489 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6490 | #endif // !defined(OS_IOS) |
| 6491 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6492 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6493 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6494 | |
| 6495 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6496 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6497 | http_test_server()->GetURL("/content-type-normalization.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6498 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6499 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6500 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6501 | |
| 6502 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6503 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6504 | EXPECT_EQ("text/html", mime_type); |
| 6505 | |
| 6506 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6507 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6508 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6509 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6510 | } |
| 6511 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6512 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6513 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6514 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6515 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6516 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6517 | |
| 6518 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6519 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6520 | } |
| 6521 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6522 | #if !defined(DISABLE_FILE_SUPPORT) |
| 6523 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 6524 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 6525 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6526 | FileProtocolHandler file_protocol_handler( |
| 6527 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6528 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 6529 | |
| 6530 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6531 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6532 | } |
| 6533 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6534 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6535 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6536 | |
| 6537 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6538 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6539 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6540 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6541 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6542 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6543 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6544 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6545 | EXPECT_THAT(req->status().error(), IsError(ERR_UNSAFE_REDIRECT)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6546 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6547 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6548 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6549 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6550 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6551 | |
| 6552 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6553 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6554 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6555 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6556 | req->Start(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 6557 | base::RunLoop().Run(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6558 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6559 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6560 | EXPECT_THAT(req->status().error(), IsError(ERR_UNSAFE_REDIRECT)); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6561 | } |
| 6562 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6563 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6564 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6565 | |
| 6566 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6567 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6568 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6569 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6570 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6571 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6572 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6573 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6574 | EXPECT_THAT(req->status().error(), IsError(ERR_INVALID_URL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6575 | } |
| 6576 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6577 | // Make sure redirects are cached, despite not reading their bodies. |
| 6578 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6579 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6580 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6581 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6582 | |
| 6583 | { |
| 6584 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6585 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6586 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6587 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6588 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6589 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6590 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6591 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | { |
| 6595 | TestDelegate d; |
| 6596 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6597 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6598 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6599 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6600 | base::RunLoop().Run(); |
| 6601 | |
| 6602 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6603 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6604 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6605 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6606 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6607 | base::RunLoop().Run(); |
| 6608 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6609 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6610 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6611 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6612 | } |
| 6613 | } |
| 6614 | |
| 6615 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 6616 | // when the headers are read, since the body won't have been read. |
| 6617 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6618 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6619 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6620 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6621 | |
| 6622 | { |
| 6623 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6624 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6625 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 6626 | redirect_to_url); |
| 6627 | |
| 6628 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6629 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6630 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6631 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6632 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6633 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6634 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6635 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6636 | } |
| 6637 | |
| 6638 | { |
| 6639 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6640 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6641 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6642 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6643 | base::RunLoop().Run(); |
| 6644 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6645 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 6646 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6647 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6648 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6649 | } |
| 6650 | } |
| 6651 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6652 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 6653 | // safe. |
| 6654 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6655 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6656 | |
| 6657 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 6658 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6659 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6660 | |
| 6661 | TestDelegate d; |
| 6662 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6663 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6664 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6665 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6666 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6667 | base::RunLoop().Run(); |
| 6668 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6669 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6671 | EXPECT_EQ(2U, r->url_chain().size()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6672 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6673 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6674 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 6675 | } |
| 6676 | } |
| 6677 | |
| 6678 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 6679 | // some other URL to the whitelist. |
| 6680 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6681 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6682 | |
| 6683 | GURL unsafe_url("data:text/html,something"); |
| 6684 | GURL different_unsafe_url("data:text/html,something-else"); |
| 6685 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6686 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 6687 | different_unsafe_url); |
| 6688 | |
| 6689 | TestDelegate d; |
| 6690 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6691 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6692 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6693 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6694 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6695 | base::RunLoop().Run(); |
| 6696 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6697 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6698 | EXPECT_THAT(r->status().error(), IsError(ERR_UNSAFE_REDIRECT)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6699 | } |
| 6700 | } |
| 6701 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6702 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 6703 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 6704 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6705 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6706 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6707 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6708 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6709 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6710 | |
| 6711 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6712 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6713 | |
| 6714 | TestDelegate d; |
| 6715 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6716 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6717 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6718 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6719 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6720 | base::RunLoop().Run(); |
| 6721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6722 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6723 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6724 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6725 | EXPECT_EQ(original_url, r->original_url()); |
| 6726 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6727 | } |
| 6728 | } |
| 6729 | |
| 6730 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 6731 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6732 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6733 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6734 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6735 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6736 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6737 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 6738 | GURL expected_redirect_url( |
| 6739 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6740 | |
| 6741 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6742 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6743 | |
| 6744 | TestDelegate d; |
| 6745 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6746 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6747 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6748 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6749 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6750 | base::RunLoop().Run(); |
| 6751 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6752 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6753 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6754 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6755 | EXPECT_EQ(original_url, r->original_url()); |
| 6756 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6757 | } |
| 6758 | } |
| 6759 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6760 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 6761 | // URL should not be changed. In particular, the reference fragment should not |
| 6762 | // be modified. |
| 6763 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6764 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6765 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6766 | GURL original_url( |
| 6767 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6768 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 6769 | |
| 6770 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6771 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 6772 | |
| 6773 | TestDelegate d; |
| 6774 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6775 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6776 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6777 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6778 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6779 | base::RunLoop().Run(); |
| 6780 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6781 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6782 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6783 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6784 | EXPECT_EQ(original_url, r->original_url()); |
| 6785 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | |
| 6789 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 6790 | // the reference fragment of the target URL must not be modified. |
| 6791 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6792 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6793 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6794 | GURL original_url( |
| 6795 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 6796 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6797 | |
| 6798 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6799 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6800 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6801 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6802 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6803 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 6804 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 6805 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6806 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6807 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6808 | base::RunLoop().Run(); |
| 6809 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6810 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6811 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6812 | EXPECT_EQ(original_url, r->original_url()); |
| 6813 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6814 | } |
| 6815 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 6816 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 6817 | ASSERT_TRUE(http_test_server()->Start()); |
| 6818 | |
| 6819 | const std::string referrer("foobar://totally.legit.referrer"); |
| 6820 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6821 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 6822 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
| 6823 | req->SetReferrer(referrer); |
| 6824 | req->Start(); |
| 6825 | base::RunLoop().Run(); |
| 6826 | |
| 6827 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 6828 | } |
| 6829 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6830 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6831 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6832 | |
| 6833 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6834 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6835 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6836 | req->SetReferrer("http://user:[email protected]/"); |
| 6837 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6838 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6839 | |
| 6840 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 6841 | } |
| 6842 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6843 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6844 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6845 | |
| 6846 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6847 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6848 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6849 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6850 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6851 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6852 | |
| 6853 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 6854 | } |
| 6855 | |
| 6856 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6857 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6858 | |
| 6859 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6860 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6861 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6862 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6863 | req->SetReferrer(""); |
| 6864 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6865 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6866 | |
| 6867 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 6868 | } |
| 6869 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6870 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6871 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6872 | |
| 6873 | TestDelegate d; |
| 6874 | { |
| 6875 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6876 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6877 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 6878 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6879 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6880 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6881 | |
| 6882 | EXPECT_EQ(1, d.response_started_count()); |
| 6883 | EXPECT_EQ(0, d.bytes_received()); |
| 6884 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6885 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6890 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6891 | |
| 6892 | TestDelegate d; |
| 6893 | { |
| 6894 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6895 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6896 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6897 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6898 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6899 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6900 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6901 | |
| 6902 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6903 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6904 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6905 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6906 | |
| 6907 | EXPECT_EQ(1, d.response_started_count()); |
| 6908 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6909 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6910 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6911 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6912 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6913 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6914 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6915 | |
| 6916 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6917 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6918 | EXPECT_EQ(contents, d.data_received()); |
| 6919 | } |
| 6920 | } |
| 6921 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6922 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6924 | |
| 6925 | TestDelegate d; |
| 6926 | { |
| 6927 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6928 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6929 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6930 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6931 | |
| 6932 | EXPECT_FALSE(d.have_full_request_headers()); |
| 6933 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6934 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6935 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6936 | |
| 6937 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6938 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6939 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 6940 | d.ClearFullRequestHeaders(); |
| 6941 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6942 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6943 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6944 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6945 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6946 | EXPECT_EQ(1, d.response_started_count()); |
| 6947 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6948 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 6949 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6950 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6951 | |
| 6952 | base::FilePath path; |
| 6953 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6954 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6955 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6956 | |
| 6957 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6958 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6959 | EXPECT_EQ(contents, d.data_received()); |
| 6960 | } |
| 6961 | } |
| 6962 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6963 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6964 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6965 | |
| 6966 | TestDelegate d; |
| 6967 | { |
| 6968 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6969 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6970 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 6971 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6972 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6973 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6974 | |
| 6975 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6977 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6978 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6979 | |
| 6980 | EXPECT_EQ(1, d.response_started_count()); |
| 6981 | EXPECT_EQ(0, d.bytes_received()); |
| 6982 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6983 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6984 | } |
| 6985 | } |
| 6986 | |
| 6987 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6988 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6989 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6990 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6991 | { |
| 6992 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6993 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6994 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 6995 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6996 | HttpRequestHeaders headers; |
| 6997 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6998 | req->SetExtraRequestHeaders(headers); |
| 6999 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7000 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7001 | |
| 7002 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7003 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7004 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7005 | } |
| 7006 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7007 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7008 | { |
| 7009 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7010 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7011 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 7012 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7013 | HttpRequestHeaders headers; |
| 7014 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7015 | req->SetExtraRequestHeaders(headers); |
| 7016 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7017 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7018 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7019 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7020 | |
| 7021 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7022 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7023 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7024 | } |
| 7025 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7026 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7027 | { |
| 7028 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7029 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7030 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 7031 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7032 | HttpRequestHeaders headers; |
| 7033 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7034 | req->SetExtraRequestHeaders(headers); |
| 7035 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7036 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7037 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7038 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7039 | |
| 7040 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7041 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7042 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7043 | } |
| 7044 | } |
| 7045 | |
| 7046 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7047 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7048 | |
| 7049 | // populate the cache |
| 7050 | { |
| 7051 | TestDelegate d; |
| 7052 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7053 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7054 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7055 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7056 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7057 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7058 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7059 | |
| 7060 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7061 | } |
| 7062 | |
| 7063 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7064 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7065 | // response should be fetched from the cache. |
| 7066 | { |
| 7067 | TestDelegate d; |
| 7068 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7069 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7070 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7071 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7072 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7073 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7074 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7075 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7076 | |
| 7077 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7078 | |
| 7079 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7080 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7085 | // http://crbug.com/6450 |
| 7086 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7087 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7088 | |
| 7089 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7090 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7091 | |
| 7092 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7093 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7094 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7095 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7096 | TestURLRequestContext context(true); |
| 7097 | context.set_network_delegate(&network_delegate); |
| 7098 | context.Init(); |
| 7099 | |
| 7100 | TestDelegate d; |
| 7101 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7102 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7103 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7104 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7105 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7106 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7107 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7108 | |
| 7109 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7110 | |
| 7111 | // Make sure we sent the cookie in the restarted transaction. |
| 7112 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7113 | != std::string::npos); |
| 7114 | } |
| 7115 | |
| 7116 | // Same test as above, except this time the restart is initiated earlier |
| 7117 | // (without user intervention since identity is embedded in the URL). |
| 7118 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7119 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7120 | TestURLRequestContext context(true); |
| 7121 | context.set_network_delegate(&network_delegate); |
| 7122 | context.Init(); |
| 7123 | |
| 7124 | TestDelegate d; |
| 7125 | |
| 7126 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 7127 | replacements.SetUsernameStr("user2"); |
| 7128 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7129 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 7130 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7131 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7132 | context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7133 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7134 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7135 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7136 | |
| 7137 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 7138 | |
| 7139 | // Make sure we sent the cookie in the restarted transaction. |
| 7140 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7141 | != std::string::npos); |
| 7142 | } |
| 7143 | } |
| 7144 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7145 | // Tests that load timing works as expected with auth and the cache. |
| 7146 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7147 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7148 | |
| 7149 | // populate the cache |
| 7150 | { |
| 7151 | TestDelegate d; |
| 7152 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7153 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7154 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7155 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7156 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7157 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7158 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7159 | |
| 7160 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7161 | |
| 7162 | LoadTimingInfo load_timing_info_before_auth; |
| 7163 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 7164 | &load_timing_info_before_auth)); |
| 7165 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 7166 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 7167 | |
| 7168 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7169 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7170 | // The test server does not support keep alive sockets, so the second |
| 7171 | // request with auth should use a new socket. |
| 7172 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 7173 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 7174 | load_timing_info.socket_log_id); |
| 7175 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 7176 | load_timing_info.connect_timing.connect_start); |
| 7177 | } |
| 7178 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7179 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 7180 | // 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] | 7181 | // response should be fetched from the cache. |
| 7182 | { |
| 7183 | TestDelegate d; |
| 7184 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7185 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7186 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7187 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7188 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7189 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7190 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7191 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7192 | |
| 7193 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7194 | |
| 7195 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7196 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7197 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7198 | // Since there was a request that went over the wire, the load timing |
| 7199 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7200 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7201 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7202 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7203 | } |
| 7204 | } |
| 7205 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7206 | // In this test, we do a POST which the server will 302 redirect. |
| 7207 | // The subsequent transaction should use GET, and should not send the |
| 7208 | // Content-Type header. |
| 7209 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 7210 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7211 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7212 | |
| 7213 | const char kData[] = "hello world"; |
| 7214 | |
| 7215 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7216 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7217 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, |
| 7218 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7219 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7220 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7221 | |
| 7222 | // Set headers (some of which are specific to the POST). |
| 7223 | HttpRequestHeaders headers; |
| 7224 | headers.AddHeadersFromString( |
| 7225 | "Content-Type: multipart/form-data; " |
| 7226 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 7227 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 7228 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 7229 | "Accept-Language: en-US,en\r\n" |
| 7230 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 7231 | "Content-Length: 11\r\n" |
| 7232 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7233 | req->SetExtraRequestHeaders(headers); |
| 7234 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7235 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7236 | |
| 7237 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7238 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7239 | EXPECT_EQ("text/html", mime_type); |
| 7240 | |
| 7241 | const std::string& data = d.data_received(); |
| 7242 | |
| 7243 | // Check that the post-specific headers were stripped: |
| 7244 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 7245 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 7246 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 7247 | |
| 7248 | // These extra request headers should not have been stripped. |
| 7249 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 7250 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 7251 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 7252 | } |
| 7253 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7254 | // The following tests check that we handle mutating the request for HTTP |
| 7255 | // redirects as expected. |
| 7256 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 7257 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7258 | |
| 7259 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7260 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7261 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7262 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7263 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7264 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7265 | |
| 7266 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7267 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7268 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7269 | |
| 7270 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7271 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7272 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7273 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7274 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7278 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7279 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7280 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7281 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7282 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7283 | |
| 7284 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7285 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7286 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7287 | |
| 7288 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7289 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7290 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7291 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7292 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7293 | } |
| 7294 | |
| 7295 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7296 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7297 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7298 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7299 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7300 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7301 | |
| 7302 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7303 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 7304 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7305 | |
| 7306 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7307 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7308 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7309 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7310 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7314 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7315 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7316 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7317 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7318 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7319 | |
| 7320 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7321 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7322 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7323 | |
| 7324 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7325 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7326 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7327 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7328 | } |
| 7329 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7330 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7331 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7332 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7333 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7334 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7335 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7336 | |
| 7337 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7338 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7339 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7340 | |
| 7341 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7342 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7343 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7344 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7345 | } |
| 7346 | |
| 7347 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 7348 | // Certain legacy apis that pre-date the response code expect this behavior |
| 7349 | // (Like Google Drive). |
| 7350 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7351 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7352 | |
| 7353 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7354 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7355 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7356 | std::unique_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7357 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7358 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7359 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7360 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7361 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7362 | EXPECT_THAT(request->status().error(), IsOk()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7363 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7364 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7365 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 7366 | } |
| 7367 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7368 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7369 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7370 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7371 | GURL original_url( |
| 7372 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 7373 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7374 | |
| 7375 | TestDelegate d; |
| 7376 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7377 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7378 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7379 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7380 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7381 | base::RunLoop().Run(); |
| 7382 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7383 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7384 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7385 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7386 | EXPECT_EQ(original_url, r->original_url()); |
| 7387 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7388 | } |
| 7389 | } |
| 7390 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7391 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7392 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7393 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7394 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7395 | GURL first_party_url("http://example.com"); |
| 7396 | |
| 7397 | TestDelegate d; |
| 7398 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7399 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7400 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7401 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7402 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7403 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7404 | base::RunLoop().Run(); |
| 7405 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7406 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7407 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7408 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7409 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7410 | } |
| 7411 | } |
| 7412 | |
| 7413 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7414 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7415 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7416 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7417 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7418 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7419 | |
| 7420 | TestDelegate d; |
| 7421 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7422 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7423 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7424 | r->set_first_party_for_cookies(original_first_party_url); |
| 7425 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7426 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 7427 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7428 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7429 | base::RunLoop().Run(); |
| 7430 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7431 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7432 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7433 | EXPECT_THAT(r->status().error(), IsOk()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7434 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7435 | } |
| 7436 | } |
| 7437 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7438 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7439 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7440 | |
| 7441 | const char kData[] = "hello world"; |
| 7442 | |
| 7443 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7444 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7445 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7446 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7447 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7448 | HttpRequestHeaders headers; |
| 7449 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7450 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7451 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7452 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7453 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7454 | req.get(), &default_network_delegate_, |
| 7455 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7456 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7457 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7458 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7459 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7460 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7461 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7465 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7466 | |
| 7467 | const char kData[] = "hello world"; |
| 7468 | |
| 7469 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7470 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7471 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7472 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7473 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7474 | HttpRequestHeaders headers; |
| 7475 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7476 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7477 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7478 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7479 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7480 | req.get(), &default_network_delegate_, |
| 7481 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 7482 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7483 | "Very Good Reason")); |
| 7484 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7485 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7486 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7487 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7488 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7489 | EXPECT_EQ(kData, d.data_received()); |
| 7490 | } |
| 7491 | |
| 7492 | // Check that default A-L header is sent. |
| 7493 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7494 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7495 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7496 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7497 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7498 | TestURLRequestContext context(true); |
| 7499 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7500 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7501 | context.Init(); |
| 7502 | |
| 7503 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7504 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7505 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7506 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7507 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7508 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7509 | EXPECT_EQ("en", d.data_received()); |
| 7510 | } |
| 7511 | |
| 7512 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 7513 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7514 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7515 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7516 | std::string empty_string; // Avoid most vexing parse on line below. |
| 7517 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7518 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7519 | TestURLRequestContext context(true); |
| 7520 | context.set_network_delegate(&network_delegate); |
| 7521 | context.Init(); |
| 7522 | // We override the language after initialization because empty entries |
| 7523 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7524 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7525 | |
| 7526 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7527 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7528 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7529 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7530 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7531 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7532 | EXPECT_EQ("None", d.data_received()); |
| 7533 | } |
| 7534 | |
| 7535 | // Check that if request overrides the A-L header, the default is not appended. |
| 7536 | // See http://crbug.com/20894 |
| 7537 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7538 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7539 | |
| 7540 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7541 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7542 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7543 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7544 | HttpRequestHeaders headers; |
| 7545 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7546 | req->SetExtraRequestHeaders(headers); |
| 7547 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7548 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7549 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 7550 | } |
| 7551 | |
| 7552 | // Check that default A-E header is sent. |
| 7553 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7554 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 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("/echoheader?Accept-Encoding"), |
| 7559 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7560 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7561 | req->SetExtraRequestHeaders(headers); |
| 7562 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7563 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7564 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 7565 | } |
| 7566 | |
| 7567 | // Check that if request overrides the A-E header, the default is not appended. |
| 7568 | // See http://crbug.com/47381 |
| 7569 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7570 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7571 | |
| 7572 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7573 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7574 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
| 7575 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7576 | HttpRequestHeaders headers; |
| 7577 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7578 | req->SetExtraRequestHeaders(headers); |
| 7579 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7580 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7581 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 7582 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 7583 | } |
| 7584 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 7585 | // Check that setting the A-C header sends the proper header. |
| 7586 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7587 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7588 | |
| 7589 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7590 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7591 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
| 7592 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7593 | HttpRequestHeaders headers; |
| 7594 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7595 | req->SetExtraRequestHeaders(headers); |
| 7596 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7597 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7598 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 7599 | } |
| 7600 | |
| 7601 | // Check that default User-Agent header is sent. |
| 7602 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7603 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7604 | |
| 7605 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7606 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7607 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7608 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7609 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7610 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7611 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 7612 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7613 | } |
| 7614 | |
| 7615 | // Check that if request overrides the User-Agent header, |
| 7616 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 7617 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 7618 | #if defined(OS_IOS) |
| 7619 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 7620 | #else |
| 7621 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 7622 | #endif |
| 7623 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7624 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7625 | |
| 7626 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7627 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7628 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7629 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7630 | HttpRequestHeaders headers; |
| 7631 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7632 | req->SetExtraRequestHeaders(headers); |
| 7633 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7634 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 7635 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7636 | } |
| 7637 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7638 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 7639 | // User-Agent header to be sent but does not send the Accept-Language and |
| 7640 | // Accept-Charset headers. |
| 7641 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7642 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7643 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7644 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7645 | TestURLRequestContext context(true); |
| 7646 | context.set_network_delegate(&network_delegate); |
| 7647 | context.Init(); |
| 7648 | // We override the HttpUserAgentSettings after initialization because empty |
| 7649 | // entries get overridden by Init(). |
| 7650 | context.set_http_user_agent_settings(NULL); |
| 7651 | |
| 7652 | struct { |
| 7653 | const char* request; |
| 7654 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7655 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 7656 | {"/echoheader?Accept-Charset", "None"}, |
| 7657 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7658 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 7659 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7660 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7661 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7662 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7663 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7664 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7665 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 7666 | << " Request = \"" << tests[i].request << "\""; |
| 7667 | } |
| 7668 | } |
| 7669 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7670 | // Make sure that URLRequest passes on its priority updates to |
| 7671 | // newly-created jobs after the first one. |
| 7672 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7673 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7674 | |
| 7675 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7676 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7677 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7678 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7679 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7680 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7681 | req.get(), &default_network_delegate_, |
| 7682 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7683 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7684 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7685 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7686 | req->SetPriority(LOW); |
| 7687 | req->Start(); |
| 7688 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7689 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7690 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7691 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7692 | req.get(), &default_network_delegate_, &job_priority)); |
| 7693 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7694 | |
| 7695 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7696 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7697 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7698 | } |
| 7699 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7700 | // Check that creating a network request while entering/exiting suspend mode |
| 7701 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 7702 | // does not return an HttpTransaction. |
| 7703 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 7704 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7705 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7706 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7707 | network_layer->OnSuspend(); |
| 7708 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7709 | HttpCache http_cache(std::move(network_layer), |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 7710 | HttpCache::DefaultBackend::InMemory(0), true); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7711 | |
| 7712 | TestURLRequestContext context(true); |
| 7713 | context.set_http_transaction_factory(&http_cache); |
| 7714 | context.Init(); |
| 7715 | |
| 7716 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7717 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7718 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7719 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7720 | base::RunLoop().Run(); |
| 7721 | |
| 7722 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7723 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7724 | EXPECT_THAT(req->status().error(), IsError(ERR_NETWORK_IO_SUSPENDED)); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7725 | } |
| 7726 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7727 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7728 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7729 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 7730 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 7731 | public: |
| 7732 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 7733 | : network_session_(network_session) {} |
| 7734 | |
| 7735 | ~FailingHttpTransactionFactory() override {} |
| 7736 | |
| 7737 | // HttpTransactionFactory methods: |
| 7738 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7739 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7740 | return ERR_FAILED; |
| 7741 | } |
| 7742 | |
| 7743 | HttpCache* GetCache() override { return nullptr; } |
| 7744 | |
| 7745 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 7746 | |
| 7747 | private: |
| 7748 | HttpNetworkSession* network_session_; |
| 7749 | |
| 7750 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 7751 | }; |
| 7752 | |
| 7753 | } // namespace |
| 7754 | |
| 7755 | // Check that when a request that fails to create an HttpTransaction can be |
| 7756 | // cancelled while the failure notification is pending, and doesn't send two |
| 7757 | // failure notifications. |
| 7758 | // |
| 7759 | // This currently only happens when in suspend mode and there's no cache, but |
| 7760 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 7761 | // behaviors. |
| 7762 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 7763 | FailingHttpTransactionFactory http_transaction_factory( |
| 7764 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7765 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7766 | context.set_http_transaction_factory(&http_transaction_factory); |
| 7767 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7768 | context.Init(); |
| 7769 | |
| 7770 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7771 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7772 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7773 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 7774 | // try to create an HttpNetworkTransaction synchronously on start). |
| 7775 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7776 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7777 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7778 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7779 | // Run pending error task, if there is one. |
| 7780 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7781 | |
| 7782 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7783 | EXPECT_EQ(1, d.response_started_count()); |
| 7784 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 7785 | |
| 7786 | // NetworkDelegate should see the cancellation, but not the error. |
| 7787 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 7788 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7789 | } |
| 7790 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7791 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7792 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7793 | |
| 7794 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7795 | GURL test_url(http_test_server()->GetURL("/")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7796 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7797 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7798 | |
| 7799 | req->Start(); |
| 7800 | base::RunLoop().Run(); |
| 7801 | |
| 7802 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7803 | } |
| 7804 | |
| 7805 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7806 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7807 | |
| 7808 | // Populate the cache. |
| 7809 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7810 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7811 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7812 | req->Start(); |
| 7813 | base::RunLoop().Run(); |
| 7814 | |
| 7815 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7816 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7817 | EXPECT_FALSE(req->response_info().was_cached); |
| 7818 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7819 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7820 | DEFAULT_PRIORITY, &d); |
| 7821 | req->Start(); |
| 7822 | base::RunLoop().Run(); |
| 7823 | |
| 7824 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7825 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7826 | EXPECT_TRUE(req->response_info().was_cached); |
| 7827 | } |
| 7828 | |
| 7829 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7830 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7831 | |
| 7832 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7833 | GURL test_url(http_test_server()->GetURL("/")); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7834 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7835 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7836 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE); |
| 7837 | |
| 7838 | req->Start(); |
| 7839 | base::RunLoop().Run(); |
| 7840 | |
| 7841 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7842 | } |
| 7843 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 7844 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 7845 | ASSERT_TRUE(http_test_server()->Start()); |
| 7846 | |
| 7847 | TestDelegate d; |
| 7848 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 7849 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
| 7850 | req->Start(); |
| 7851 | base::RunLoop().Run(); |
| 7852 | |
| 7853 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 7854 | } |
| 7855 | |
| 7856 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 7857 | ASSERT_TRUE(http_test_server()->Start()); |
| 7858 | |
| 7859 | TestDelegate d; |
| 7860 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 7861 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d)); |
| 7862 | req->Start(); |
| 7863 | base::RunLoop().Run(); |
| 7864 | |
| 7865 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 7866 | } |
| 7867 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7868 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 7869 | public: |
| 7870 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 7871 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 7872 | // tests into the factory tests. |
| 7873 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 7874 | } |
| 7875 | |
| 7876 | void SetUpFactory() override { |
| 7877 | interceptor_ = new MockURLRequestInterceptor(); |
| 7878 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7879 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7880 | } |
| 7881 | |
| 7882 | MockURLRequestInterceptor* interceptor() const { |
| 7883 | return interceptor_; |
| 7884 | } |
| 7885 | |
| 7886 | private: |
| 7887 | MockURLRequestInterceptor* interceptor_; |
| 7888 | }; |
| 7889 | |
| 7890 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7891 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 7892 | interceptor()->set_intercept_redirect(true); |
| 7893 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 7894 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 7895 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7896 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7897 | |
| 7898 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7899 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7900 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7901 | req->Start(); |
| 7902 | base::RunLoop().Run(); |
| 7903 | |
| 7904 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 7905 | // Check we got one good response |
| 7906 | EXPECT_TRUE(req->status().is_success()); |
| 7907 | if (req->status().is_success()) |
| 7908 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7909 | |
| 7910 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 7911 | EXPECT_EQ(1, d.response_started_count()); |
| 7912 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7913 | |
| 7914 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 7915 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7916 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 7917 | } |
| 7918 | |
| 7919 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7920 | NetworkDelegateNotificationOnErrorIntercept) { |
| 7921 | // Intercept that error and respond with an OK response. |
| 7922 | interceptor()->set_intercept_final_response(true); |
| 7923 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 7924 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 7925 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 7926 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7927 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7928 | |
| 7929 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7930 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7931 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7932 | &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7933 | req->set_method("GET"); |
| 7934 | req->Start(); |
| 7935 | base::RunLoop().Run(); |
| 7936 | |
| 7937 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 7938 | |
| 7939 | // Check we received one good response. |
| 7940 | EXPECT_TRUE(req->status().is_success()); |
| 7941 | if (req->status().is_success()) |
| 7942 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7943 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 7944 | EXPECT_EQ(1, d.response_started_count()); |
| 7945 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7946 | |
| 7947 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 7948 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7949 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 7950 | } |
| 7951 | |
| 7952 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7953 | NetworkDelegateNotificationOnResponseIntercept) { |
| 7954 | // Intercept that error and respond with an OK response. |
| 7955 | interceptor()->set_intercept_final_response(true); |
| 7956 | |
| 7957 | // Intercept with a real URLRequestHttpJob. |
| 7958 | interceptor()->set_use_url_request_http_job(true); |
| 7959 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7960 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7961 | |
| 7962 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7963 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7964 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7965 | req->set_method("GET"); |
| 7966 | req->Start(); |
| 7967 | base::RunLoop().Run(); |
| 7968 | |
| 7969 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 7970 | |
| 7971 | // Check we received one good response. |
| 7972 | EXPECT_TRUE(req->status().is_success()); |
| 7973 | if (req->status().is_success()) |
| 7974 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7975 | EXPECT_EQ("hello", d.data_received()); |
| 7976 | EXPECT_EQ(1, d.response_started_count()); |
| 7977 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7978 | |
| 7979 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 7980 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7981 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 7982 | } |
| 7983 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7984 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 7985 | public: |
| 7986 | URLRequestTestReferrerPolicy() {} |
| 7987 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7988 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 7989 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 7990 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 7991 | origin_server_->AddDefaultHandlers( |
| 7992 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7993 | } else { |
| 7994 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7995 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7996 | ASSERT_TRUE(origin_server_->Start()); |
| 7997 | } |
| 7998 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7999 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 8000 | net::EmbeddedTestServer::Type dest_type) { |
| 8001 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 8002 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8003 | origin_server_->AddDefaultHandlers( |
| 8004 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8005 | } else { |
| 8006 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8007 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8008 | ASSERT_TRUE(origin_server_->Start()); |
| 8009 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8010 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 8011 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8012 | destination_server_->AddDefaultHandlers( |
| 8013 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8014 | } else { |
| 8015 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8016 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8017 | ASSERT_TRUE(destination_server_->Start()); |
| 8018 | } |
| 8019 | |
| 8020 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 8021 | const GURL& referrer, |
| 8022 | const GURL& expected) { |
| 8023 | // Create and execute the request: we'll only have a |destination_server_| |
| 8024 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 8025 | // |origin_server_| for both endpoints. |
| 8026 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8027 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 8028 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8029 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8030 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8031 | |
| 8032 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8033 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8034 | default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8035 | req->set_referrer_policy(policy); |
| 8036 | req->SetReferrer(referrer.spec()); |
| 8037 | req->Start(); |
| 8038 | base::RunLoop().Run(); |
| 8039 | |
| 8040 | EXPECT_EQ(1, d.response_started_count()); |
| 8041 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8042 | EXPECT_EQ(destination_url, req->url()); |
| 8043 | EXPECT_TRUE(req->status().is_success()); |
| 8044 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8045 | |
| 8046 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 8047 | if (expected.is_empty()) |
| 8048 | EXPECT_EQ("None", d.data_received()); |
| 8049 | else |
| 8050 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 8051 | } |
| 8052 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8053 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8054 | |
| 8055 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8056 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 8057 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8058 | }; |
| 8059 | |
| 8060 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8061 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8062 | |
| 8063 | VerifyReferrerAfterRedirect( |
| 8064 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8065 | origin_server()->GetURL("/path/to/file.html"), |
| 8066 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8067 | |
| 8068 | VerifyReferrerAfterRedirect( |
| 8069 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8070 | origin_server()->GetURL("/path/to/file.html"), |
| 8071 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8072 | |
| 8073 | VerifyReferrerAfterRedirect( |
| 8074 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8075 | origin_server()->GetURL("/path/to/file.html"), |
| 8076 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8077 | |
| 8078 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8079 | origin_server()->GetURL("/path/to/file.html"), |
| 8080 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8081 | } |
| 8082 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8083 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 8084 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 8085 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8086 | |
| 8087 | VerifyReferrerAfterRedirect( |
| 8088 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8089 | origin_server()->GetURL("/path/to/file.html"), |
| 8090 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8091 | |
| 8092 | VerifyReferrerAfterRedirect( |
| 8093 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8094 | origin_server()->GetURL("/path/to/file.html"), |
| 8095 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8096 | |
| 8097 | VerifyReferrerAfterRedirect( |
| 8098 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8099 | origin_server()->GetURL("/path/to/file.html"), |
| 8100 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8101 | |
| 8102 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8103 | origin_server()->GetURL("/path/to/file.html"), |
| 8104 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8105 | } |
| 8106 | |
| 8107 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8108 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8109 | |
| 8110 | VerifyReferrerAfterRedirect( |
| 8111 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8112 | origin_server()->GetURL("/path/to/file.html"), |
| 8113 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8114 | |
| 8115 | VerifyReferrerAfterRedirect( |
| 8116 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8117 | origin_server()->GetURL("/path/to/file.html"), |
| 8118 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8119 | |
| 8120 | VerifyReferrerAfterRedirect( |
| 8121 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8122 | origin_server()->GetURL("/path/to/file.html"), |
| 8123 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8124 | |
| 8125 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8126 | origin_server()->GetURL("/path/to/file.html"), |
| 8127 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8128 | } |
| 8129 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8130 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 8131 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 8132 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8133 | |
| 8134 | VerifyReferrerAfterRedirect( |
| 8135 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8136 | origin_server()->GetURL("/path/to/file.html"), |
| 8137 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8138 | |
| 8139 | VerifyReferrerAfterRedirect( |
| 8140 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8141 | origin_server()->GetURL("/path/to/file.html"), |
| 8142 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8143 | |
| 8144 | VerifyReferrerAfterRedirect( |
| 8145 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8146 | origin_server()->GetURL("/path/to/file.html"), |
| 8147 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8148 | |
| 8149 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8150 | origin_server()->GetURL("/path/to/file.html"), |
| 8151 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8152 | } |
| 8153 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8154 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 8155 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 8156 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8157 | |
| 8158 | VerifyReferrerAfterRedirect( |
| 8159 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8160 | origin_server()->GetURL("/path/to/file.html"), |
| 8161 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8162 | |
| 8163 | VerifyReferrerAfterRedirect( |
| 8164 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8165 | origin_server()->GetURL("/path/to/file.html"), |
| 8166 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8167 | |
| 8168 | VerifyReferrerAfterRedirect( |
| 8169 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8170 | origin_server()->GetURL("/path/to/file.html"), |
| 8171 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8172 | |
| 8173 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8174 | origin_server()->GetURL("/path/to/file.html"), |
| 8175 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8176 | } |
| 8177 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8178 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 8179 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 8180 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8181 | |
| 8182 | VerifyReferrerAfterRedirect( |
| 8183 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8184 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8185 | |
| 8186 | VerifyReferrerAfterRedirect( |
| 8187 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8188 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8189 | |
| 8190 | VerifyReferrerAfterRedirect( |
| 8191 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8192 | origin_server()->GetURL("/path/to/file.html"), |
| 8193 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8194 | |
| 8195 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8196 | origin_server()->GetURL("/path/to/file.html"), |
| 8197 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8198 | } |
| 8199 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 8200 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8201 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8202 | HTTPSRequestTest() : default_context_(true) { |
| 8203 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8204 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8205 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8206 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 8207 | |
| 8208 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8209 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8210 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8211 | }; |
| 8212 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 8213 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8214 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8215 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8216 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8217 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8218 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8219 | TestDelegate d; |
| 8220 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8221 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8222 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8223 | r->Start(); |
| 8224 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8225 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8226 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8227 | |
| 8228 | EXPECT_EQ(1, d.response_started_count()); |
| 8229 | EXPECT_FALSE(d.received_data_before_response()); |
| 8230 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8231 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8232 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8233 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8234 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8235 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8236 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8237 | } |
| 8238 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 8239 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8240 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8241 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8242 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8243 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8244 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8245 | |
| 8246 | bool err_allowed = true; |
| 8247 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 8248 | TestDelegate d; |
| 8249 | { |
| 8250 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8251 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8252 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8253 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8254 | r->Start(); |
| 8255 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8256 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8257 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8258 | |
| 8259 | EXPECT_EQ(1, d.response_started_count()); |
| 8260 | EXPECT_FALSE(d.received_data_before_response()); |
| 8261 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8262 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8263 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8264 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8265 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8266 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8267 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8268 | } |
| 8269 | } |
| 8270 | } |
| 8271 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 8272 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8273 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8274 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 8275 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8276 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8277 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8278 | |
| 8279 | // Iterate from false to true, just so that we do the opposite of the |
| 8280 | // previous test in order to increase test coverage. |
| 8281 | bool err_allowed = false; |
| 8282 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 8283 | TestDelegate d; |
| 8284 | { |
| 8285 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8286 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8287 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8288 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8289 | r->Start(); |
| 8290 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8291 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8292 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8293 | |
| 8294 | EXPECT_EQ(1, d.response_started_count()); |
| 8295 | EXPECT_FALSE(d.received_data_before_response()); |
| 8296 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8297 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8298 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8299 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8300 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8301 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8302 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8303 | } |
| 8304 | } |
| 8305 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 8306 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8307 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 8308 | // security state. (see http://crbug.com/550977). |
| 8309 | #if !defined(OS_IOS) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8310 | // This tests that a load of www.google.com with a certificate error sets |
| 8311 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 8312 | // the interstitial to be fatal. |
| 8313 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8314 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8315 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8316 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8317 | ASSERT_TRUE(test_server.Start()); |
| 8318 | |
| 8319 | // We require that the URL be www.google.com in order to pick up the |
| 8320 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 8321 | // 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] | 8322 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8323 | |
| 8324 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8325 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8326 | TestURLRequestContext context(true); |
| 8327 | context.set_network_delegate(&network_delegate); |
| 8328 | context.set_host_resolver(&host_resolver); |
| 8329 | TransportSecurityState transport_security_state; |
| 8330 | context.set_transport_security_state(&transport_security_state); |
| 8331 | context.Init(); |
| 8332 | |
| 8333 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8334 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8335 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8336 | test_server.host_port_pair().port())), |
| 8337 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8338 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8339 | r->Start(); |
| 8340 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8341 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8342 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8343 | |
| 8344 | EXPECT_EQ(1, d.response_started_count()); |
| 8345 | EXPECT_FALSE(d.received_data_before_response()); |
| 8346 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8347 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8348 | } |
| 8349 | |
| 8350 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 8351 | // TransportSecurityState. |
| 8352 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 8353 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 8354 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8355 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8356 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8357 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8358 | ASSERT_TRUE(test_server.Start()); |
| 8359 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8360 | // We require that the URL be www.google.com in order to pick up the static |
| 8361 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 8362 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 8363 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8364 | |
| 8365 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8366 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8367 | TestURLRequestContext context(true); |
| 8368 | context.set_network_delegate(&network_delegate); |
| 8369 | context.set_host_resolver(&host_resolver); |
| 8370 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8371 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8372 | TransportSecurityState::STSState static_sts_state; |
| 8373 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8374 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8375 | "www.google.com", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8376 | context.set_transport_security_state(&transport_security_state); |
| 8377 | context.Init(); |
| 8378 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8379 | TransportSecurityState::STSState dynamic_sts_state; |
| 8380 | TransportSecurityState::PKPState dynamic_pkp_state; |
| 8381 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState("www.google.com", |
| 8382 | &dynamic_sts_state)); |
| 8383 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState("www.google.com", |
| 8384 | &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8385 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8386 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8387 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8388 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8389 | test_server.host_port_pair().port())), |
| 8390 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8391 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8392 | r->Start(); |
| 8393 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8394 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8395 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8396 | |
| 8397 | EXPECT_EQ(1, d.response_started_count()); |
| 8398 | EXPECT_FALSE(d.received_data_before_response()); |
| 8399 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8400 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8401 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8402 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8403 | TransportSecurityState::STSState new_static_sts_state; |
| 8404 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8405 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8406 | "www.google.com", &new_static_sts_state, &new_static_pkp_state)); |
| 8407 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 8408 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 8409 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 8410 | "www.google.com", &new_dynamic_sts_state)); |
| 8411 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 8412 | "www.google.com", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8413 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8414 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 8415 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 8416 | static_sts_state.include_subdomains); |
| 8417 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 8418 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 8419 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 8420 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 8421 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8422 | } |
| 8423 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8424 | // Make sure HSTS preserves a POST request's method and body. |
| 8425 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 8426 | static const char kData[] = "hello world"; |
| 8427 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8428 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8429 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8430 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8431 | ASSERT_TRUE(test_server.Start()); |
| 8432 | |
| 8433 | |
| 8434 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8435 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8436 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8437 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8438 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8439 | |
| 8440 | // Force https for www.somewhere.com. |
| 8441 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 8442 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 8443 | bool include_subdomains = false; |
| 8444 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 8445 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8446 | |
| 8447 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8448 | |
| 8449 | TestURLRequestContext context(true); |
| 8450 | context.set_host_resolver(&host_resolver); |
| 8451 | context.set_transport_security_state(&transport_security_state); |
| 8452 | context.set_network_delegate(&network_delegate); |
| 8453 | context.Init(); |
| 8454 | |
| 8455 | TestDelegate d; |
| 8456 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 8457 | // cause a certificate error. Ignore the error. |
| 8458 | d.set_allow_certificate_errors(true); |
| 8459 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8460 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8461 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8462 | test_server.host_port_pair().port())), |
| 8463 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8464 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8465 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8466 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8467 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8468 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8469 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8470 | EXPECT_EQ("https", req->url().scheme()); |
| 8471 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8472 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 8473 | |
| 8474 | LoadTimingInfo load_timing_info; |
| 8475 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 8476 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 8477 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8478 | } |
| 8479 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8480 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 8481 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 8482 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 8483 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8484 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8485 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8486 | ASSERT_TRUE(test_server.Start()); |
| 8487 | |
| 8488 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8489 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8490 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 8491 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8492 | MockHostResolver host_resolver; |
| 8493 | |
| 8494 | TransportSecurityState transport_security_state; |
| 8495 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 8496 | bool include_subdomains = false; |
| 8497 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 8498 | |
| 8499 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8500 | |
| 8501 | MockCertVerifier cert_verifier; |
| 8502 | cert_verifier.set_default_result(OK); |
| 8503 | |
| 8504 | TestURLRequestContext context(true); |
| 8505 | context.set_host_resolver(&host_resolver); |
| 8506 | context.set_transport_security_state(&transport_security_state); |
| 8507 | context.set_network_delegate(&network_delegate); |
| 8508 | context.set_cert_verifier(&cert_verifier); |
| 8509 | context.Init(); |
| 8510 | |
| 8511 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 8512 | test_server.host_port_pair().port())); |
| 8513 | url::Replacements<char> replacements; |
| 8514 | const char kNewScheme[] = "https"; |
| 8515 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 8516 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 8517 | |
| 8518 | TestDelegate d; |
| 8519 | // Quit on redirect to allow response header inspection upon redirect. |
| 8520 | d.set_quit_on_redirect(true); |
| 8521 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8522 | std::unique_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8523 | context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8524 | // Set Origin header to simulate a cross-origin request. |
| 8525 | HttpRequestHeaders request_headers; |
| 8526 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 8527 | req->SetExtraRequestHeaders(request_headers); |
| 8528 | |
| 8529 | req->Start(); |
| 8530 | base::RunLoop().Run(); |
| 8531 | |
| 8532 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8533 | |
| 8534 | const HttpResponseHeaders* headers = req->response_headers(); |
| 8535 | std::string redirect_location; |
| 8536 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 8537 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 8538 | |
| 8539 | std::string received_cors_header; |
| 8540 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 8541 | &received_cors_header)); |
| 8542 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 8543 | } |
| 8544 | |
davidben | b4c25b6 | 2016-06-24 02:39:27 | [diff] [blame] | 8545 | // Test that DHE-only servers fail with the expected dedicated error code. |
| 8546 | TEST_F(HTTPSRequestTest, DHE) { |
| 8547 | SpawnedTestServer::SSLOptions ssl_options; |
| 8548 | ssl_options.key_exchanges = |
| 8549 | SpawnedTestServer::SSLOptions::KEY_EXCHANGE_DHE_RSA; |
| 8550 | SpawnedTestServer test_server( |
| 8551 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 8552 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8553 | ASSERT_TRUE(test_server.Start()); |
| 8554 | |
| 8555 | TestDelegate d; |
| 8556 | { |
| 8557 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8558 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
| 8559 | |
| 8560 | r->Start(); |
| 8561 | EXPECT_TRUE(r->is_pending()); |
| 8562 | |
| 8563 | base::RunLoop().Run(); |
| 8564 | |
| 8565 | EXPECT_EQ(1, d.response_started_count()); |
| 8566 | EXPECT_FALSE(r->status().is_success()); |
| 8567 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8568 | EXPECT_THAT(r->status().error(), IsError(ERR_SSL_OBSOLETE_CIPHER)); |
davidben | b4c25b6 | 2016-06-24 02:39:27 | [diff] [blame] | 8569 | } |
| 8570 | } |
| 8571 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8572 | namespace { |
| 8573 | |
| 8574 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 8575 | public: |
| 8576 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 8577 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8578 | void OnCertificateRequested(URLRequest* request, |
| 8579 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8580 | on_certificate_requested_count_++; |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 8581 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8582 | } |
| 8583 | int on_certificate_requested_count() { |
| 8584 | return on_certificate_requested_count_; |
| 8585 | } |
| 8586 | private: |
| 8587 | int on_certificate_requested_count_; |
| 8588 | }; |
| 8589 | |
| 8590 | } // namespace |
| 8591 | |
| 8592 | // TODO(davidben): Test the rest of the code. Specifically, |
| 8593 | // - Filtering which certificates to select. |
| 8594 | // - Sending a certificate back. |
| 8595 | // - Getting a certificate request in an SSL renegotiation sending the |
| 8596 | // HTTP request. |
| 8597 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8598 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8599 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 8600 | ssl_config.client_cert_type = |
| 8601 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8602 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 8603 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8604 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8605 | ASSERT_TRUE(test_server.Start()); |
| 8606 | |
| 8607 | SSLClientAuthTestDelegate d; |
| 8608 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8609 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8610 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8611 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8612 | r->Start(); |
| 8613 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8614 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8615 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8616 | |
| 8617 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 8618 | EXPECT_FALSE(d.received_data_before_response()); |
| 8619 | EXPECT_EQ(0, d.bytes_received()); |
| 8620 | |
| 8621 | // Send no certificate. |
| 8622 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 8623 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 8624 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8625 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8626 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8627 | |
| 8628 | EXPECT_EQ(1, d.response_started_count()); |
| 8629 | EXPECT_FALSE(d.received_data_before_response()); |
| 8630 | EXPECT_NE(0, d.bytes_received()); |
| 8631 | } |
| 8632 | } |
| 8633 | |
| 8634 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 8635 | // Test that we attempt a session resume when making two connections to the |
| 8636 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8637 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8638 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8639 | SpawnedTestServer test_server( |
| 8640 | SpawnedTestServer::TYPE_HTTPS, |
| 8641 | ssl_options, |
| 8642 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8643 | ASSERT_TRUE(test_server.Start()); |
| 8644 | |
| 8645 | SSLClientSocket::ClearSessionCache(); |
| 8646 | |
| 8647 | { |
| 8648 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8649 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8650 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8651 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8652 | r->Start(); |
| 8653 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8654 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8655 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8656 | |
| 8657 | EXPECT_EQ(1, d.response_started_count()); |
| 8658 | } |
| 8659 | |
| 8660 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8661 | CloseAllConnections(); |
| 8662 | |
| 8663 | { |
| 8664 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8665 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8666 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8667 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8668 | r->Start(); |
| 8669 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8670 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8671 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8672 | |
| 8673 | // The response will look like; |
| 8674 | // insert abc |
| 8675 | // lookup abc |
| 8676 | // insert xyz |
| 8677 | // |
| 8678 | // With a newline at the end which makes the split think that there are |
| 8679 | // four lines. |
| 8680 | |
| 8681 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8682 | std::vector<std::string> lines = base::SplitString( |
| 8683 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8684 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 8685 | |
| 8686 | std::string session_id; |
| 8687 | |
| 8688 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8689 | std::vector<std::string> parts = base::SplitString( |
| 8690 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8691 | ASSERT_EQ(2u, parts.size()); |
| 8692 | if (i == 0) { |
| 8693 | EXPECT_EQ("insert", parts[0]); |
| 8694 | session_id = parts[1]; |
| 8695 | } else { |
| 8696 | EXPECT_EQ("lookup", parts[0]); |
| 8697 | EXPECT_EQ(session_id, parts[1]); |
| 8698 | } |
| 8699 | } |
| 8700 | } |
| 8701 | } |
| 8702 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8703 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 8704 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 8705 | // that exactly two different sessions were inserted, with no lookups etc. |
| 8706 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8707 | std::vector<std::string> lines = base::SplitString( |
| 8708 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8709 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 8710 | |
| 8711 | std::string session_id; |
| 8712 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8713 | std::vector<std::string> parts = base::SplitString( |
| 8714 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8715 | ASSERT_EQ(2u, parts.size()); |
| 8716 | EXPECT_EQ("insert", parts[0]); |
| 8717 | if (i == 0) { |
| 8718 | session_id = parts[1]; |
| 8719 | } else { |
| 8720 | EXPECT_NE(session_id, parts[1]); |
| 8721 | } |
| 8722 | } |
| 8723 | } |
| 8724 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8725 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 8726 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 8727 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8728 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8729 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8730 | SpawnedTestServer test_server( |
| 8731 | SpawnedTestServer::TYPE_HTTPS, |
| 8732 | ssl_options, |
| 8733 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8734 | ASSERT_TRUE(test_server.Start()); |
| 8735 | |
| 8736 | SSLClientSocket::ClearSessionCache(); |
| 8737 | |
| 8738 | { |
| 8739 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8740 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8741 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8742 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8743 | r->Start(); |
| 8744 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8745 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8746 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8747 | |
| 8748 | EXPECT_EQ(1, d.response_started_count()); |
| 8749 | } |
| 8750 | |
| 8751 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 8752 | HttpNetworkSession::Params params; |
| 8753 | params.host_resolver = default_context_.host_resolver(); |
| 8754 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 8755 | params.transport_security_state = default_context_.transport_security_state(); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 8756 | params.cert_transparency_verifier = |
| 8757 | default_context_.cert_transparency_verifier(); |
| 8758 | params.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8759 | params.proxy_service = default_context_.proxy_service(); |
| 8760 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 8761 | params.http_auth_handler_factory = |
| 8762 | default_context_.http_auth_handler_factory(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8763 | params.http_server_properties = default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8764 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8765 | HttpNetworkSession network_session(params); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8766 | std::unique_ptr<HttpCache> cache(new HttpCache( |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8767 | &network_session, HttpCache::DefaultBackend::InMemory(0), false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8768 | |
| 8769 | default_context_.set_http_transaction_factory(cache.get()); |
| 8770 | |
| 8771 | { |
| 8772 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8773 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8774 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8775 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8776 | r->Start(); |
| 8777 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8778 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8779 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8780 | |
| 8781 | // The response will look like; |
| 8782 | // insert abc |
| 8783 | // insert xyz |
| 8784 | // |
| 8785 | // With a newline at the end which makes the split think that there are |
| 8786 | // three lines. |
| 8787 | |
| 8788 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8789 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8790 | } |
| 8791 | } |
| 8792 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8793 | class HTTPSFallbackTest : public testing::Test { |
| 8794 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8795 | HTTPSFallbackTest() : context_(true) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8796 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8797 | |
| 8798 | protected: |
| 8799 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 8800 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8801 | context_.Init(); |
| 8802 | delegate_.set_allow_certificate_errors(true); |
| 8803 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8804 | SpawnedTestServer test_server( |
| 8805 | SpawnedTestServer::TYPE_HTTPS, |
| 8806 | ssl_options, |
| 8807 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8808 | ASSERT_TRUE(test_server.Start()); |
| 8809 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8810 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 8811 | &delegate_); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8812 | request_->Start(); |
| 8813 | |
| 8814 | base::RunLoop().Run(); |
| 8815 | } |
| 8816 | |
| 8817 | void ExpectConnection(int version) { |
| 8818 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8819 | EXPECT_NE(0, delegate_.bytes_received()); |
| 8820 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 8821 | request_->ssl_info().connection_status)); |
| 8822 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 8823 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8824 | } |
| 8825 | |
| 8826 | void ExpectFailure(int error) { |
| 8827 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8828 | EXPECT_FALSE(request_->status().is_success()); |
| 8829 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 8830 | EXPECT_EQ(error, request_->status().error()); |
| 8831 | } |
| 8832 | |
| 8833 | private: |
| 8834 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 8835 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8836 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8837 | }; |
| 8838 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8839 | // Tests the TLS 1.0 fallback doesn't happen. |
| 8840 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8841 | SpawnedTestServer::SSLOptions ssl_options( |
| 8842 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8843 | ssl_options.tls_intolerant = |
| 8844 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 8845 | |
| 8846 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 8847 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8848 | } |
| 8849 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 8850 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 8851 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8852 | SpawnedTestServer::SSLOptions ssl_options( |
| 8853 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8854 | ssl_options.tls_intolerant = |
| 8855 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 8856 | |
| 8857 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8858 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 8859 | } |
| 8860 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8861 | class HTTPSSessionTest : public testing::Test { |
| 8862 | public: |
| 8863 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8864 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8865 | |
| 8866 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8867 | default_context_.set_cert_verifier(&cert_verifier_); |
| 8868 | default_context_.Init(); |
| 8869 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8870 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8871 | |
| 8872 | protected: |
| 8873 | MockCertVerifier cert_verifier_; |
| 8874 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 8875 | TestURLRequestContext default_context_; |
| 8876 | }; |
| 8877 | |
| 8878 | // Tests that session resumption is not attempted if an invalid certificate |
| 8879 | // is presented. |
| 8880 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 8881 | SpawnedTestServer::SSLOptions ssl_options; |
| 8882 | ssl_options.record_resume = true; |
| 8883 | SpawnedTestServer test_server( |
| 8884 | SpawnedTestServer::TYPE_HTTPS, |
| 8885 | ssl_options, |
| 8886 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8887 | ASSERT_TRUE(test_server.Start()); |
| 8888 | |
| 8889 | SSLClientSocket::ClearSessionCache(); |
| 8890 | |
| 8891 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8892 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8893 | { |
| 8894 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8895 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8896 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8897 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8898 | r->Start(); |
| 8899 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8900 | |
| 8901 | base::RunLoop().Run(); |
| 8902 | |
| 8903 | EXPECT_EQ(1, d.response_started_count()); |
| 8904 | } |
| 8905 | |
| 8906 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8907 | CloseAllConnections(); |
| 8908 | |
| 8909 | // Now change the certificate to be acceptable (so that the response is |
| 8910 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8911 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8912 | { |
| 8913 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8914 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8915 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8916 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8917 | r->Start(); |
| 8918 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8919 | |
| 8920 | base::RunLoop().Run(); |
| 8921 | |
| 8922 | // The response will look like; |
| 8923 | // insert abc |
| 8924 | // insert xyz |
| 8925 | // |
| 8926 | // With a newline at the end which makes the split think that there are |
| 8927 | // three lines. |
| 8928 | // |
| 8929 | // If a session was presented (eg: a bug), then the response would look |
| 8930 | // like; |
| 8931 | // insert abc |
| 8932 | // lookup abc |
| 8933 | // insert xyz |
| 8934 | |
| 8935 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8936 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8937 | } |
| 8938 | } |
| 8939 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8940 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 8941 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 8942 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8943 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 8944 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 8945 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8946 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 8947 | // testserver. |
| 8948 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 8949 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 8950 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 8951 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 8952 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 8953 | } }; |
| 8954 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8955 | // This is the policy OID contained in the certificates that testserver |
| 8956 | // generates. |
| 8957 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 8958 | |
| 8959 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 8960 | public: |
| 8961 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8962 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8963 | ev_test_policy_( |
| 8964 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 8965 | kOCSPTestCertFingerprint, |
| 8966 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8967 | } |
| 8968 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8969 | void SetUp() override { |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 8970 | context_.SetCTPolicyEnforcer( |
| 8971 | base::MakeUnique<AllowAnyCertCTPolicyEnforcer>()); |
| 8972 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8973 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8974 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8975 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8976 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 8977 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8978 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8979 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 8980 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8981 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8982 | EnsureNSSHttpIOInit(); |
| 8983 | #endif |
| 8984 | } |
| 8985 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 8986 | void DoConnectionWithDelegate( |
| 8987 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 8988 | TestDelegate* delegate, |
| 8989 | SSLInfo* out_ssl_info) { |
| 8990 | // Always overwrite |out_ssl_info|. |
| 8991 | out_ssl_info->Reset(); |
| 8992 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8993 | SpawnedTestServer test_server( |
| 8994 | SpawnedTestServer::TYPE_HTTPS, |
| 8995 | ssl_options, |
| 8996 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8997 | ASSERT_TRUE(test_server.Start()); |
| 8998 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 8999 | delegate->set_allow_certificate_errors(true); |
| 9000 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 9001 | test_server.GetURL("/"), DEFAULT_PRIORITY, delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9002 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9003 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9004 | base::RunLoop().Run(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9005 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9006 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9007 | *out_ssl_info = r->ssl_info(); |
| 9008 | } |
| 9009 | |
| 9010 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 9011 | CertStatus* out_cert_status) { |
| 9012 | // Always overwrite |out_cert_status|. |
| 9013 | *out_cert_status = 0; |
| 9014 | |
| 9015 | TestDelegate d; |
| 9016 | SSLInfo ssl_info; |
| 9017 | ASSERT_NO_FATAL_FAILURE( |
| 9018 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 9019 | |
| 9020 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9021 | } |
| 9022 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 9023 | ~HTTPSOCSPTest() override { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 9024 | #if defined(USE_NSS_CERTS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9025 | ShutdownNSSHttpIO(); |
| 9026 | #endif |
| 9027 | } |
| 9028 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9029 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9030 | class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
| 9031 | public: |
| 9032 | AllowAnyCertCTPolicyEnforcer() = default; |
| 9033 | ~AllowAnyCertCTPolicyEnforcer() override = default; |
| 9034 | |
| 9035 | ct::CertPolicyCompliance DoesConformToCertPolicy( |
| 9036 | X509Certificate* cert, |
| 9037 | const SCTList& verified_scts, |
| 9038 | const BoundNetLog& net_log) override { |
| 9039 | return ct::CertPolicyCompliance::CERT_POLICY_COMPLIES_VIA_SCTS; |
| 9040 | } |
| 9041 | |
| 9042 | ct::EVPolicyCompliance DoesConformToCTEVPolicy( |
| 9043 | X509Certificate* cert, |
| 9044 | const ct::EVCertsWhitelist* ev_whitelist, |
| 9045 | const SCTList& verified_scts, |
| 9046 | const BoundNetLog& net_log) override { |
| 9047 | return ct::EVPolicyCompliance::EV_POLICY_COMPLIES_VIA_SCTS; |
| 9048 | } |
| 9049 | }; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9050 | // SetupContext configures the URLRequestContext that will be used for making |
| 9051 | // connetions to testserver. This can be overridden in test subclasses for |
| 9052 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9053 | virtual void SetupContext() { |
| 9054 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9055 | true /* check for EV */, true /* online revocation checking */, |
| 9056 | false /* require rev. checking for local |
| 9057 | anchors */, |
| 9058 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9059 | } |
| 9060 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9061 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9062 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9063 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9064 | }; |
| 9065 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9066 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 9067 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9068 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 9069 | // have that ability on other platforms. |
| 9070 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 9071 | #else |
| 9072 | return 0; |
| 9073 | #endif |
| 9074 | } |
| 9075 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9076 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 9077 | // operating system supports revocation checking and can distinguish between |
| 9078 | // situations where a given certificate lacks any revocation information (eg: |
| 9079 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 9080 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 9081 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 9082 | // skipped. |
| 9083 | static bool SystemSupportsHardFailRevocationChecking() { |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 9084 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9085 | return true; |
| 9086 | #else |
| 9087 | return false; |
| 9088 | #endif |
| 9089 | } |
| 9090 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9091 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 9092 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 9093 | // several tests are effected because our testing EV certificate won't be |
| 9094 | // recognised as EV. |
| 9095 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9096 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 9097 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 9098 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9099 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 9100 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 9101 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 9102 | return false; |
| 9103 | #else |
| 9104 | return true; |
| 9105 | #endif |
| 9106 | } |
| 9107 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9108 | static bool SystemSupportsOCSP() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 9109 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9110 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 9111 | return false; |
| 9112 | #else |
| 9113 | return true; |
| 9114 | #endif |
| 9115 | } |
| 9116 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9117 | static bool SystemSupportsOCSPStapling() { |
davidben | 7ee2354 | 2016-06-03 18:27:33 | [diff] [blame] | 9118 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9119 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9120 | #else |
| 9121 | return false; |
| 9122 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9123 | } |
| 9124 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9125 | TEST_F(HTTPSOCSPTest, Valid) { |
| 9126 | if (!SystemSupportsOCSP()) { |
| 9127 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9128 | return; |
| 9129 | } |
| 9130 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9131 | SpawnedTestServer::SSLOptions ssl_options( |
| 9132 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9133 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9134 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9135 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9136 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9137 | |
| 9138 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9139 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9140 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9141 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9142 | |
| 9143 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9144 | } |
| 9145 | |
| 9146 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 9147 | if (!SystemSupportsOCSP()) { |
| 9148 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9149 | return; |
| 9150 | } |
| 9151 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9152 | SpawnedTestServer::SSLOptions ssl_options( |
| 9153 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9154 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9155 | |
| 9156 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9157 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9158 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 9159 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9160 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 9161 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9162 | #endif |
| 9163 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9164 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9165 | } |
| 9166 | |
| 9167 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 9168 | if (!SystemSupportsOCSP()) { |
| 9169 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9170 | return; |
| 9171 | } |
| 9172 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9173 | SpawnedTestServer::SSLOptions ssl_options( |
| 9174 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9175 | ssl_options.ocsp_status = |
| 9176 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9177 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9178 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9179 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9180 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9181 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9182 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9183 | |
| 9184 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9185 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9186 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9187 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9188 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9189 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9190 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9191 | LOG(WARNING) |
| 9192 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9193 | return; |
| 9194 | } |
| 9195 | |
| 9196 | SpawnedTestServer::SSLOptions ssl_options( |
| 9197 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9198 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9199 | ssl_options.staple_ocsp_response = true; |
| 9200 | ssl_options.ocsp_server_unavailable = true; |
| 9201 | |
| 9202 | CertStatus cert_status; |
| 9203 | DoConnection(ssl_options, &cert_status); |
| 9204 | |
| 9205 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9206 | |
| 9207 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9208 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9209 | |
| 9210 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9211 | } |
| 9212 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9213 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 9214 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9215 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 9216 | #else |
| 9217 | #define MAYBE_RevokedStapled RevokedStapled |
| 9218 | #endif |
| 9219 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9220 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9221 | LOG(WARNING) |
| 9222 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9223 | return; |
| 9224 | } |
| 9225 | |
| 9226 | SpawnedTestServer::SSLOptions ssl_options( |
| 9227 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9228 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9229 | ssl_options.staple_ocsp_response = true; |
| 9230 | ssl_options.ocsp_server_unavailable = true; |
| 9231 | |
| 9232 | CertStatus cert_status; |
| 9233 | DoConnection(ssl_options, &cert_status); |
| 9234 | |
| 9235 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9236 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9237 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9238 | } |
| 9239 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9240 | static const struct OCSPVerifyTestData { |
| 9241 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 9242 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 9243 | OCSPVerifyResult::ResponseStatus response_status; |
| 9244 | bool has_revocation_status; |
| 9245 | OCSPRevocationStatus cert_status; |
| 9246 | } kOCSPVerifyData[] = { |
| 9247 | |
| 9248 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9249 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9250 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9251 | OCSPVerifyResult::PROVIDED, |
| 9252 | true, |
| 9253 | OCSPRevocationStatus::GOOD}, |
| 9254 | |
| 9255 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9256 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 9257 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9258 | OCSPVerifyResult::INVALID_DATE, |
| 9259 | false, |
| 9260 | OCSPRevocationStatus::UNKNOWN}, |
| 9261 | |
| 9262 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9263 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 9264 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9265 | OCSPVerifyResult::INVALID_DATE, |
| 9266 | false, |
| 9267 | OCSPRevocationStatus::UNKNOWN}, |
| 9268 | |
| 9269 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9270 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 9271 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9272 | OCSPVerifyResult::INVALID_DATE, |
| 9273 | false, |
| 9274 | OCSPRevocationStatus::UNKNOWN}, |
| 9275 | |
| 9276 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9277 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 9278 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9279 | OCSPVerifyResult::INVALID_DATE, |
| 9280 | false, |
| 9281 | OCSPRevocationStatus::UNKNOWN}, |
| 9282 | |
| 9283 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 9284 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9285 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9286 | OCSPVerifyResult::ERROR_RESPONSE, |
| 9287 | false, |
| 9288 | OCSPRevocationStatus::UNKNOWN}, |
| 9289 | |
| 9290 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 9291 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9292 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9293 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 9294 | false, |
| 9295 | OCSPRevocationStatus::UNKNOWN}, |
| 9296 | |
| 9297 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 9298 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9299 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9300 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 9301 | false, |
| 9302 | OCSPRevocationStatus::UNKNOWN}, |
| 9303 | |
| 9304 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9305 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 9306 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9307 | OCSPVerifyResult::INVALID_DATE, |
| 9308 | false, |
| 9309 | OCSPRevocationStatus::UNKNOWN}, |
| 9310 | |
| 9311 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9312 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9313 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9314 | OCSPVerifyResult::PROVIDED, |
| 9315 | true, |
| 9316 | OCSPRevocationStatus::UNKNOWN}, |
| 9317 | |
| 9318 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9319 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 9320 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9321 | OCSPVerifyResult::INVALID_DATE, |
| 9322 | false, |
| 9323 | OCSPRevocationStatus::UNKNOWN}, |
| 9324 | |
| 9325 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9326 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 9327 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9328 | OCSPVerifyResult::INVALID_DATE, |
| 9329 | false, |
| 9330 | OCSPRevocationStatus::UNKNOWN}, |
| 9331 | |
| 9332 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9333 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9334 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 9335 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 9336 | false, |
| 9337 | OCSPRevocationStatus::UNKNOWN}, |
| 9338 | |
| 9339 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9340 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9341 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 9342 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 9343 | false, |
| 9344 | OCSPRevocationStatus::UNKNOWN}, |
| 9345 | |
| 9346 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9347 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9348 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 9349 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 9350 | false, |
| 9351 | OCSPRevocationStatus::UNKNOWN}, |
| 9352 | |
| 9353 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9354 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9355 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9356 | OCSPVerifyResult::PROVIDED, |
| 9357 | true, |
| 9358 | OCSPRevocationStatus::REVOKED}, |
| 9359 | |
| 9360 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9361 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 9362 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9363 | OCSPVerifyResult::INVALID_DATE, |
| 9364 | false, |
| 9365 | OCSPRevocationStatus::UNKNOWN}, |
| 9366 | |
| 9367 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9368 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 9369 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9370 | OCSPVerifyResult::INVALID_DATE, |
| 9371 | false, |
| 9372 | OCSPRevocationStatus::UNKNOWN}, |
| 9373 | |
| 9374 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9375 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9376 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9377 | OCSPVerifyResult::PROVIDED, |
| 9378 | true, |
| 9379 | OCSPRevocationStatus::GOOD}, |
| 9380 | |
| 9381 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9382 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 9383 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9384 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9385 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9386 | OCSPVerifyResult::PROVIDED, |
| 9387 | true, |
| 9388 | OCSPRevocationStatus::GOOD}, |
| 9389 | |
| 9390 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9391 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 9392 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9393 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9394 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9395 | OCSPVerifyResult::PROVIDED, |
| 9396 | true, |
| 9397 | OCSPRevocationStatus::GOOD}, |
| 9398 | |
| 9399 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9400 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 9401 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9402 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9403 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9404 | OCSPVerifyResult::PROVIDED, |
| 9405 | true, |
| 9406 | OCSPRevocationStatus::GOOD}, |
| 9407 | |
| 9408 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9409 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 9410 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9411 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 9412 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9413 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 9414 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9415 | OCSPVerifyResult::INVALID_DATE, |
| 9416 | false, |
| 9417 | OCSPRevocationStatus::UNKNOWN}, |
| 9418 | |
| 9419 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9420 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 9421 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9422 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 9423 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9424 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9425 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9426 | OCSPVerifyResult::PROVIDED, |
| 9427 | true, |
| 9428 | OCSPRevocationStatus::REVOKED}, |
| 9429 | |
| 9430 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9431 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 9432 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9433 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9434 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9435 | OCSPVerifyResult::PROVIDED, |
| 9436 | true, |
| 9437 | OCSPRevocationStatus::UNKNOWN}, |
| 9438 | |
| 9439 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 9440 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 9441 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 9442 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 9443 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 9444 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9445 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9446 | OCSPVerifyResult::PROVIDED, |
| 9447 | true, |
| 9448 | OCSPRevocationStatus::UNKNOWN}, |
| 9449 | |
| 9450 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 9451 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 9452 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9453 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 9454 | false, |
| 9455 | OCSPRevocationStatus::UNKNOWN}, |
| 9456 | |
| 9457 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 9458 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 9459 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 9460 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 9461 | false, |
| 9462 | OCSPRevocationStatus::UNKNOWN}, |
| 9463 | |
| 9464 | }; |
| 9465 | |
| 9466 | class HTTPSOCSPVerifyTest |
| 9467 | : public HTTPSOCSPTest, |
| 9468 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 9469 | |
| 9470 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 9471 | SpawnedTestServer::SSLOptions ssl_options( |
| 9472 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9473 | OCSPVerifyTestData test = GetParam(); |
| 9474 | |
| 9475 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 9476 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 9477 | ssl_options.staple_ocsp_response = true; |
| 9478 | |
| 9479 | SSLInfo ssl_info; |
| 9480 | OCSPErrorTestDelegate delegate; |
| 9481 | ASSERT_NO_FATAL_FAILURE( |
| 9482 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 9483 | |
| 9484 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 9485 | // URLRequest caches certificate errors. |
| 9486 | if (delegate.have_certificate_errors()) { |
| 9487 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 9488 | ssl_info = delegate.ssl_info(); |
| 9489 | } |
| 9490 | |
| 9491 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 9492 | |
| 9493 | if (test.has_revocation_status) |
| 9494 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 9495 | } |
| 9496 | |
| 9497 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 9498 | HTTPSOCSPVerifyTest, |
| 9499 | testing::ValuesIn(kOCSPVerifyData)); |
| 9500 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9501 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 9502 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9503 | void SetupContext() override { |
| 9504 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9505 | false /* check for EV */, false /* online revocation checking */, |
| 9506 | true /* require rev. checking for local |
| 9507 | anchors */, |
| 9508 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9509 | } |
| 9510 | }; |
| 9511 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9512 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 9513 | if (!SystemSupportsOCSP()) { |
| 9514 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9515 | return; |
| 9516 | } |
| 9517 | |
| 9518 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 9519 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 9520 | << "revocation checking"; |
| 9521 | return; |
| 9522 | } |
| 9523 | |
| 9524 | SpawnedTestServer::SSLOptions ssl_options( |
| 9525 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9526 | ssl_options.ocsp_status = |
| 9527 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9528 | |
| 9529 | CertStatus cert_status; |
| 9530 | DoConnection(ssl_options, &cert_status); |
| 9531 | |
| 9532 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 9533 | cert_status & CERT_STATUS_REVOKED); |
| 9534 | |
| 9535 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9536 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9537 | } |
| 9538 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9539 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 9540 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9541 | void SetupContext() override { |
| 9542 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9543 | true /* check for EV */, false /* online revocation checking */, |
| 9544 | false /* require rev. checking for local |
| 9545 | anchors */, |
| 9546 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9547 | } |
| 9548 | }; |
| 9549 | |
| 9550 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 9551 | if (!SystemSupportsOCSP()) { |
| 9552 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9553 | return; |
| 9554 | } |
| 9555 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9556 | SpawnedTestServer::SSLOptions ssl_options( |
| 9557 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9558 | ssl_options.ocsp_status = |
| 9559 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9560 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9561 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9562 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9563 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9564 | |
| 9565 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9566 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9567 | |
| 9568 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9569 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9570 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9571 | } |
| 9572 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 9573 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 9574 | if (!SystemSupportsOCSP()) { |
| 9575 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9576 | return; |
| 9577 | } |
| 9578 | |
| 9579 | SpawnedTestServer::SSLOptions ssl_options( |
| 9580 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9581 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9582 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9583 | |
| 9584 | CertStatus cert_status; |
| 9585 | DoConnection(ssl_options, &cert_status); |
| 9586 | |
| 9587 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 9588 | // possible to determine whether the check failed because of actual |
| 9589 | // revocation or because there was an OCSP failure. |
| 9590 | #if defined(OS_WIN) |
| 9591 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9592 | #else |
| 9593 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9594 | #endif |
| 9595 | |
| 9596 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9597 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9598 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9599 | } |
| 9600 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9601 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 9602 | if (!SystemSupportsOCSP()) { |
| 9603 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9604 | return; |
| 9605 | } |
| 9606 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9607 | SpawnedTestServer::SSLOptions ssl_options( |
| 9608 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9609 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9610 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9611 | |
| 9612 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9613 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9614 | |
| 9615 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9616 | |
| 9617 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9618 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9619 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9620 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9621 | } |
| 9622 | |
| 9623 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 9624 | if (!SystemSupportsOCSP()) { |
| 9625 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9626 | return; |
| 9627 | } |
| 9628 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9629 | SpawnedTestServer::SSLOptions ssl_options( |
| 9630 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9631 | ssl_options.ocsp_status = |
| 9632 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9633 | SSLConfigService::SetCRLSet( |
| 9634 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9635 | |
| 9636 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9637 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9638 | |
| 9639 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9640 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9641 | |
| 9642 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9643 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9644 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9645 | } |
| 9646 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9647 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 9648 | if (!SystemSupportsOCSP()) { |
| 9649 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9650 | return; |
| 9651 | } |
| 9652 | |
| 9653 | SpawnedTestServer::SSLOptions ssl_options( |
| 9654 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9655 | ssl_options.ocsp_status = |
| 9656 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9657 | SSLConfigService::SetCRLSet( |
| 9658 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9659 | false, &kOCSPTestCertSPKI, ""))); |
| 9660 | |
| 9661 | CertStatus cert_status; |
| 9662 | DoConnection(ssl_options, &cert_status); |
| 9663 | |
| 9664 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 9665 | // revocation check for EV. |
| 9666 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9667 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9668 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9669 | EXPECT_FALSE( |
| 9670 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9671 | } |
| 9672 | |
| 9673 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 9674 | if (!SystemSupportsOCSP()) { |
| 9675 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9676 | return; |
| 9677 | } |
| 9678 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9679 | SpawnedTestServer::SSLOptions ssl_options( |
| 9680 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9681 | ssl_options.ocsp_status = |
| 9682 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9683 | SSLConfigService::SetCRLSet( |
| 9684 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 9685 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9686 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9687 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9688 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9689 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 9690 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 9691 | // test. |
| 9692 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9693 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9694 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9695 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9696 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9697 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9698 | } |
| 9699 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9700 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 9701 | // Test that when EV verification is requested, but online revocation |
| 9702 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 9703 | // no revocation checking actually happens. |
| 9704 | if (!SystemSupportsOCSP()) { |
| 9705 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9706 | return; |
| 9707 | } |
| 9708 | |
| 9709 | // Unmark the certificate's OID as EV, which should disable revocation |
| 9710 | // checking (as per the user preference) |
| 9711 | ev_test_policy_.reset(); |
| 9712 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9713 | SpawnedTestServer::SSLOptions ssl_options( |
| 9714 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9715 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9716 | SSLConfigService::SetCRLSet( |
| 9717 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9718 | |
| 9719 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9720 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9721 | |
| 9722 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9723 | |
| 9724 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9725 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9726 | } |
| 9727 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9728 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 9729 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9730 | void SetupContext() override { |
| 9731 | context_.set_ssl_config_service(new TestSSLConfigService( |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 9732 | false /* check for EV */, false /* online revocation checking */, |
| 9733 | false /* require rev. checking for local |
| 9734 | anchors */, |
| 9735 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9736 | } |
| 9737 | }; |
| 9738 | |
| 9739 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9740 | SpawnedTestServer::SSLOptions ssl_options( |
| 9741 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame^] | 9742 | ssl_options.ocsp_status = |
| 9743 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9744 | SSLConfigService::SetCRLSet( |
| 9745 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9746 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9747 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9748 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9749 | |
| 9750 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 9751 | // we don't fall back to online revocation checks. |
| 9752 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9753 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9754 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9755 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9756 | |
| 9757 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 9758 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9759 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 9760 | return; |
| 9761 | #endif |
| 9762 | |
| 9763 | SpawnedTestServer::SSLOptions ssl_options( |
| 9764 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9765 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9766 | ssl_options.cert_serial = 10; |
| 9767 | SSLConfigService::SetCRLSet( |
| 9768 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9769 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 9770 | |
| 9771 | CertStatus cert_status = 0; |
| 9772 | DoConnection(ssl_options, &cert_status); |
| 9773 | |
| 9774 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 9775 | // reflected without online revocation checking. |
| 9776 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9777 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9778 | EXPECT_FALSE( |
| 9779 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9780 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9781 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9782 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9783 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 9784 | // These tests aren't passing on Android. Either the RemoteTestServer isn't |
| 9785 | // starting up successfully, or it can't access the test files. |
| 9786 | // TODO(mmenke): Fix this. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9787 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9788 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 9789 | URLRequestTestFTP() |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9790 | : ftp_transaction_factory_(&host_resolver_), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9791 | ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
| 9792 | SpawnedTestServer::kLocalhost, |
| 9793 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9794 | // Can't use |default_context_|'s HostResolver to set up the |
| 9795 | // FTPTransactionFactory because it hasn't been created yet. |
| 9796 | default_context_.set_host_resolver(&host_resolver_); |
| 9797 | } |
| 9798 | |
| 9799 | // URLRequestTest interface: |
| 9800 | void SetUpFactory() override { |
| 9801 | // Add FTP support to the default URLRequestContext. |
| 9802 | job_factory_impl_->SetProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 9803 | "ftp", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9804 | base::WrapUnique(new FtpProtocolHandler(&ftp_transaction_factory_))); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9805 | } |
| 9806 | |
| 9807 | std::string GetTestFileContents() { |
| 9808 | base::FilePath path; |
| 9809 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 9810 | path = path.Append(kTestFilePath); |
| 9811 | path = path.AppendASCII(kFtpTestFile); |
| 9812 | std::string contents; |
| 9813 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 9814 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9815 | } |
| 9816 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9817 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9818 | MockHostResolver host_resolver_; |
| 9819 | FtpNetworkLayer ftp_transaction_factory_; |
| 9820 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9821 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9822 | }; |
| 9823 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9824 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 9825 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9826 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9827 | |
| 9828 | TestDelegate d; |
| 9829 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9830 | std::unique_ptr<URLRequest> r( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9831 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9832 | r->Start(); |
| 9833 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9834 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9835 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9836 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9837 | EXPECT_FALSE(r->is_pending()); |
| 9838 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9839 | EXPECT_THAT(r->status().error(), IsError(ERR_UNSAFE_PORT)); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9840 | } |
| 9841 | } |
| 9842 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9843 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9844 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9845 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9846 | TestDelegate d; |
| 9847 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9848 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9849 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9850 | r->Start(); |
| 9851 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9852 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9853 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9854 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9855 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9856 | EXPECT_EQ(1, d.response_started_count()); |
| 9857 | EXPECT_FALSE(d.received_data_before_response()); |
| 9858 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9859 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9860 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9861 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9862 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9863 | } |
| 9864 | } |
| 9865 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9866 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9867 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9868 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9869 | TestDelegate d; |
| 9870 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9871 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9872 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9873 | r->Start(); |
| 9874 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9875 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9876 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9877 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9878 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9879 | EXPECT_EQ(1, d.response_started_count()); |
| 9880 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9881 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9882 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9883 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9884 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9885 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9886 | } |
| 9887 | } |
| 9888 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9889 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9890 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9891 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9892 | TestDelegate d; |
| 9893 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9894 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9895 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9896 | "chrome"), |
| 9897 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9898 | r->Start(); |
| 9899 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9900 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9901 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9902 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9903 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9904 | EXPECT_EQ(1, d.response_started_count()); |
| 9905 | EXPECT_FALSE(d.received_data_before_response()); |
| 9906 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9907 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9908 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9909 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9910 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 9911 | |
| 9912 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9913 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 9914 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9915 | } |
| 9916 | } |
| 9917 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9918 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9919 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9920 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9921 | TestDelegate d; |
| 9922 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9923 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9924 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9925 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9926 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9927 | r->Start(); |
| 9928 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9929 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9930 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9931 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9932 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9933 | EXPECT_EQ(1, d.response_started_count()); |
| 9934 | EXPECT_FALSE(d.received_data_before_response()); |
| 9935 | EXPECT_EQ(d.bytes_received(), 0); |
| 9936 | } |
| 9937 | } |
| 9938 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9939 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9940 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9941 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9942 | TestDelegate d; |
| 9943 | // Set correct login credentials. The delegate will be asked for them when |
| 9944 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9945 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9946 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9947 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9948 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9949 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9950 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9951 | r->Start(); |
| 9952 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9953 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9954 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9955 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9956 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9957 | EXPECT_EQ(1, d.response_started_count()); |
| 9958 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9959 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9960 | } |
| 9961 | } |
| 9962 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9963 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9964 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9965 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9966 | TestDelegate d; |
| 9967 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9968 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9969 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9970 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9971 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9972 | r->Start(); |
| 9973 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9974 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9975 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9977 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9978 | EXPECT_EQ(1, d.response_started_count()); |
| 9979 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9980 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9981 | } |
| 9982 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9983 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9984 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9985 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9986 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9987 | TestDelegate d; |
| 9988 | // Set correct login credentials. The delegate will be asked for them when |
| 9989 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9990 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9991 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9992 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9993 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9994 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9995 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9996 | r->Start(); |
| 9997 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9998 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9999 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10000 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10001 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10002 | EXPECT_EQ(1, d.response_started_count()); |
| 10003 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10004 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 10005 | } |
| 10006 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10007 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10008 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10009 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10010 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10011 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10012 | { |
| 10013 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10014 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10015 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 10016 | "chrome"), |
| 10017 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10018 | r->Start(); |
| 10019 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10020 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10021 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10022 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10023 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10024 | EXPECT_EQ(1, d->response_started_count()); |
| 10025 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10026 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10027 | } |
| 10028 | |
| 10029 | d.reset(new TestDelegate); |
| 10030 | { |
| 10031 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10032 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10033 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10034 | r->Start(); |
| 10035 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10036 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10037 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10038 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10039 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10040 | EXPECT_EQ(1, d->response_started_count()); |
| 10041 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10042 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10043 | } |
| 10044 | } |
| 10045 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10046 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10047 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10048 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10049 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10050 | // Set correct login credentials. The delegate will be asked for them when |
| 10051 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 10052 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10053 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10054 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10055 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 10056 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10057 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10058 | r->Start(); |
| 10059 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10060 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10061 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10063 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10064 | EXPECT_EQ(1, d->response_started_count()); |
| 10065 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10066 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10067 | } |
| 10068 | |
| 10069 | // Use a new delegate without explicit credentials. The cached ones should be |
| 10070 | // used. |
| 10071 | d.reset(new TestDelegate); |
| 10072 | { |
| 10073 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 10074 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10075 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10076 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10077 | r->Start(); |
| 10078 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10079 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 10080 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10081 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10082 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10083 | EXPECT_EQ(1, d->response_started_count()); |
| 10084 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 10085 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 10086 | } |
| 10087 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 10088 | |
| 10089 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 10090 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 10091 | |
| 10092 | TestDelegate d; |
| 10093 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 10094 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d)); |
| 10095 | req->Start(); |
| 10096 | base::RunLoop().Run(); |
| 10097 | |
| 10098 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 10099 | } |
| 10100 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10101 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 10102 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10103 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 10104 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10105 | std::unique_ptr<URLRequest> req( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10106 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
| 10107 | |
| 10108 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10109 | |
| 10110 | req->Start(); |
| 10111 | base::RunLoop().Run(); |
| 10112 | |
| 10113 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 10114 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10115 | } |
| 10116 | |
| 10117 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 10118 | MockHostResolver host_resolver; |
| 10119 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10120 | TestURLRequestContext context(true); |
| 10121 | context.set_network_delegate(&network_delegate); |
| 10122 | context.set_host_resolver(&host_resolver); |
| 10123 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 10124 | context.Init(); |
| 10125 | |
| 10126 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10127 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 10128 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 10129 | |
| 10130 | EXPECT_FALSE(req->response_info().network_accessed); |
| 10131 | |
| 10132 | req->Start(); |
| 10133 | base::RunLoop().Run(); |
| 10134 | EXPECT_TRUE(req->response_info().network_accessed); |
| 10135 | } |
| 10136 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10137 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10138 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10139 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10140 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10141 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10142 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d)); |
| 10143 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10144 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10145 | req.get(), &default_network_delegate_, |
| 10146 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 10147 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 10148 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10149 | |
| 10150 | req->Start(); |
| 10151 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 10152 | base::RunLoop().RunUntilIdle(); |
| 10153 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 10154 | EXPECT_EQ(0, d.received_redirect_count()); |
| 10155 | } |
| 10156 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 10157 | } // namespace net |