[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 7 | #include <math.h> // ceil |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8 | #include <stdarg.h> |
sclittle | be1ccf6 | 2015-09-02 19:40:36 | [diff] [blame] | 9 | #include <stdint.h> |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 11 | #include <limits> |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 12 | #include <set> |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 13 | #include <string> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 15 | #include <vector> |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 16 | |
Sebastien Marchand | 6d0558fd | 2019-01-25 16:49:37 | [diff] [blame] | 17 | #include "base/bind.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 21 | #include "base/json/json_writer.h" |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 22 | #include "base/logging.h" |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
[email protected] | bf82898 | 2013-08-14 18:01:47 | [diff] [blame] | 24 | #include "base/memory/weak_ptr.h" |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 25 | #include "base/optional.h" |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 26 | #include "base/run_loop.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 29 | #include "base/strings/string_util.h" |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 30 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 31 | #include "base/strings/utf_string_conversions.h" |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 32 | #include "base/test/metrics/histogram_tester.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 33 | #include "base/test/scoped_feature_list.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 34 | #include "base/test/simple_test_clock.h" |
| 35 | #include "base/test/simple_test_tick_clock.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 36 | #include "base/test/task_environment.h" |
[email protected] | f36a813 | 2011-09-02 18:36:33 | [diff] [blame] | 37 | #include "base/test/test_file_util.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 38 | #include "base/threading/thread_task_runner_handle.h" |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 39 | #include "build/build_config.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 40 | #include "net/base/auth.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 41 | #include "net/base/chunked_upload_data_stream.h" |
Bence Béky | a25e3f7 | 2018-02-13 21:13:39 | [diff] [blame] | 42 | #include "net/base/completion_once_callback.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 43 | #include "net/base/elements_upload_data_stream.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 44 | #include "net/base/features.h" |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 45 | #include "net/base/host_port_pair.h" |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 46 | #include "net/base/ip_endpoint.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 47 | #include "net/base/load_timing_info.h" |
| 48 | #include "net/base/load_timing_info_test_util.h" |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 49 | #include "net/base/net_errors.h" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 50 | #include "net/base/privacy_mode.h" |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 51 | #include "net/base/proxy_delegate.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 52 | #include "net/base/proxy_server.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 53 | #include "net/base/request_priority.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "net/base/test_completion_callback.h" |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 55 | #include "net/base/test_proxy_delegate.h" |
[email protected] | b2d26cfd | 2012-12-11 10:36:06 | [diff] [blame] | 56 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 57 | #include "net/base/upload_file_element_reader.h" |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 58 | #include "net/cert/cert_status_flags.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 59 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 60 | #include "net/dns/mock_host_resolver.h" |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 61 | #include "net/http/http_auth_challenge_tokenizer.h" |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 62 | #include "net/http/http_auth_handler_digest.h" |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 63 | #include "net/http/http_auth_handler_mock.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 64 | #include "net/http/http_auth_handler_ntlm.h" |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 65 | #include "net/http/http_auth_ntlm_mechanism.h" |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 66 | #include "net/http/http_auth_scheme.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 67 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 68 | #include "net/http/http_network_session.h" |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 69 | #include "net/http/http_network_session_peer.h" |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 70 | #include "net/http/http_proxy_connect_job.h" |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 71 | #include "net/http/http_request_headers.h" |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 72 | #include "net/http/http_response_info.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 73 | #include "net/http/http_server_properties.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 74 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 75 | #include "net/http/http_stream_factory.h" |
[email protected] | c41737d | 2014-05-14 07:47:19 | [diff] [blame] | 76 | #include "net/http/http_transaction_test_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 77 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 78 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 79 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 80 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 81 | #include "net/log/test_net_log_util.h" |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 82 | #include "net/proxy_resolution/configured_proxy_resolution_service.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 83 | #include "net/proxy_resolution/mock_proxy_resolver.h" |
| 84 | #include "net/proxy_resolution/proxy_config_service_fixed.h" |
| 85 | #include "net/proxy_resolution/proxy_info.h" |
| 86 | #include "net/proxy_resolution/proxy_resolver.h" |
| 87 | #include "net/proxy_resolution/proxy_resolver_factory.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 88 | #include "net/socket/client_socket_factory.h" |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 89 | #include "net/socket/client_socket_pool.h" |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 90 | #include "net/socket/client_socket_pool_manager.h" |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 91 | #include "net/socket/connect_job.h" |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 92 | #include "net/socket/connection_attempts.h" |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 93 | #include "net/socket/mock_client_socket_pool_manager.h" |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 94 | #include "net/socket/next_proto.h" |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 95 | #include "net/socket/socket_tag.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 96 | #include "net/socket/socket_test_util.h" |
Matt Menke | 4b412da | 2019-01-25 19:31:12 | [diff] [blame] | 97 | #include "net/socket/socks_connect_job.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 98 | #include "net/socket/ssl_client_socket.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 99 | #include "net/spdy/spdy_session.h" |
| 100 | #include "net/spdy/spdy_session_pool.h" |
| 101 | #include "net/spdy/spdy_test_util_common.h" |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 102 | #include "net/ssl/client_cert_identity_test_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 103 | #include "net/ssl/ssl_cert_request_info.h" |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 104 | #include "net/ssl/ssl_config.h" |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 105 | #include "net/ssl/ssl_config_service.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 106 | #include "net/ssl/ssl_info.h" |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 107 | #include "net/ssl/ssl_private_key.h" |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 108 | #include "net/ssl/test_ssl_config_service.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 109 | #include "net/test/cert_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 110 | #include "net/test/gtest_util.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 111 | #include "net/test/test_data_directory.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 112 | #include "net/test/test_with_task_environment.h" |
Victor Vasiliev | 27cc771 | 2019-01-24 11:50:14 | [diff] [blame] | 113 | #include "net/third_party/quiche/src/spdy/core/spdy_framer.h" |
[email protected] | baee31a | 2018-01-18 06:10:23 | [diff] [blame] | 114 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 115 | #include "net/url_request/static_http_user_agent_settings.h" |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 116 | #include "net/websockets/websocket_handshake_stream_base.h" |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 117 | #include "net/websockets/websocket_test_util.h" |
bnc | f458840 | 2015-11-24 13:33:18 | [diff] [blame] | 118 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 119 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 120 | #include "testing/platform_test.h" |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 121 | #include "url/gurl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 123 | #if defined(NTLM_PORTABLE) |
| 124 | #include "base/base64.h" |
| 125 | #include "net/ntlm/ntlm_test_data.h" |
| 126 | #endif |
| 127 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 128 | #if BUILDFLAG(ENABLE_REPORTING) |
| 129 | #include "net/network_error_logging/network_error_logging_service.h" |
| 130 | #include "net/network_error_logging/network_error_logging_test_util.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 131 | #include "net/reporting/reporting_cache.h" |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 132 | #include "net/reporting/reporting_endpoint.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 133 | #include "net/reporting/reporting_header_parser.h" |
| 134 | #include "net/reporting/reporting_service.h" |
| 135 | #include "net/reporting/reporting_test_util.h" |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 136 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 137 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 138 | using net::test::IsError; |
| 139 | using net::test::IsOk; |
| 140 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 141 | using base::ASCIIToUTF16; |
| 142 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 143 | using testing::AnyOf; |
| 144 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 145 | //----------------------------------------------------------------------------- |
| 146 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 147 | namespace net { |
| 148 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 149 | namespace { |
| 150 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 151 | const base::string16 kBar(ASCIIToUTF16("bar")); |
| 152 | const base::string16 kBar2(ASCIIToUTF16("bar2")); |
| 153 | const base::string16 kBar3(ASCIIToUTF16("bar3")); |
| 154 | const base::string16 kBaz(ASCIIToUTF16("baz")); |
| 155 | const base::string16 kFirst(ASCIIToUTF16("first")); |
| 156 | const base::string16 kFoo(ASCIIToUTF16("foo")); |
| 157 | const base::string16 kFoo2(ASCIIToUTF16("foo2")); |
| 158 | const base::string16 kFoo3(ASCIIToUTF16("foo3")); |
| 159 | const base::string16 kFou(ASCIIToUTF16("fou")); |
| 160 | const base::string16 kSecond(ASCIIToUTF16("second")); |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 161 | const base::string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 162 | |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 163 | const char kAlternativeServiceHttpHeader[] = |
| 164 | "Alt-Svc: h2=\"mail.example.org:443\"\r\n"; |
| 165 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 166 | int GetIdleSocketCountInTransportSocketPool(HttpNetworkSession* session) { |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 167 | return session |
| 168 | ->GetSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 169 | ProxyServer::Direct()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 170 | ->IdleSocketCount(); |
[email protected] | e5c02664 | 2012-03-17 00:14:02 | [diff] [blame] | 171 | } |
| 172 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 173 | bool IsTransportSocketPoolStalled(HttpNetworkSession* session) { |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 174 | return session |
| 175 | ->GetSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 176 | ProxyServer::Direct()) |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 177 | ->IsStalled(); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 180 | // Takes in a Value created from a NetLogHttpResponseParameter, and returns |
| 181 | // a JSONified list of headers as a single string. Uses single quotes instead |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 182 | // of double quotes for easier comparison. |
| 183 | std::string GetHeaders(const base::Value& params) { |
| 184 | if (!params.is_dict()) |
| 185 | return ""; |
| 186 | const base::Value* header_list = params.FindListKey("headers"); |
| 187 | if (!header_list) |
| 188 | return ""; |
| 189 | std::string headers; |
| 190 | base::JSONWriter::Write(*header_list, &headers); |
| 191 | base::ReplaceChars(headers, "\"", "'", &headers); |
| 192 | return headers; |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 195 | // Tests LoadTimingInfo in the case a socket is reused and no PAC script is |
| 196 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 197 | void TestLoadTimingReused(const LoadTimingInfo& load_timing_info) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 198 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 199 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 200 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 201 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 202 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 203 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 204 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 205 | EXPECT_FALSE(load_timing_info.send_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 206 | |
| 207 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 208 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 209 | // Set at a higher level. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 210 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 211 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 212 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 215 | // Tests LoadTimingInfo in the case a new socket is used and no PAC script is |
| 216 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 217 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 218 | int connect_timing_flags) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 219 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 220 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 221 | |
| 222 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 223 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 224 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 225 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 226 | connect_timing_flags); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 227 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 228 | load_timing_info.send_start); |
| 229 | |
| 230 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 231 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 232 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 233 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 234 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 235 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Tests LoadTimingInfo in the case a socket is reused and a PAC script is |
| 239 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 240 | void TestLoadTimingReusedWithPac(const LoadTimingInfo& load_timing_info) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 241 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 242 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 243 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 244 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 245 | |
| 246 | EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); |
| 247 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 248 | load_timing_info.proxy_resolve_end); |
| 249 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 250 | load_timing_info.send_start); |
| 251 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 252 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 253 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 254 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 255 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 256 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Tests LoadTimingInfo in the case a new socket is used and a PAC script is |
| 260 | // used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 261 | void TestLoadTimingNotReusedWithPac(const LoadTimingInfo& load_timing_info, |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 262 | int connect_timing_flags) { |
| 263 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 264 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 265 | |
| 266 | EXPECT_FALSE(load_timing_info.proxy_resolve_start.is_null()); |
| 267 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 268 | load_timing_info.proxy_resolve_end); |
| 269 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 270 | load_timing_info.connect_timing.connect_start); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 271 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 272 | connect_timing_flags); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 273 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 274 | load_timing_info.send_start); |
| 275 | |
| 276 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 277 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 278 | // Set at a higher level. |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 279 | EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 280 | EXPECT_TRUE(load_timing_info.request_start.is_null()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 281 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 282 | } |
| 283 | |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 284 | // ProxyResolver that records URLs passed to it, and that can be told what |
| 285 | // result to return. |
| 286 | class CapturingProxyResolver : public ProxyResolver { |
| 287 | public: |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 288 | struct LookupInfo { |
| 289 | GURL url; |
| 290 | NetworkIsolationKey network_isolation_key; |
| 291 | }; |
| 292 | |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 293 | CapturingProxyResolver() |
| 294 | : proxy_server_(ProxyServer::SCHEME_HTTP, HostPortPair("myproxy", 80)) {} |
| 295 | ~CapturingProxyResolver() override = default; |
| 296 | |
| 297 | int GetProxyForURL(const GURL& url, |
Matt Menke | cd522ee0 | 2019-11-13 19:53:39 | [diff] [blame] | 298 | const NetworkIsolationKey& network_isolation_key, |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 299 | ProxyInfo* results, |
| 300 | CompletionOnceCallback callback, |
| 301 | std::unique_ptr<Request>* request, |
| 302 | const NetLogWithSource& net_log) override { |
| 303 | results->UseProxyServer(proxy_server_); |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 304 | lookup_info_.push_back(LookupInfo{url, network_isolation_key}); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 305 | return OK; |
| 306 | } |
| 307 | |
| 308 | // Sets whether the resolver should use direct connections, instead of a |
| 309 | // proxy. |
| 310 | void set_proxy_server(ProxyServer proxy_server) { |
| 311 | proxy_server_ = proxy_server; |
| 312 | } |
| 313 | |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 314 | const std::vector<LookupInfo>& lookup_info() const { return lookup_info_; } |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 315 | |
| 316 | private: |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 317 | std::vector<LookupInfo> lookup_info_; |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 318 | |
| 319 | ProxyServer proxy_server_; |
| 320 | |
| 321 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 322 | }; |
| 323 | |
| 324 | class CapturingProxyResolverFactory : public ProxyResolverFactory { |
| 325 | public: |
| 326 | explicit CapturingProxyResolverFactory(CapturingProxyResolver* resolver) |
| 327 | : ProxyResolverFactory(false), resolver_(resolver) {} |
| 328 | |
| 329 | int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script, |
| 330 | std::unique_ptr<ProxyResolver>* resolver, |
| 331 | CompletionOnceCallback callback, |
| 332 | std::unique_ptr<Request>* request) override { |
| 333 | *resolver = std::make_unique<ForwardingProxyResolver>(resolver_); |
| 334 | return OK; |
| 335 | } |
| 336 | |
| 337 | private: |
| 338 | ProxyResolver* resolver_; |
| 339 | }; |
| 340 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 341 | std::unique_ptr<HttpNetworkSession> CreateSession( |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 342 | SpdySessionDependencies* session_deps) { |
[email protected] | c6bf815 | 2012-12-02 07:43:34 | [diff] [blame] | 343 | return SpdySessionDependencies::SpdyCreateSession(session_deps); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 344 | } |
| 345 | |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 346 | class FailingProxyResolverFactory : public ProxyResolverFactory { |
| 347 | public: |
| 348 | FailingProxyResolverFactory() : ProxyResolverFactory(false) {} |
| 349 | |
| 350 | // ProxyResolverFactory override. |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 351 | int CreateProxyResolver(const scoped_refptr<PacFileData>& script_data, |
| 352 | std::unique_ptr<ProxyResolver>* result, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 353 | CompletionOnceCallback callback, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 354 | std::unique_ptr<Request>* request) override { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 355 | return ERR_PAC_SCRIPT_FAILED; |
| 356 | } |
| 357 | }; |
| 358 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 359 | } // namespace |
| 360 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 361 | class HttpNetworkTransactionTest : public PlatformTest, |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 362 | public WithTaskEnvironment { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 363 | public: |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 364 | ~HttpNetworkTransactionTest() override { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 365 | // Important to restore the per-pool limit first, since the pool limit must |
| 366 | // always be greater than group limit, and the tests reduce both limits. |
| 367 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 368 | HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_pool_sockets_); |
| 369 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 370 | HttpNetworkSession::NORMAL_SOCKET_POOL, old_max_group_sockets_); |
| 371 | } |
| 372 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 373 | protected: |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 374 | HttpNetworkTransactionTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 375 | : WithTaskEnvironment(base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 376 | dummy_connect_job_params_( |
| 377 | nullptr /* client_socket_factory */, |
| 378 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 379 | nullptr /* http_auth_cache */, |
| 380 | nullptr /* http_auth_handler_factory */, |
| 381 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 382 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 383 | nullptr /* quic_stream_factory */, |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 384 | nullptr /* proxy_delegate */, |
| 385 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 386 | nullptr /* ssl_client_context */, |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 387 | nullptr /* socket_performance_watcher_factory */, |
| 388 | nullptr /* network_quality_estimator */, |
| 389 | nullptr /* net_log */, |
| 390 | nullptr /* websocket_endpoint_lock_manager */), |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 391 | ssl_(ASYNC, OK), |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 392 | old_max_group_sockets_(ClientSocketPoolManager::max_sockets_per_group( |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 393 | HttpNetworkSession::NORMAL_SOCKET_POOL)), |
| 394 | old_max_pool_sockets_(ClientSocketPoolManager::max_sockets_per_pool( |
| 395 | HttpNetworkSession::NORMAL_SOCKET_POOL)) { |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 396 | session_deps_.enable_http2_alternative_service = true; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 397 | } |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 398 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 399 | struct SimpleGetHelperResult { |
| 400 | int rv; |
| 401 | std::string status_line; |
| 402 | std::string response_data; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 403 | int64_t total_received_bytes; |
| 404 | int64_t total_sent_bytes; |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 405 | LoadTimingInfo load_timing_info; |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 406 | ConnectionAttempts connection_attempts; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 407 | IPEndPoint remote_endpoint_after_start; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 408 | }; |
| 409 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 410 | void SetUp() override { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 411 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 412 | base::RunLoop().RunUntilIdle(); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 413 | // Set an initial delay to ensure that the first call to TimeTicks::Now() |
| 414 | // before incrementing the counter does not return a null value. |
Matt Menke | 6dc0823 | 2019-10-03 18:00:28 | [diff] [blame] | 415 | FastForwardBy(base::TimeDelta::FromSeconds(1)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 416 | } |
| 417 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 418 | void TearDown() override { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 419 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 420 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 421 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 422 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 423 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 424 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 425 | base::RunLoop().RunUntilIdle(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 426 | } |
| 427 | |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 428 | void Check100ResponseTiming(bool use_spdy); |
| 429 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 430 | // Either |write_failure| specifies a write failure or |read_failure| |
| 431 | // specifies a read failure when using a reused socket. In either case, the |
| 432 | // failure should cause the network transaction to resend the request, and the |
| 433 | // other argument should be NULL. |
| 434 | void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, |
| 435 | const MockRead* read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 436 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 437 | // Either |write_failure| specifies a write failure or |read_failure| |
| 438 | // specifies a read failure when using a reused socket. In either case, the |
| 439 | // failure should cause the network transaction to resend the request, and the |
| 440 | // other argument should be NULL. |
| 441 | void PreconnectErrorResendRequestTest(const MockWrite* write_failure, |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 442 | const MockRead* read_failure, |
| 443 | bool use_spdy); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 444 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 445 | SimpleGetHelperResult SimpleGetHelperForData( |
| 446 | base::span<StaticSocketDataProvider*> providers) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 447 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 448 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 449 | HttpRequestInfo request; |
| 450 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 451 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 452 | request.traffic_annotation = |
| 453 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 454 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 455 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 456 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 457 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 458 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 459 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 460 | for (auto* provider : providers) { |
| 461 | session_deps_.socket_factory->AddSocketDataProvider(provider); |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 462 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 463 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 464 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 465 | |
eroman | 24bc6a1 | 2015-05-06 19:55:48 | [diff] [blame] | 466 | EXPECT_TRUE(log.bound().IsCapturing()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 467 | int rv = trans.Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 468 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 469 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 470 | out.rv = callback.WaitForResult(); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 471 | out.total_received_bytes = trans.GetTotalReceivedBytes(); |
| 472 | out.total_sent_bytes = trans.GetTotalSentBytes(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 473 | |
| 474 | // Even in the failure cases that use this function, connections are always |
| 475 | // successfully established before the error. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 476 | EXPECT_TRUE(trans.GetLoadTimingInfo(&out.load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 477 | TestLoadTimingNotReused(out.load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 478 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 479 | if (out.rv != OK) |
| 480 | return out; |
| 481 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 482 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 483 | // Can't use ASSERT_* inside helper functions like this, so |
| 484 | // return an error. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 485 | if (!response || !response->headers) { |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 486 | out.rv = ERR_UNEXPECTED; |
| 487 | return out; |
| 488 | } |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 489 | out.status_line = response->headers->GetStatusLine(); |
| 490 | |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 491 | EXPECT_EQ("127.0.0.1", response->remote_endpoint.ToStringWithoutPort()); |
| 492 | EXPECT_EQ(80, response->remote_endpoint.port()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 493 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 494 | bool got_endpoint = |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 495 | trans.GetRemoteEndpoint(&out.remote_endpoint_after_start); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 496 | EXPECT_EQ(got_endpoint, |
| 497 | out.remote_endpoint_after_start.address().size() > 0); |
| 498 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 499 | rv = ReadTransaction(&trans, &out.response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 500 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 501 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 502 | auto entries = log.GetEntries(); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 503 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 504 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 505 | NetLogEventPhase::NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 506 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 507 | entries, pos, NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 508 | NetLogEventPhase::NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 509 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 510 | EXPECT_EQ("GET / HTTP/1.1\r\n", |
| 511 | GetStringValueFromParams(entries[pos], "line")); |
[email protected] | f3da152d | 2012-06-02 01:00:57 | [diff] [blame] | 512 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 513 | EXPECT_EQ("['Host: www.example.org','Connection: keep-alive']", |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 514 | GetHeaders(entries[pos].params)); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 515 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 516 | out.total_received_bytes = trans.GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 517 | // The total number of sent bytes should not have changed. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 518 | EXPECT_EQ(out.total_sent_bytes, trans.GetTotalSentBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 519 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 520 | trans.GetConnectionAttempts(&out.connection_attempts); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 521 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 522 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 523 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 524 | SimpleGetHelperResult SimpleGetHelper(base::span<const MockRead> data_reads) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 525 | MockWrite data_writes[] = { |
| 526 | MockWrite("GET / HTTP/1.1\r\n" |
| 527 | "Host: www.example.org\r\n" |
| 528 | "Connection: keep-alive\r\n\r\n"), |
| 529 | }; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 530 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 531 | StaticSocketDataProvider reads(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 532 | StaticSocketDataProvider* data[] = {&reads}; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 533 | SimpleGetHelperResult out = SimpleGetHelperForData(data); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 534 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 535 | EXPECT_EQ(CountWriteBytes(data_writes), out.total_sent_bytes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 536 | return out; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 537 | } |
| 538 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 539 | void AddSSLSocketData() { |
| 540 | ssl_.next_proto = kProtoHTTP2; |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 541 | ssl_.ssl_info.cert = |
| 542 | ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"); |
| 543 | ASSERT_TRUE(ssl_.ssl_info.cert); |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 544 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_); |
| 545 | } |
| 546 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 547 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 548 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 549 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 550 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 551 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 552 | void CheckErrorIsPassedBack(int error, IoMode mode); |
| 553 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 554 | base::RepeatingClosure FastForwardByCallback(base::TimeDelta delta) { |
| 555 | return base::BindRepeating(&HttpNetworkTransactionTest::FastForwardBy, |
| 556 | base::Unretained(this), delta); |
| 557 | } |
| 558 | |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 559 | const CommonConnectJobParams dummy_connect_job_params_; |
| 560 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 561 | // These clocks are defined here, even though they're only used in the |
| 562 | // Reporting tests below, since they need to be destroyed after |
| 563 | // |session_deps_|. |
| 564 | base::SimpleTestClock clock_; |
| 565 | base::SimpleTestTickClock tick_clock_; |
| 566 | |
[email protected] | 4bd4622 | 2013-05-14 19:32:23 | [diff] [blame] | 567 | SpdyTestUtil spdy_util_; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 568 | SpdySessionDependencies session_deps_; |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 569 | SSLSocketDataProvider ssl_; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 570 | |
| 571 | // Original socket limits. Some tests set these. Safest to always restore |
| 572 | // them once each test has been run. |
| 573 | int old_max_group_sockets_; |
| 574 | int old_max_pool_sockets_; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 575 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 576 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 577 | namespace { |
| 578 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 579 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 580 | void FillLargeHeadersString(std::string* str, int size) { |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 581 | const char row[] = |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 582 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 583 | const int sizeof_row = strlen(row); |
| 584 | const int num_rows = static_cast<int>( |
| 585 | ceil(static_cast<float>(size) / sizeof_row)); |
| 586 | const int sizeof_data = num_rows * sizeof_row; |
| 587 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 588 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 589 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 590 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 591 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 592 | } |
| 593 | |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 594 | #if defined(NTLM_PORTABLE) |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 595 | uint64_t MockGetMSTime() { |
| 596 | // Tue, 23 May 2017 20:13:07 +0000 |
| 597 | return 131400439870000000; |
| 598 | } |
| 599 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 600 | // Alternative functions that eliminate randomness and dependency on the local |
| 601 | // host name so that the generated NTLM messages are reproducible. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 602 | void MockGenerateRandom(uint8_t* output, size_t n) { |
| 603 | // This is set to 0xaa because the client challenge for testing in |
| 604 | // [MS-NLMP] Section 4.2.1 is 8 bytes of 0xaa. |
| 605 | memset(output, 0xaa, n); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 608 | std::string MockGetHostName() { |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 609 | return ntlm::test::kHostnameAscii; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 610 | } |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 611 | #endif // defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 612 | |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 613 | class CaptureGroupIdTransportSocketPool : public TransportClientSocketPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 614 | public: |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 615 | explicit CaptureGroupIdTransportSocketPool( |
| 616 | const CommonConnectJobParams* common_connect_job_params) |
| 617 | : TransportClientSocketPool(0, |
| 618 | 0, |
| 619 | base::TimeDelta(), |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 620 | ProxyServer::Direct(), |
| 621 | false /* is_for_websockets */, |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 622 | common_connect_job_params) {} |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 623 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 624 | const ClientSocketPool::GroupId& last_group_id_received() const { |
| 625 | return last_group_id_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 626 | } |
| 627 | |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 628 | bool socket_requested() const { return socket_requested_; } |
| 629 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 630 | int RequestSocket( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 631 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 632 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 633 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 634 | RequestPriority priority, |
| 635 | const SocketTag& socket_tag, |
| 636 | ClientSocketPool::RespectLimits respect_limits, |
| 637 | ClientSocketHandle* handle, |
| 638 | CompletionOnceCallback callback, |
| 639 | const ClientSocketPool::ProxyAuthCallback& proxy_auth_callback, |
| 640 | const NetLogWithSource& net_log) override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 641 | last_group_id_ = group_id; |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 642 | socket_requested_ = true; |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 643 | return ERR_IO_PENDING; |
| 644 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 645 | void CancelRequest(const ClientSocketPool::GroupId& group_id, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 646 | ClientSocketHandle* handle, |
| 647 | bool cancel_connect_job) override {} |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 648 | void ReleaseSocket(const ClientSocketPool::GroupId& group_id, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 649 | std::unique_ptr<StreamSocket> socket, |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 650 | int64_t generation) override {} |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 651 | void CloseIdleSockets(const char* net_log_reason_utf8) override {} |
| 652 | void CloseIdleSocketsInGroup(const ClientSocketPool::GroupId& group_id, |
| 653 | const char* net_log_reason_utf8) override {} |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 654 | int IdleSocketCount() const override { return 0; } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 655 | size_t IdleSocketCountInGroup( |
| 656 | const ClientSocketPool::GroupId& group_id) const override { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 657 | return 0; |
| 658 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 659 | LoadState GetLoadState(const ClientSocketPool::GroupId& group_id, |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 660 | const ClientSocketHandle* handle) const override { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 661 | return LOAD_STATE_IDLE; |
| 662 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 663 | |
| 664 | private: |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 665 | ClientSocketPool::GroupId last_group_id_; |
Tarun Bansal | 162eabe5 | 2018-01-20 01:16:39 | [diff] [blame] | 666 | bool socket_requested_ = false; |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 667 | }; |
| 668 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 669 | //----------------------------------------------------------------------------- |
| 670 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 671 | // Helper functions for validating that AuthChallengeInfo's are correctly |
| 672 | // configured for common cases. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 673 | bool CheckBasicServerAuth( |
| 674 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 675 | if (!auth_challenge) |
| 676 | return false; |
| 677 | EXPECT_FALSE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 678 | EXPECT_EQ("http://www.example.org", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 679 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 680 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 681 | return true; |
| 682 | } |
| 683 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 684 | bool CheckBasicSecureServerAuth( |
| 685 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
| 686 | if (!auth_challenge) |
| 687 | return false; |
| 688 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 689 | EXPECT_EQ("https://www.example.org", auth_challenge->challenger.Serialize()); |
| 690 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 691 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
| 692 | return true; |
| 693 | } |
| 694 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 695 | bool CheckBasicProxyAuth( |
| 696 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 697 | if (!auth_challenge) |
| 698 | return false; |
| 699 | EXPECT_TRUE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 700 | EXPECT_EQ("http://myproxy:70", auth_challenge->challenger.Serialize()); |
| 701 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 702 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
| 703 | return true; |
| 704 | } |
| 705 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 706 | bool CheckBasicSecureProxyAuth( |
| 707 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 708 | if (!auth_challenge) |
| 709 | return false; |
| 710 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 711 | EXPECT_EQ("https://myproxy:70", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 712 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 713 | EXPECT_EQ(kBasicAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 714 | return true; |
| 715 | } |
| 716 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 717 | bool CheckDigestServerAuth( |
| 718 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 719 | if (!auth_challenge) |
| 720 | return false; |
| 721 | EXPECT_FALSE(auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 722 | EXPECT_EQ("http://www.example.org", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 723 | EXPECT_EQ("digestive", auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 724 | EXPECT_EQ(kDigestAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 725 | return true; |
| 726 | } |
| 727 | |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 728 | #if defined(NTLM_PORTABLE) |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 729 | bool CheckNTLMServerAuth( |
| 730 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 731 | if (!auth_challenge) |
| 732 | return false; |
| 733 | EXPECT_FALSE(auth_challenge->is_proxy); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 734 | EXPECT_EQ("https://server", auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 735 | EXPECT_EQ(std::string(), auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 736 | EXPECT_EQ(kNtlmAuthScheme, auth_challenge->scheme); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 737 | return true; |
| 738 | } |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 739 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 740 | bool CheckNTLMProxyAuth( |
| 741 | const base::Optional<AuthChallengeInfo>& auth_challenge) { |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 742 | if (!auth_challenge) |
| 743 | return false; |
| 744 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 745 | EXPECT_EQ("http://server", auth_challenge->challenger.Serialize()); |
| 746 | EXPECT_EQ(std::string(), auth_challenge->realm); |
| 747 | EXPECT_EQ(kNtlmAuthScheme, auth_challenge->scheme); |
| 748 | return true; |
| 749 | } |
thakis | 84dff94 | 2015-07-28 20:47:38 | [diff] [blame] | 750 | #endif // defined(NTLM_PORTABLE) |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 751 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 752 | } // namespace |
| 753 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 754 | // TODO(950069): Add testing for frame_origin in NetworkIsolationKey |
| 755 | // using kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| 756 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 757 | TEST_F(HttpNetworkTransactionTest, Basic) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 758 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 759 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 760 | } |
| 761 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 762 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 763 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 764 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 765 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 766 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 767 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 768 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 769 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 770 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 771 | EXPECT_EQ("hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 772 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 773 | EXPECT_EQ(reads_size, out.total_received_bytes); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 774 | EXPECT_EQ(0u, out.connection_attempts.size()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 775 | |
| 776 | EXPECT_FALSE(out.remote_endpoint_after_start.address().empty()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | // Response with no status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 780 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 781 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 782 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 783 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 784 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 785 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 786 | EXPECT_THAT(out.rv, IsOk()); |
| 787 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 788 | EXPECT_EQ("hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 789 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 790 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 791 | } |
| 792 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 793 | // Response with no status line, and a weird port. Should fail by default. |
| 794 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeadersWeirdPort) { |
| 795 | MockRead data_reads[] = { |
| 796 | MockRead("hello world"), MockRead(SYNCHRONOUS, OK), |
| 797 | }; |
| 798 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 799 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 800 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 801 | |
| 802 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 803 | |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 804 | HttpRequestInfo request; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 805 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 806 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 807 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 808 | request.method = "GET"; |
| 809 | request.url = GURL("http://www.example.com:2000/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 810 | request.traffic_annotation = |
| 811 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 812 | |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 813 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 814 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
mmenke | a7da6da | 2016-09-01 21:56:52 | [diff] [blame] | 815 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_INVALID_HTTP_RESPONSE)); |
| 816 | } |
| 817 | |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 818 | // Tests that request info can be destroyed after the headers phase is complete. |
| 819 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoReadDestroyRequestInfo) { |
| 820 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 821 | auto trans = |
| 822 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 823 | |
| 824 | MockRead data_reads[] = { |
| 825 | MockRead("HTTP/1.0 200 OK\r\n"), MockRead("Connection: keep-alive\r\n"), |
| 826 | MockRead("Content-Length: 100\r\n\r\n"), MockRead(SYNCHRONOUS, 0), |
| 827 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 828 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 829 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 830 | |
| 831 | TestCompletionCallback callback; |
| 832 | |
| 833 | { |
| 834 | auto request = std::make_unique<HttpRequestInfo>(); |
| 835 | request->method = "GET"; |
| 836 | request->url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 837 | request->traffic_annotation = |
| 838 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 839 | |
| 840 | int rv = |
| 841 | trans->Start(request.get(), callback.callback(), NetLogWithSource()); |
| 842 | |
| 843 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 844 | } // Let request info be destroyed. |
| 845 | |
| 846 | trans.reset(); |
| 847 | } |
| 848 | |
dalyk | 6d7a8c5 | 2019-12-18 21:43:01 | [diff] [blame] | 849 | // Test that a failure in resolving the hostname is retrievable. |
| 850 | TEST_F(HttpNetworkTransactionTest, SimpleGETHostResolutionFailure) { |
| 851 | HttpRequestInfo request; |
| 852 | request.method = "GET"; |
| 853 | request.url = GURL("http://www.example.org/"); |
| 854 | request.traffic_annotation = |
| 855 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 856 | |
| 857 | RecordingTestNetLog log; |
| 858 | MockHostResolver* resolver = new MockHostResolver(); |
| 859 | resolver->rules()->AddSimulatedTimeoutFailure("www.example.org"); |
| 860 | session_deps_.net_log = &log; |
| 861 | session_deps_.host_resolver.reset(resolver); |
| 862 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 863 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 864 | TestCompletionCallback callback; |
| 865 | |
| 866 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 867 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 868 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NAME_NOT_RESOLVED)); |
| 869 | |
| 870 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 871 | ASSERT_TRUE(response); |
| 872 | EXPECT_THAT(response->resolve_error_info.error, IsError(ERR_DNS_TIMED_OUT)); |
| 873 | } |
| 874 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 875 | // Allow up to 4 bytes of junk to precede status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 876 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk3Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 877 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 878 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 879 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 880 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 881 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 882 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 883 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 884 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 885 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 886 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | // Allow up to 4 bytes of junk to precede status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 890 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 891 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 892 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 893 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 894 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 895 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 896 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 897 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 898 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 899 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 900 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | // Beyond 4 bytes of slop and it should fail to find a status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 904 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 905 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 906 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 907 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 908 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 909 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 910 | EXPECT_THAT(out.rv, IsOk()); |
| 911 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 912 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 913 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 914 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 918 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 919 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 920 | MockRead("\n"), |
| 921 | MockRead("\n"), |
| 922 | MockRead("Q"), |
| 923 | MockRead("J"), |
| 924 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 925 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 926 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 927 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 928 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 929 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 930 | EXPECT_EQ("DATA", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 931 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 932 | EXPECT_EQ(reads_size, out.total_received_bytes); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | // Close the connection before enough bytes to have a status line. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 936 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 937 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 938 | MockRead("HTT"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 939 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 940 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 941 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 942 | EXPECT_THAT(out.rv, IsOk()); |
| 943 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 944 | EXPECT_EQ("HTT", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 945 | int64_t reads_size = CountReadBytes(data_reads); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 946 | EXPECT_EQ(reads_size, out.total_received_bytes); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 947 | } |
| 948 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 949 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 950 | // persistent connection. The response should still terminate since a 204 |
| 951 | // cannot have a response body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 952 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 953 | char junk[] = "junk"; |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 954 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 955 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 956 | MockRead(junk), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 957 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 958 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 959 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 960 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 961 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 962 | EXPECT_EQ("", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 963 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 964 | int64_t response_size = reads_size - strlen(junk); |
| 965 | EXPECT_EQ(response_size, out.total_received_bytes); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 966 | } |
| 967 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 968 | // A simple request using chunked encoding with some extra data after. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 969 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 970 | std::string final_chunk = "0\r\n\r\n"; |
| 971 | std::string extra_data = "HTTP/1.1 200 OK\r\n"; |
| 972 | std::string last_read = final_chunk + extra_data; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 973 | MockRead data_reads[] = { |
| 974 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 975 | MockRead("5\r\nHello\r\n"), |
| 976 | MockRead("1\r\n"), |
| 977 | MockRead(" \r\n"), |
| 978 | MockRead("5\r\nworld\r\n"), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 979 | MockRead(last_read.data()), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 980 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 981 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 982 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 983 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 984 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 985 | EXPECT_EQ("Hello world", out.response_data); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 986 | int64_t reads_size = CountReadBytes(data_reads); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 987 | int64_t response_size = reads_size - extra_data.size(); |
| 988 | EXPECT_EQ(response_size, out.total_received_bytes); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 989 | } |
| 990 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 991 | // Next tests deal with http://crbug.com/56344. |
| 992 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 993 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 994 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 995 | MockRead data_reads[] = { |
| 996 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 997 | MockRead("Content-Length: 10\r\n"), |
| 998 | MockRead("Content-Length: 5\r\n\r\n"), |
| 999 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1000 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1001 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH)); |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1002 | } |
| 1003 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1004 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1005 | DuplicateContentLengthHeadersNoTransferEncoding) { |
| 1006 | MockRead data_reads[] = { |
| 1007 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1008 | MockRead("Content-Length: 5\r\n"), |
| 1009 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1010 | MockRead("Hello"), |
| 1011 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1012 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1013 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1014 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1015 | EXPECT_EQ("Hello", out.response_data); |
| 1016 | } |
| 1017 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1018 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1019 | ComplexContentLengthHeadersNoTransferEncoding) { |
| 1020 | // More than 2 dupes. |
| 1021 | { |
| 1022 | MockRead data_reads[] = { |
| 1023 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1024 | MockRead("Content-Length: 5\r\n"), |
| 1025 | MockRead("Content-Length: 5\r\n"), |
| 1026 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1027 | MockRead("Hello"), |
| 1028 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1029 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1030 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1031 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1032 | EXPECT_EQ("Hello", out.response_data); |
| 1033 | } |
| 1034 | // HTTP/1.0 |
| 1035 | { |
| 1036 | MockRead data_reads[] = { |
| 1037 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1038 | MockRead("Content-Length: 5\r\n"), |
| 1039 | MockRead("Content-Length: 5\r\n"), |
| 1040 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1041 | MockRead("Hello"), |
| 1042 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1043 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1044 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1045 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 1046 | EXPECT_EQ("Hello", out.response_data); |
| 1047 | } |
| 1048 | // 2 dupes and one mismatched. |
| 1049 | { |
| 1050 | MockRead data_reads[] = { |
| 1051 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1052 | MockRead("Content-Length: 10\r\n"), |
| 1053 | MockRead("Content-Length: 10\r\n"), |
| 1054 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1055 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1056 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1057 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH)); |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1061 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1062 | MultipleContentLengthHeadersTransferEncoding) { |
| 1063 | MockRead data_reads[] = { |
| 1064 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1065 | MockRead("Content-Length: 666\r\n"), |
| 1066 | MockRead("Content-Length: 1337\r\n"), |
| 1067 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 1068 | MockRead("5\r\nHello\r\n"), |
| 1069 | MockRead("1\r\n"), |
| 1070 | MockRead(" \r\n"), |
| 1071 | MockRead("5\r\nworld\r\n"), |
| 1072 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1073 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1074 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1075 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1076 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 1077 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1078 | EXPECT_EQ("Hello world", out.response_data); |
| 1079 | } |
| 1080 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1081 | // Next tests deal with http://crbug.com/98895. |
| 1082 | |
| 1083 | // Checks that a single Content-Disposition header results in no error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1084 | TEST_F(HttpNetworkTransactionTest, SingleContentDispositionHeader) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1085 | MockRead data_reads[] = { |
| 1086 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1087 | MockRead("Content-Disposition: attachment;filename=\"salutations.txt\"r\n"), |
| 1088 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1089 | MockRead("Hello"), |
| 1090 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1091 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1092 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1093 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1094 | EXPECT_EQ("Hello", out.response_data); |
| 1095 | } |
| 1096 | |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1097 | // Checks that two identical Content-Disposition headers result in no error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1098 | TEST_F(HttpNetworkTransactionTest, TwoIdenticalContentDispositionHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1099 | MockRead data_reads[] = { |
| 1100 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1101 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1102 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1103 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1104 | MockRead("Hello"), |
| 1105 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1106 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1107 | EXPECT_THAT(out.rv, IsOk()); |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1108 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 1109 | EXPECT_EQ("Hello", out.response_data); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | // Checks that two distinct Content-Disposition headers result in an error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1113 | TEST_F(HttpNetworkTransactionTest, TwoDistinctContentDispositionHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1114 | MockRead data_reads[] = { |
| 1115 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1116 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 1117 | MockRead("Content-Disposition: attachment;filename=\"hi.txt\"r\n"), |
| 1118 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1119 | MockRead("Hello"), |
| 1120 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1121 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1122 | EXPECT_THAT(out.rv, |
| 1123 | IsError(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1124 | } |
| 1125 | |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1126 | // Checks that two identical Location headers result in no error. |
| 1127 | // Also tests Location header behavior. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1128 | TEST_F(HttpNetworkTransactionTest, TwoIdenticalLocationHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1129 | MockRead data_reads[] = { |
| 1130 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 1131 | MockRead("Location: http://good.com/\r\n"), |
[email protected] | 54a9c6e5 | 2012-03-21 20:10:59 | [diff] [blame] | 1132 | MockRead("Location: http://good.com/\r\n"), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1133 | MockRead("Content-Length: 0\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1134 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1135 | }; |
| 1136 | |
| 1137 | HttpRequestInfo request; |
| 1138 | request.method = "GET"; |
| 1139 | request.url = GURL("http://redirect.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1140 | request.traffic_annotation = |
| 1141 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1142 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1143 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1144 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1145 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1146 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1147 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1148 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1149 | TestCompletionCallback callback; |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1150 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1151 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1152 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1153 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1154 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1155 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1156 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1157 | ASSERT_TRUE(response); |
| 1158 | ASSERT_TRUE(response->headers); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1159 | EXPECT_EQ("HTTP/1.1 302 Redirect", response->headers->GetStatusLine()); |
| 1160 | std::string url; |
| 1161 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 1162 | EXPECT_EQ("http://good.com/", url); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1163 | EXPECT_TRUE(response->proxy_server.is_direct()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1164 | } |
| 1165 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1166 | // Checks that two distinct Location headers result in an error. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1167 | TEST_F(HttpNetworkTransactionTest, TwoDistinctLocationHeaders) { |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1168 | MockRead data_reads[] = { |
| 1169 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 1170 | MockRead("Location: http://good.com/\r\n"), |
| 1171 | MockRead("Location: http://evil.com/\r\n"), |
| 1172 | MockRead("Content-Length: 0\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1173 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1174 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1175 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1176 | EXPECT_THAT(out.rv, IsError(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION)); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 1177 | } |
| 1178 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1179 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 1180 | // message body (since HEAD has none). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1181 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1182 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1183 | request.method = "HEAD"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 1184 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1185 | request.traffic_annotation = |
| 1186 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1187 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1188 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1189 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1190 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1191 | MockWrite data_writes1[] = { |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 1192 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 1193 | "Host: www.example.org\r\n" |
| 1194 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1195 | }; |
| 1196 | MockRead data_reads1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 1197 | MockRead("HTTP/1.1 404 Not Found\r\n"), MockRead("Server: Blah\r\n"), |
| 1198 | MockRead("Content-Length: 1234\r\n\r\n"), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1199 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 1200 | // No response body because the test stops reading here. |
| 1201 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1202 | }; |
| 1203 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1204 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1205 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1206 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1207 | TestCompletionCallback callback1; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1208 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1209 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1210 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1211 | |
| 1212 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1213 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1214 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1215 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1216 | ASSERT_TRUE(response); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1217 | |
| 1218 | // Check that the headers got parsed. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1219 | EXPECT_TRUE(response->headers); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1220 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 1221 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1222 | EXPECT_TRUE(response->proxy_server.is_direct()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1223 | |
| 1224 | std::string server_header; |
olli.raula | ee489a5 | 2016-01-25 08:37:10 | [diff] [blame] | 1225 | size_t iter = 0; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1226 | bool has_server_header = response->headers->EnumerateHeader( |
| 1227 | &iter, "Server", &server_header); |
| 1228 | EXPECT_TRUE(has_server_header); |
| 1229 | EXPECT_EQ("Blah", server_header); |
| 1230 | |
| 1231 | // Reading should give EOF right away, since there is no message body |
| 1232 | // (despite non-zero content-length). |
| 1233 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1234 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1235 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1236 | EXPECT_EQ("", response_data); |
| 1237 | } |
| 1238 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1239 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1240 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1241 | |
| 1242 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1243 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1244 | MockRead("hello"), |
| 1245 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1246 | MockRead("world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1247 | MockRead(SYNCHRONOUS, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1248 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1249 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1250 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1251 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1252 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1253 | "hello", "world" |
| 1254 | }; |
| 1255 | |
| 1256 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1257 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1258 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 1259 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1260 | request.traffic_annotation = |
| 1261 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1262 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1263 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1264 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1265 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1266 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1267 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1268 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1269 | |
| 1270 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1271 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1272 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1273 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1274 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1275 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1276 | EXPECT_TRUE(response->headers); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1277 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1278 | EXPECT_TRUE(response->proxy_server.is_direct()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1279 | |
| 1280 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1281 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1282 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1283 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1287 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1288 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1289 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 1290 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 1291 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 1292 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1293 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1294 | request.method = "POST"; |
| 1295 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 1296 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1297 | request.traffic_annotation = |
| 1298 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1299 | |
shivanisha | b9a14395 | 2016-09-19 17:23:41 | [diff] [blame] | 1300 | // Check the upload progress returned before initialization is correct. |
| 1301 | UploadProgress progress = request.upload_data_stream->GetUploadProgress(); |
| 1302 | EXPECT_EQ(0u, progress.size()); |
| 1303 | EXPECT_EQ(0u, progress.position()); |
| 1304 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1305 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1306 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1307 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1308 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1309 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 1310 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 1311 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1312 | MockRead(SYNCHRONOUS, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1313 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1314 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1315 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1316 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1317 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1318 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1319 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1320 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1321 | |
| 1322 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1323 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1324 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1325 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1326 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1327 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1328 | EXPECT_TRUE(response->headers); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1329 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1330 | |
| 1331 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1332 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1333 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1334 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1335 | } |
| 1336 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1337 | // This test is almost the same as Ignores100 above, but the response contains |
| 1338 | // a 102 instead of a 100. Also, instead of HTTP/1.0 the response is |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1339 | // HTTP/1.1 and the two status headers are read in one read. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1340 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1341 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1342 | request.method = "GET"; |
| 1343 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1344 | request.traffic_annotation = |
| 1345 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1346 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1347 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1348 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1349 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1350 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1351 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 1352 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1353 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1354 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1355 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1356 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1357 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1358 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1359 | TestCompletionCallback callback; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1360 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1361 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1362 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1363 | |
| 1364 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1365 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1366 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1367 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1368 | ASSERT_TRUE(response); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1369 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1370 | EXPECT_TRUE(response->headers); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1371 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1372 | |
| 1373 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1374 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1375 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 1376 | EXPECT_EQ("hello world", response_data); |
| 1377 | } |
| 1378 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1379 | TEST_F(HttpNetworkTransactionTest, LoadTimingMeasuresTimeToFirstByteForHttp) { |
| 1380 | static const base::TimeDelta kDelayAfterFirstByte = |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1381 | base::TimeDelta::FromMilliseconds(10); |
| 1382 | |
| 1383 | HttpRequestInfo request; |
| 1384 | request.method = "GET"; |
| 1385 | request.url = GURL("http://www.foo.com/"); |
| 1386 | request.traffic_annotation = |
| 1387 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 1388 | |
| 1389 | std::vector<MockWrite> data_writes = { |
| 1390 | MockWrite(ASYNC, 0, |
| 1391 | "GET / HTTP/1.1\r\n" |
| 1392 | "Host: www.foo.com\r\n" |
| 1393 | "Connection: keep-alive\r\n\r\n"), |
| 1394 | }; |
| 1395 | |
| 1396 | std::vector<MockRead> data_reads = { |
| 1397 | // Write one byte of the status line, followed by a pause. |
| 1398 | MockRead(ASYNC, 1, "H"), |
| 1399 | MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1400 | MockRead(ASYNC, 3, "TTP/1.1 200 OK\r\n\r\n"), |
| 1401 | MockRead(ASYNC, 4, "hello world"), |
| 1402 | MockRead(SYNCHRONOUS, OK, 5), |
| 1403 | }; |
| 1404 | |
| 1405 | SequencedSocketData data(data_reads, data_writes); |
| 1406 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 1407 | |
| 1408 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1409 | |
| 1410 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1411 | |
| 1412 | TestCompletionCallback callback; |
| 1413 | |
| 1414 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1415 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1416 | |
| 1417 | data.RunUntilPaused(); |
| 1418 | ASSERT_TRUE(data.IsPaused()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1419 | FastForwardBy(kDelayAfterFirstByte); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1420 | data.Resume(); |
| 1421 | |
| 1422 | rv = callback.WaitForResult(); |
| 1423 | EXPECT_THAT(rv, IsOk()); |
| 1424 | |
| 1425 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 1426 | ASSERT_TRUE(response); |
| 1427 | |
| 1428 | EXPECT_TRUE(response->headers); |
| 1429 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1430 | |
| 1431 | LoadTimingInfo load_timing_info; |
| 1432 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
| 1433 | EXPECT_FALSE(load_timing_info.receive_headers_start.is_null()); |
| 1434 | EXPECT_FALSE(load_timing_info.connect_timing.connect_end.is_null()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1435 | // Ensure we didn't include the delay in the TTFB time. |
| 1436 | EXPECT_EQ(load_timing_info.receive_headers_start, |
| 1437 | load_timing_info.connect_timing.connect_end); |
| 1438 | // Ensure that the mock clock advanced at all. |
| 1439 | EXPECT_EQ(base::TimeTicks::Now() - load_timing_info.receive_headers_start, |
| 1440 | kDelayAfterFirstByte); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1441 | |
| 1442 | std::string response_data; |
| 1443 | rv = ReadTransaction(&trans, &response_data); |
| 1444 | EXPECT_THAT(rv, IsOk()); |
| 1445 | EXPECT_EQ("hello world", response_data); |
| 1446 | } |
| 1447 | |
| 1448 | // Tests that the time-to-first-byte reported in a transaction's load timing |
| 1449 | // info uses the first response, even if 1XX/informational. |
| 1450 | void HttpNetworkTransactionTest::Check100ResponseTiming(bool use_spdy) { |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1451 | static const base::TimeDelta kDelayAfter100Response = |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1452 | base::TimeDelta::FromMilliseconds(10); |
| 1453 | |
| 1454 | HttpRequestInfo request; |
| 1455 | request.method = "GET"; |
| 1456 | request.url = GURL("https://www.foo.com/"); |
| 1457 | request.traffic_annotation = |
| 1458 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 1459 | |
| 1460 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 1461 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 1462 | |
| 1463 | std::vector<MockWrite> data_writes; |
| 1464 | std::vector<MockRead> data_reads; |
| 1465 | |
| 1466 | spdy::SpdySerializedFrame spdy_req( |
| 1467 | spdy_util_.ConstructSpdyGet(request.url.spec().c_str(), 1, LOWEST)); |
| 1468 | |
| 1469 | spdy::SpdyHeaderBlock spdy_resp1_headers; |
| 1470 | spdy_resp1_headers[spdy::kHttp2StatusHeader] = "100"; |
| 1471 | spdy::SpdySerializedFrame spdy_resp1( |
| 1472 | spdy_util_.ConstructSpdyReply(1, spdy_resp1_headers.Clone())); |
| 1473 | spdy::SpdySerializedFrame spdy_resp2( |
| 1474 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 1475 | spdy::SpdySerializedFrame spdy_data( |
| 1476 | spdy_util_.ConstructSpdyDataFrame(1, "hello world", true)); |
| 1477 | |
| 1478 | if (use_spdy) { |
| 1479 | ssl.next_proto = kProtoHTTP2; |
| 1480 | |
| 1481 | data_writes = {CreateMockWrite(spdy_req, 0)}; |
| 1482 | |
| 1483 | data_reads = { |
| 1484 | CreateMockRead(spdy_resp1, 1), MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1485 | CreateMockRead(spdy_resp2, 3), CreateMockRead(spdy_data, 4), |
| 1486 | MockRead(SYNCHRONOUS, OK, 5), |
| 1487 | }; |
| 1488 | } else { |
| 1489 | data_writes = { |
| 1490 | MockWrite(ASYNC, 0, |
| 1491 | "GET / HTTP/1.1\r\n" |
| 1492 | "Host: www.foo.com\r\n" |
| 1493 | "Connection: keep-alive\r\n\r\n"), |
| 1494 | }; |
| 1495 | |
| 1496 | data_reads = { |
| 1497 | MockRead(ASYNC, 1, "HTTP/1.1 100 Continue\r\n\r\n"), |
| 1498 | MockRead(ASYNC, ERR_IO_PENDING, 2), |
| 1499 | |
| 1500 | MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n\r\n"), |
| 1501 | MockRead(ASYNC, 4, "hello world"), |
| 1502 | MockRead(SYNCHRONOUS, OK, 5), |
| 1503 | }; |
| 1504 | } |
| 1505 | |
| 1506 | SequencedSocketData data(data_reads, data_writes); |
| 1507 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 1508 | |
| 1509 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1510 | |
| 1511 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1512 | |
| 1513 | TestCompletionCallback callback; |
| 1514 | |
| 1515 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1516 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1517 | |
| 1518 | data.RunUntilPaused(); |
| 1519 | // We should now have parsed the 100 response and hit ERR_IO_PENDING. Insert |
| 1520 | // the delay before parsing the 200 response. |
| 1521 | ASSERT_TRUE(data.IsPaused()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1522 | FastForwardBy(kDelayAfter100Response); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1523 | data.Resume(); |
| 1524 | |
| 1525 | rv = callback.WaitForResult(); |
| 1526 | EXPECT_THAT(rv, IsOk()); |
| 1527 | |
| 1528 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 1529 | ASSERT_TRUE(response); |
| 1530 | |
| 1531 | LoadTimingInfo load_timing_info; |
| 1532 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
| 1533 | EXPECT_FALSE(load_timing_info.receive_headers_start.is_null()); |
| 1534 | EXPECT_FALSE(load_timing_info.connect_timing.connect_end.is_null()); |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1535 | // Ensure we didn't include the delay in the TTFB time. |
| 1536 | EXPECT_EQ(load_timing_info.receive_headers_start, |
| 1537 | load_timing_info.connect_timing.connect_end); |
| 1538 | // Ensure that the mock clock advanced at all. |
| 1539 | EXPECT_EQ(base::TimeTicks::Now() - load_timing_info.receive_headers_start, |
| 1540 | kDelayAfter100Response); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1541 | |
| 1542 | std::string response_data; |
| 1543 | rv = ReadTransaction(&trans, &response_data); |
| 1544 | EXPECT_THAT(rv, IsOk()); |
| 1545 | EXPECT_EQ("hello world", response_data); |
| 1546 | } |
| 1547 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1548 | TEST_F(HttpNetworkTransactionTest, MeasuresTimeToFirst100ResponseForHttp) { |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1549 | Check100ResponseTiming(false /* use_spdy */); |
| 1550 | } |
| 1551 | |
Andrew Comminos | 517a92c | 2019-01-14 17:49:56 | [diff] [blame] | 1552 | TEST_F(HttpNetworkTransactionTest, MeasuresTimeToFirst100ResponseForSpdy) { |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 1553 | Check100ResponseTiming(true /* use_spdy */); |
| 1554 | } |
| 1555 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1556 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1557 | HttpRequestInfo request; |
| 1558 | request.method = "POST"; |
| 1559 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1560 | request.traffic_annotation = |
| 1561 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1562 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1563 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1564 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1565 | |
| 1566 | MockRead data_reads[] = { |
| 1567 | MockRead(SYNCHRONOUS, "HTTP/1.0 100 Continue\r\n"), |
| 1568 | MockRead(ASYNC, 0), |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1569 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1570 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1571 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1572 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1573 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1574 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1575 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1576 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1577 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1578 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1579 | EXPECT_THAT(rv, IsOk()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1580 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1581 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1582 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1583 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 1584 | EXPECT_EQ("", response_data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1585 | } |
| 1586 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1587 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1588 | HttpRequestInfo request; |
| 1589 | request.method = "POST"; |
| 1590 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1591 | request.traffic_annotation = |
| 1592 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1593 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1594 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1595 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1596 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1597 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1598 | MockRead(ASYNC, 0), |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1599 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1600 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1601 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1602 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1603 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1604 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1605 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1606 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1607 | |
| 1608 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1609 | EXPECT_THAT(rv, IsError(ERR_EMPTY_RESPONSE)); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1610 | } |
| 1611 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 1612 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1613 | const MockWrite* write_failure, |
| 1614 | const MockRead* read_failure) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1615 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1616 | request.method = "GET"; |
| 1617 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1618 | request.traffic_annotation = |
| 1619 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1620 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1621 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1622 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1623 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1624 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1625 | // Written data for successfully sending both requests. |
| 1626 | MockWrite data1_writes[] = { |
| 1627 | MockWrite("GET / HTTP/1.1\r\n" |
| 1628 | "Host: www.foo.com\r\n" |
| 1629 | "Connection: keep-alive\r\n\r\n"), |
| 1630 | MockWrite("GET / HTTP/1.1\r\n" |
| 1631 | "Host: www.foo.com\r\n" |
| 1632 | "Connection: keep-alive\r\n\r\n") |
| 1633 | }; |
| 1634 | |
| 1635 | // Read results for the first request. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1636 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1637 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1638 | MockRead("hello"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1639 | MockRead(ASYNC, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1640 | }; |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1641 | |
| 1642 | if (write_failure) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1643 | ASSERT_FALSE(read_failure); |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1644 | data1_writes[1] = *write_failure; |
| 1645 | } else { |
| 1646 | ASSERT_TRUE(read_failure); |
| 1647 | data1_reads[2] = *read_failure; |
| 1648 | } |
| 1649 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1650 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1651 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1652 | |
| 1653 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1654 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1655 | MockRead("world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1656 | MockRead(ASYNC, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1657 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1658 | StaticSocketDataProvider data2(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 1659 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1660 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 1661 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1662 | "hello", "world" |
| 1663 | }; |
| 1664 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 1665 | uint32_t first_socket_log_id = NetLogSource::kInvalidId; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1666 | for (int i = 0; i < 2; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1667 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1668 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1669 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1670 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1671 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1672 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1673 | |
| 1674 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1675 | EXPECT_THAT(rv, IsOk()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1676 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1677 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1678 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1679 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1680 | if (i == 0) { |
| 1681 | first_socket_log_id = load_timing_info.socket_log_id; |
| 1682 | } else { |
| 1683 | // The second request should be using a new socket. |
| 1684 | EXPECT_NE(first_socket_log_id, load_timing_info.socket_log_id); |
| 1685 | } |
| 1686 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1687 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1688 | ASSERT_TRUE(response); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1689 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1690 | EXPECT_TRUE(response->headers); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 1691 | EXPECT_TRUE(response->proxy_server.is_direct()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1692 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1693 | |
| 1694 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1695 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1696 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1697 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1698 | } |
| 1699 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1700 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1701 | void HttpNetworkTransactionTest::PreconnectErrorResendRequestTest( |
| 1702 | const MockWrite* write_failure, |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1703 | const MockRead* read_failure, |
| 1704 | bool use_spdy) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1705 | HttpRequestInfo request; |
| 1706 | request.method = "GET"; |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1707 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1708 | request.traffic_annotation = |
| 1709 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1710 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1711 | RecordingTestNetLog net_log; |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1712 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1713 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1714 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1715 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 1716 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 1717 | if (use_spdy) { |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 1718 | ssl1.next_proto = kProtoHTTP2; |
| 1719 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1720 | } |
| 1721 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 1722 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1723 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1724 | // SPDY versions of the request and response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1725 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 1726 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1727 | spdy::SpdySerializedFrame spdy_response( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1728 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1729 | spdy::SpdySerializedFrame spdy_data( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 1730 | spdy_util_.ConstructSpdyDataFrame(1, "hello", true)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1731 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1732 | // HTTP/1.1 versions of the request and response. |
| 1733 | const char kHttpRequest[] = "GET / HTTP/1.1\r\n" |
| 1734 | "Host: www.foo.com\r\n" |
| 1735 | "Connection: keep-alive\r\n\r\n"; |
| 1736 | const char kHttpResponse[] = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"; |
| 1737 | const char kHttpData[] = "hello"; |
| 1738 | |
| 1739 | std::vector<MockRead> data1_reads; |
| 1740 | std::vector<MockWrite> data1_writes; |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1741 | if (write_failure) { |
| 1742 | ASSERT_FALSE(read_failure); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1743 | data1_writes.push_back(*write_failure); |
| 1744 | data1_reads.push_back(MockRead(ASYNC, OK)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1745 | } else { |
| 1746 | ASSERT_TRUE(read_failure); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1747 | if (use_spdy) { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1748 | data1_writes.push_back(CreateMockWrite(spdy_request)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1749 | } else { |
| 1750 | data1_writes.push_back(MockWrite(kHttpRequest)); |
| 1751 | } |
| 1752 | data1_reads.push_back(*read_failure); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1753 | } |
| 1754 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1755 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1756 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1757 | |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1758 | std::vector<MockRead> data2_reads; |
| 1759 | std::vector<MockWrite> data2_writes; |
| 1760 | |
| 1761 | if (use_spdy) { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1762 | data2_writes.push_back(CreateMockWrite(spdy_request, 0, ASYNC)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1763 | |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 1764 | data2_reads.push_back(CreateMockRead(spdy_response, 1, ASYNC)); |
| 1765 | data2_reads.push_back(CreateMockRead(spdy_data, 2, ASYNC)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1766 | data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1767 | } else { |
| 1768 | data2_writes.push_back( |
| 1769 | MockWrite(ASYNC, kHttpRequest, strlen(kHttpRequest), 0)); |
| 1770 | |
| 1771 | data2_reads.push_back( |
| 1772 | MockRead(ASYNC, kHttpResponse, strlen(kHttpResponse), 1)); |
| 1773 | data2_reads.push_back(MockRead(ASYNC, kHttpData, strlen(kHttpData), 2)); |
| 1774 | data2_reads.push_back(MockRead(ASYNC, OK, 3)); |
| 1775 | } |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1776 | SequencedSocketData data2(data2_reads, data2_writes); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1777 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1778 | |
| 1779 | // Preconnect a socket. |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 1780 | session->http_stream_factory()->PreconnectStreams(1, request); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1781 | // Wait for the preconnect to complete. |
| 1782 | // TODO(davidben): Some way to wait for an idle socket count might be handy. |
| 1783 | base::RunLoop().RunUntilIdle(); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 1784 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1785 | |
| 1786 | // Make the request. |
| 1787 | TestCompletionCallback callback; |
| 1788 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1789 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1790 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 1791 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1792 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1793 | |
| 1794 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1795 | EXPECT_THAT(rv, IsOk()); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1796 | |
| 1797 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1798 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1799 | TestLoadTimingNotReused( |
| 1800 | load_timing_info, |
| 1801 | CONNECT_TIMING_HAS_DNS_TIMES|CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1802 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1803 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1804 | ASSERT_TRUE(response); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1805 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 1806 | EXPECT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 1807 | if (response->was_fetched_via_spdy) { |
| 1808 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 1809 | } else { |
| 1810 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1811 | } |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1812 | |
| 1813 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1814 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1815 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1816 | EXPECT_EQ(kHttpData, response_data); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1817 | } |
| 1818 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1819 | // Test that we do not retry indefinitely when a server sends an error like |
Bence Béky | d0d6950 | 2019-06-25 19:47:18 | [diff] [blame] | 1820 | // ERR_HTTP2_PING_FAILED, ERR_HTTP2_SERVER_REFUSED_STREAM, |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1821 | // ERR_QUIC_HANDSHAKE_FAILED or ERR_QUIC_PROTOCOL_ERROR. |
| 1822 | TEST_F(HttpNetworkTransactionTest, FiniteRetriesOnIOError) { |
| 1823 | HttpRequestInfo request; |
| 1824 | request.method = "GET"; |
| 1825 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1826 | request.traffic_annotation = |
| 1827 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1828 | |
| 1829 | // Check whether we give up after the third try. |
| 1830 | |
| 1831 | // Construct an HTTP2 request and a "Go away" response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1832 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1833 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1834 | spdy::SpdySerializedFrame spdy_response_go_away( |
| 1835 | spdy_util_.ConstructSpdyGoAway(0)); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1836 | MockRead data_read1[] = {CreateMockRead(spdy_response_go_away)}; |
| 1837 | MockWrite data_write[] = {CreateMockWrite(spdy_request, 0)}; |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1838 | |
| 1839 | // Three go away responses. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1840 | StaticSocketDataProvider data1(data_read1, data_write); |
| 1841 | StaticSocketDataProvider data2(data_read1, data_write); |
| 1842 | StaticSocketDataProvider data3(data_read1, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1843 | |
| 1844 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1845 | AddSSLSocketData(); |
| 1846 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1847 | AddSSLSocketData(); |
| 1848 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 1849 | AddSSLSocketData(); |
| 1850 | |
| 1851 | TestCompletionCallback callback; |
| 1852 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1853 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1854 | |
| 1855 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1856 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1857 | |
| 1858 | rv = callback.WaitForResult(); |
Bence Béky | d0d6950 | 2019-06-25 19:47:18 | [diff] [blame] | 1859 | EXPECT_THAT(rv, IsError(ERR_HTTP2_SERVER_REFUSED_STREAM)); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | TEST_F(HttpNetworkTransactionTest, RetryTwiceOnIOError) { |
| 1863 | HttpRequestInfo request; |
| 1864 | request.method = "GET"; |
| 1865 | request.url = GURL("https://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1866 | request.traffic_annotation = |
| 1867 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1868 | |
| 1869 | // Check whether we try atleast thrice before giving up. |
| 1870 | |
| 1871 | // Construct an HTTP2 request and a "Go away" response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1872 | spdy::SpdySerializedFrame spdy_request(spdy_util_.ConstructSpdyGet( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1873 | request.url.spec().c_str(), 1, DEFAULT_PRIORITY)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1874 | spdy::SpdySerializedFrame spdy_response_go_away( |
| 1875 | spdy_util_.ConstructSpdyGoAway(0)); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1876 | MockRead data_read1[] = {CreateMockRead(spdy_response_go_away)}; |
| 1877 | MockWrite data_write[] = {CreateMockWrite(spdy_request, 0)}; |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1878 | |
| 1879 | // Construct a non error HTTP2 response. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1880 | spdy::SpdySerializedFrame spdy_response_no_error( |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1881 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 1882 | spdy::SpdySerializedFrame spdy_data( |
| 1883 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1884 | MockRead data_read2[] = {CreateMockRead(spdy_response_no_error, 1), |
| 1885 | CreateMockRead(spdy_data, 2)}; |
| 1886 | |
| 1887 | // Two error responses. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1888 | StaticSocketDataProvider data1(data_read1, data_write); |
| 1889 | StaticSocketDataProvider data2(data_read1, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1890 | // Followed by a success response. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 1891 | SequencedSocketData data3(data_read2, data_write); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1892 | |
| 1893 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 1894 | AddSSLSocketData(); |
| 1895 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 1896 | AddSSLSocketData(); |
| 1897 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 1898 | AddSSLSocketData(); |
| 1899 | |
| 1900 | TestCompletionCallback callback; |
| 1901 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1902 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 1903 | |
| 1904 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 1905 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1906 | |
| 1907 | rv = callback.WaitForResult(); |
| 1908 | EXPECT_THAT(rv, IsOk()); |
| 1909 | } |
| 1910 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1911 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionNotConnectedOnWrite) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1912 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1913 | KeepAliveConnectionResendRequestTest(&write_failure, nullptr); |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1914 | } |
| 1915 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1916 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1917 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1918 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1919 | } |
| 1920 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1921 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 1922 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1923 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1924 | } |
| 1925 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1926 | // Make sure that on a 408 response (Request Timeout), the request is retried, |
| 1927 | // if the socket was a reused keep alive socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1928 | TEST_F(HttpNetworkTransactionTest, KeepAlive408) { |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1929 | MockRead read_failure(SYNCHRONOUS, |
| 1930 | "HTTP/1.1 408 Request Timeout\r\n" |
| 1931 | "Connection: Keep-Alive\r\n" |
| 1932 | "Content-Length: 6\r\n\r\n" |
| 1933 | "Pickle"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1934 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1935 | } |
| 1936 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1937 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorNotConnectedOnWrite) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1938 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1939 | PreconnectErrorResendRequestTest(&write_failure, nullptr, false); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1940 | } |
| 1941 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1942 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorReset) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1943 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1944 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1945 | } |
| 1946 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1947 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorEOF) { |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1948 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1949 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1950 | } |
| 1951 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1952 | TEST_F(HttpNetworkTransactionTest, PreconnectErrorAsyncEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1953 | MockRead read_failure(ASYNC, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1954 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1955 | } |
| 1956 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1957 | // Make sure that on a 408 response (Request Timeout), the request is retried, |
| 1958 | // if the socket was a preconnected (UNUSED_IDLE) socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1959 | TEST_F(HttpNetworkTransactionTest, RetryOnIdle408) { |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1960 | MockRead read_failure(SYNCHRONOUS, |
| 1961 | "HTTP/1.1 408 Request Timeout\r\n" |
| 1962 | "Connection: Keep-Alive\r\n" |
| 1963 | "Content-Length: 6\r\n\r\n" |
| 1964 | "Pickle"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1965 | KeepAliveConnectionResendRequestTest(nullptr, &read_failure); |
| 1966 | PreconnectErrorResendRequestTest(nullptr, &read_failure, false); |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1967 | } |
| 1968 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1969 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorNotConnectedOnWrite) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1970 | MockWrite write_failure(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1971 | PreconnectErrorResendRequestTest(&write_failure, nullptr, true); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1972 | } |
| 1973 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1974 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorReset) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1975 | MockRead read_failure(ASYNC, ERR_CONNECTION_RESET); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1976 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1977 | } |
| 1978 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1979 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1980 | MockRead read_failure(SYNCHRONOUS, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1981 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1982 | } |
| 1983 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1984 | TEST_F(HttpNetworkTransactionTest, SpdyPreconnectErrorAsyncEOF) { |
[email protected] | 09356c65 | 2014-03-25 15:36:10 | [diff] [blame] | 1985 | MockRead read_failure(ASYNC, OK); // EOF |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1986 | PreconnectErrorResendRequestTest(nullptr, &read_failure, true); |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1987 | } |
| 1988 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 1989 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1990 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1991 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 1992 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 1993 | request.traffic_annotation = |
| 1994 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1995 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 1996 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 1997 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1998 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1999 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2000 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 2001 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 2002 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2003 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2004 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2005 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2006 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2007 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2008 | TestCompletionCallback callback; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2009 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2010 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2011 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2012 | |
| 2013 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2014 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2015 | |
| 2016 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2017 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2018 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | // What do various browsers do when the server closes a non-keepalive |
| 2022 | // connection without sending any response header or body? |
| 2023 | // |
| 2024 | // IE7: error page |
| 2025 | // Safari 3.1.2 (Windows): error page |
| 2026 | // Firefox 3.0.1: blank page |
| 2027 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2028 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 2029 | // Us: error page (EMPTY_RESPONSE) |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2030 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2031 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2032 | MockRead(SYNCHRONOUS, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 2033 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 2034 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2035 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2036 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2037 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2038 | EXPECT_THAT(out.rv, IsError(ERR_EMPTY_RESPONSE)); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 2039 | } |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 2040 | |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2041 | // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression |
| 2042 | // tests. There was a bug causing HttpNetworkTransaction to hang in the |
| 2043 | // destructor in such situations. |
| 2044 | // See http://crbug.com/154712 and http://crbug.com/156609. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2045 | TEST_F(HttpNetworkTransactionTest, KeepAliveEarlyClose) { |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2046 | HttpRequestInfo request; |
| 2047 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2048 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2049 | request.traffic_annotation = |
| 2050 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2051 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2052 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2053 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2054 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2055 | |
| 2056 | MockRead data_reads[] = { |
| 2057 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2058 | MockRead("Connection: keep-alive\r\n"), |
| 2059 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2060 | MockRead("hello"), |
| 2061 | MockRead(SYNCHRONOUS, 0), |
| 2062 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2063 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2064 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2065 | |
| 2066 | TestCompletionCallback callback; |
| 2067 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2068 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2069 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2070 | |
| 2071 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2072 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2073 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 2074 | scoped_refptr<IOBufferWithSize> io_buf = |
| 2075 | base::MakeRefCounted<IOBufferWithSize>(100); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2076 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2077 | if (rv == ERR_IO_PENDING) |
| 2078 | rv = callback.WaitForResult(); |
| 2079 | EXPECT_EQ(5, rv); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2080 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2081 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2082 | |
| 2083 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 2084 | base::RunLoop().RunUntilIdle(); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2085 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2086 | } |
| 2087 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2088 | TEST_F(HttpNetworkTransactionTest, KeepAliveEarlyClose2) { |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2089 | HttpRequestInfo request; |
| 2090 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2091 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2092 | request.traffic_annotation = |
| 2093 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2094 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2095 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2096 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2097 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2098 | |
| 2099 | MockRead data_reads[] = { |
| 2100 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2101 | MockRead("Connection: keep-alive\r\n"), |
| 2102 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2103 | MockRead(SYNCHRONOUS, 0), |
| 2104 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2105 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2106 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2107 | |
| 2108 | TestCompletionCallback callback; |
| 2109 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2110 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2111 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2112 | |
| 2113 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2114 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2115 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 2116 | scoped_refptr<IOBufferWithSize> io_buf( |
| 2117 | base::MakeRefCounted<IOBufferWithSize>(100)); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2118 | rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2119 | if (rv == ERR_IO_PENDING) |
| 2120 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2121 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2122 | |
| 2123 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 2124 | base::RunLoop().RunUntilIdle(); |
[email protected] | 7a5378b | 2012-11-04 03:25:17 | [diff] [blame] | 2125 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2126 | } |
| 2127 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2128 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 2129 | // reading the body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2130 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2131 | HttpRequestInfo request; |
| 2132 | request.method = "GET"; |
| 2133 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2134 | request.traffic_annotation = |
| 2135 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2136 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2137 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2138 | session_deps_.net_log = &net_log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2139 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2140 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2141 | const char* request_data = |
| 2142 | "GET / HTTP/1.1\r\n" |
| 2143 | "Host: www.foo.com\r\n" |
| 2144 | "Connection: keep-alive\r\n\r\n"; |
| 2145 | MockWrite data_writes[] = { |
| 2146 | MockWrite(ASYNC, 0, request_data), MockWrite(ASYNC, 2, request_data), |
| 2147 | MockWrite(ASYNC, 4, request_data), MockWrite(ASYNC, 6, request_data), |
| 2148 | MockWrite(ASYNC, 8, request_data), MockWrite(ASYNC, 10, request_data), |
| 2149 | MockWrite(ASYNC, 12, request_data), MockWrite(ASYNC, 14, request_data), |
| 2150 | MockWrite(ASYNC, 17, request_data), MockWrite(ASYNC, 20, request_data), |
| 2151 | }; |
| 2152 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2153 | // Note that because all these reads happen in the same |
| 2154 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 2155 | // all transactions. |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2156 | MockRead data_reads[] = { |
| 2157 | MockRead(ASYNC, 1, "HTTP/1.1 204 No Content\r\n\r\n"), |
| 2158 | MockRead(ASYNC, 3, "HTTP/1.1 205 Reset Content\r\n\r\n"), |
| 2159 | MockRead(ASYNC, 5, "HTTP/1.1 304 Not Modified\r\n\r\n"), |
| 2160 | MockRead(ASYNC, 7, |
| 2161 | "HTTP/1.1 302 Found\r\n" |
| 2162 | "Content-Length: 0\r\n\r\n"), |
| 2163 | MockRead(ASYNC, 9, |
| 2164 | "HTTP/1.1 302 Found\r\n" |
| 2165 | "Content-Length: 5\r\n\r\n" |
| 2166 | "hello"), |
| 2167 | MockRead(ASYNC, 11, |
| 2168 | "HTTP/1.1 301 Moved Permanently\r\n" |
| 2169 | "Content-Length: 0\r\n\r\n"), |
| 2170 | MockRead(ASYNC, 13, |
| 2171 | "HTTP/1.1 301 Moved Permanently\r\n" |
| 2172 | "Content-Length: 5\r\n\r\n" |
| 2173 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2174 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2175 | // In the next two rounds, IsConnectedAndIdle returns false, due to |
| 2176 | // the set_busy_before_sync_reads(true) call, while the |
| 2177 | // HttpNetworkTransaction is being shut down, but the socket is still |
| 2178 | // reuseable. See http://crbug.com/544255. |
| 2179 | MockRead(ASYNC, 15, |
| 2180 | "HTTP/1.1 200 Hunky-Dory\r\n" |
| 2181 | "Content-Length: 5\r\n\r\n"), |
| 2182 | MockRead(SYNCHRONOUS, 16, "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2183 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2184 | MockRead(ASYNC, 18, |
| 2185 | "HTTP/1.1 200 Hunky-Dory\r\n" |
| 2186 | "Content-Length: 5\r\n\r\n" |
| 2187 | "he"), |
| 2188 | MockRead(SYNCHRONOUS, 19, "llo"), |
| 2189 | |
| 2190 | // The body of the final request is actually read. |
| 2191 | MockRead(ASYNC, 21, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 2192 | MockRead(ASYNC, 22, "hello"), |
| 2193 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2194 | SequencedSocketData data(data_reads, data_writes); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2195 | data.set_busy_before_sync_reads(true); |
| 2196 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 2197 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 2198 | const int kNumUnreadBodies = base::size(data_writes) - 1; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2199 | std::string response_lines[kNumUnreadBodies]; |
| 2200 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 2201 | uint32_t first_socket_log_id = NetLogSource::kInvalidId; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2202 | for (size_t i = 0; i < kNumUnreadBodies; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2203 | TestCompletionCallback callback; |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2204 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2205 | auto trans = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2206 | session.get()); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2207 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2208 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2209 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2210 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2211 | LoadTimingInfo load_timing_info; |
| 2212 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 2213 | if (i == 0) { |
| 2214 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2215 | first_socket_log_id = load_timing_info.socket_log_id; |
| 2216 | } else { |
| 2217 | TestLoadTimingReused(load_timing_info); |
| 2218 | EXPECT_EQ(first_socket_log_id, load_timing_info.socket_log_id); |
| 2219 | } |
| 2220 | |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2221 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2222 | ASSERT_TRUE(response); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2223 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2224 | ASSERT_TRUE(response->headers); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2225 | response_lines[i] = response->headers->GetStatusLine(); |
| 2226 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2227 | // Delete the transaction without reading the response bodies. Then spin |
| 2228 | // the message loop, so the response bodies are drained. |
| 2229 | trans.reset(); |
| 2230 | base::RunLoop().RunUntilIdle(); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2231 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2232 | |
| 2233 | const char* const kStatusLines[] = { |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2234 | "HTTP/1.1 204 No Content", |
| 2235 | "HTTP/1.1 205 Reset Content", |
| 2236 | "HTTP/1.1 304 Not Modified", |
| 2237 | "HTTP/1.1 302 Found", |
| 2238 | "HTTP/1.1 302 Found", |
| 2239 | "HTTP/1.1 301 Moved Permanently", |
| 2240 | "HTTP/1.1 301 Moved Permanently", |
| 2241 | "HTTP/1.1 200 Hunky-Dory", |
| 2242 | "HTTP/1.1 200 Hunky-Dory", |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2243 | }; |
| 2244 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 2245 | static_assert(kNumUnreadBodies == base::size(kStatusLines), |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 2246 | "forgot to update kStatusLines"); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2247 | |
| 2248 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 2249 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 2250 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2251 | TestCompletionCallback callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2252 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2253 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2254 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2255 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2256 | ASSERT_TRUE(response); |
| 2257 | ASSERT_TRUE(response->headers); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2258 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2259 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2260 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2261 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2262 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 2263 | } |
| 2264 | |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2265 | // Sockets that receive extra data after a response is complete should not be |
| 2266 | // reused. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2267 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData1) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2268 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2269 | MockWrite data_writes1[] = { |
| 2270 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 2271 | "Host: www.borked.com\r\n" |
| 2272 | "Connection: keep-alive\r\n\r\n"), |
| 2273 | }; |
| 2274 | |
| 2275 | MockRead data_reads1[] = { |
| 2276 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2277 | "Connection: keep-alive\r\n" |
| 2278 | "Content-Length: 22\r\n\r\n" |
| 2279 | "This server is borked."), |
| 2280 | }; |
| 2281 | |
| 2282 | MockWrite data_writes2[] = { |
| 2283 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2284 | "Host: www.borked.com\r\n" |
| 2285 | "Connection: keep-alive\r\n\r\n"), |
| 2286 | }; |
| 2287 | |
| 2288 | MockRead data_reads2[] = { |
| 2289 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2290 | "Content-Length: 3\r\n\r\n" |
| 2291 | "foo"), |
| 2292 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2293 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2294 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2295 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2296 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2297 | |
| 2298 | TestCompletionCallback callback; |
| 2299 | HttpRequestInfo request1; |
| 2300 | request1.method = "HEAD"; |
| 2301 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2302 | request1.traffic_annotation = |
| 2303 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2304 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2305 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2306 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2307 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2308 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2309 | |
| 2310 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2311 | ASSERT_TRUE(response1); |
| 2312 | ASSERT_TRUE(response1->headers); |
| 2313 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2314 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2315 | |
| 2316 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2317 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2318 | EXPECT_EQ("", response_data1); |
| 2319 | // Deleting the transaction attempts to release the socket back into the |
| 2320 | // socket pool. |
| 2321 | trans1.reset(); |
| 2322 | |
| 2323 | HttpRequestInfo request2; |
| 2324 | request2.method = "GET"; |
| 2325 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2326 | request2.traffic_annotation = |
| 2327 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2328 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2329 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2330 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2331 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2332 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2333 | |
| 2334 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2335 | ASSERT_TRUE(response2); |
| 2336 | ASSERT_TRUE(response2->headers); |
| 2337 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2338 | |
| 2339 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2340 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2341 | EXPECT_EQ("foo", response_data2); |
| 2342 | } |
| 2343 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2344 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData2) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2345 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2346 | MockWrite data_writes1[] = { |
| 2347 | MockWrite("GET / HTTP/1.1\r\n" |
| 2348 | "Host: www.borked.com\r\n" |
| 2349 | "Connection: keep-alive\r\n\r\n"), |
| 2350 | }; |
| 2351 | |
| 2352 | MockRead data_reads1[] = { |
| 2353 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2354 | "Connection: keep-alive\r\n" |
| 2355 | "Content-Length: 22\r\n\r\n" |
| 2356 | "This server is borked." |
| 2357 | "Bonus data!"), |
| 2358 | }; |
| 2359 | |
| 2360 | MockWrite data_writes2[] = { |
| 2361 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2362 | "Host: www.borked.com\r\n" |
| 2363 | "Connection: keep-alive\r\n\r\n"), |
| 2364 | }; |
| 2365 | |
| 2366 | MockRead data_reads2[] = { |
| 2367 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2368 | "Content-Length: 3\r\n\r\n" |
| 2369 | "foo"), |
| 2370 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2371 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2372 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2373 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2374 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2375 | |
| 2376 | TestCompletionCallback callback; |
| 2377 | HttpRequestInfo request1; |
| 2378 | request1.method = "GET"; |
| 2379 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2380 | request1.traffic_annotation = |
| 2381 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2382 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2383 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2384 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2385 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2386 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2387 | |
| 2388 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2389 | ASSERT_TRUE(response1); |
| 2390 | ASSERT_TRUE(response1->headers); |
| 2391 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2392 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2393 | |
| 2394 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2395 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2396 | EXPECT_EQ("This server is borked.", response_data1); |
| 2397 | // Deleting the transaction attempts to release the socket back into the |
| 2398 | // socket pool. |
| 2399 | trans1.reset(); |
| 2400 | |
| 2401 | HttpRequestInfo request2; |
| 2402 | request2.method = "GET"; |
| 2403 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2404 | request2.traffic_annotation = |
| 2405 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2406 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2407 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2408 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2409 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2410 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2411 | |
| 2412 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2413 | ASSERT_TRUE(response2); |
| 2414 | ASSERT_TRUE(response2->headers); |
| 2415 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2416 | |
| 2417 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2418 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2419 | EXPECT_EQ("foo", response_data2); |
| 2420 | } |
| 2421 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2422 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData3) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2423 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2424 | MockWrite data_writes1[] = { |
| 2425 | MockWrite("GET / HTTP/1.1\r\n" |
| 2426 | "Host: www.borked.com\r\n" |
| 2427 | "Connection: keep-alive\r\n\r\n"), |
| 2428 | }; |
| 2429 | |
| 2430 | MockRead data_reads1[] = { |
| 2431 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2432 | "Connection: keep-alive\r\n" |
| 2433 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 2434 | MockRead("16\r\nThis server is borked.\r\n"), |
| 2435 | MockRead("0\r\n\r\nBonus data!"), |
| 2436 | }; |
| 2437 | |
| 2438 | MockWrite data_writes2[] = { |
| 2439 | MockWrite("GET /foo HTTP/1.1\r\n" |
| 2440 | "Host: www.borked.com\r\n" |
| 2441 | "Connection: keep-alive\r\n\r\n"), |
| 2442 | }; |
| 2443 | |
| 2444 | MockRead data_reads2[] = { |
| 2445 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2446 | "Content-Length: 3\r\n\r\n" |
| 2447 | "foo"), |
| 2448 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2449 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2450 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2451 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2452 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2453 | |
| 2454 | TestCompletionCallback callback; |
| 2455 | HttpRequestInfo request1; |
| 2456 | request1.method = "GET"; |
| 2457 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2458 | request1.traffic_annotation = |
| 2459 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2460 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2461 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2462 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2463 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2464 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2465 | |
| 2466 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
| 2467 | ASSERT_TRUE(response1); |
| 2468 | ASSERT_TRUE(response1->headers); |
| 2469 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2470 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2471 | |
| 2472 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2473 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2474 | EXPECT_EQ("This server is borked.", response_data1); |
| 2475 | // Deleting the transaction attempts to release the socket back into the |
| 2476 | // socket pool. |
| 2477 | trans1.reset(); |
| 2478 | |
| 2479 | HttpRequestInfo request2; |
| 2480 | request2.method = "GET"; |
| 2481 | request2.url = GURL("http://www.borked.com/foo"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2482 | request2.traffic_annotation = |
| 2483 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2484 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2485 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2486 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2487 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2488 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2489 | |
| 2490 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
| 2491 | ASSERT_TRUE(response2); |
| 2492 | ASSERT_TRUE(response2->headers); |
| 2493 | EXPECT_EQ(200, response2->headers->response_code()); |
| 2494 | |
| 2495 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2496 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2497 | EXPECT_EQ("foo", response_data2); |
| 2498 | } |
| 2499 | |
| 2500 | // This is a little different from the others - it tests the case that the |
| 2501 | // HttpStreamParser doesn't know if there's extra data on a socket or not when |
| 2502 | // the HttpNetworkTransaction is torn down, because the response body hasn't |
| 2503 | // been read from yet, but the request goes through the HttpResponseBodyDrainer. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2504 | TEST_F(HttpNetworkTransactionTest, KeepAliveWithUnusedData4) { |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2505 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2506 | MockWrite data_writes1[] = { |
| 2507 | MockWrite("GET / HTTP/1.1\r\n" |
| 2508 | "Host: www.borked.com\r\n" |
| 2509 | "Connection: keep-alive\r\n\r\n"), |
| 2510 | }; |
| 2511 | |
| 2512 | MockRead data_reads1[] = { |
| 2513 | MockRead("HTTP/1.1 200 OK\r\n" |
| 2514 | "Connection: keep-alive\r\n" |
| 2515 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 2516 | MockRead("16\r\nThis server is borked.\r\n"), |
| 2517 | MockRead("0\r\n\r\nBonus data!"), |
| 2518 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2519 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2520 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2521 | |
| 2522 | TestCompletionCallback callback; |
| 2523 | HttpRequestInfo request1; |
| 2524 | request1.method = "GET"; |
| 2525 | request1.url = GURL("http://www.borked.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2526 | request1.traffic_annotation = |
| 2527 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2528 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2529 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 2530 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2531 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2532 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2533 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2534 | const HttpResponseInfo* response1 = trans->GetResponseInfo(); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2535 | ASSERT_TRUE(response1); |
| 2536 | ASSERT_TRUE(response1->headers); |
| 2537 | EXPECT_EQ(200, response1->headers->response_code()); |
| 2538 | EXPECT_TRUE(response1->headers->IsKeepAlive()); |
| 2539 | |
| 2540 | // Deleting the transaction creates an HttpResponseBodyDrainer to read the |
| 2541 | // response body. |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 2542 | trans.reset(); |
mmenke | 5f94fda | 2016-06-02 20:54:13 | [diff] [blame] | 2543 | |
| 2544 | // Let the HttpResponseBodyDrainer drain the socket. It should determine the |
| 2545 | // socket can't be reused, rather than returning it to the socket pool. |
| 2546 | base::RunLoop().RunUntilIdle(); |
| 2547 | |
| 2548 | // There should be no idle sockets in the pool. |
| 2549 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 2550 | } |
| 2551 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2552 | // Test the request-challenge-retry sequence for basic auth. |
| 2553 | // (basic auth is the easiest to mock, because it has no randomness). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2554 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2555 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2556 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2557 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2558 | request.traffic_annotation = |
| 2559 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2560 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2561 | RecordingTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2562 | session_deps_.net_log = &log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2563 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2564 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2565 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2566 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2567 | MockWrite( |
| 2568 | "GET / HTTP/1.1\r\n" |
| 2569 | "Host: www.example.org\r\n" |
| 2570 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2571 | }; |
| 2572 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2573 | MockRead data_reads1[] = { |
| 2574 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2575 | // Give a couple authenticate options (only the middle one is actually |
| 2576 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2577 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2578 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2579 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2580 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2581 | // Large content-length -- won't matter, as connection will be reset. |
| 2582 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2583 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2584 | }; |
| 2585 | |
| 2586 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2587 | // be issuing -- the final header line contains the credentials. |
| 2588 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2589 | MockWrite( |
| 2590 | "GET / HTTP/1.1\r\n" |
| 2591 | "Host: www.example.org\r\n" |
| 2592 | "Connection: keep-alive\r\n" |
| 2593 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2594 | }; |
| 2595 | |
| 2596 | // Lastly, the server responds with the actual content. |
| 2597 | MockRead data_reads2[] = { |
| 2598 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2599 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2600 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2601 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2602 | }; |
| 2603 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2604 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 2605 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2606 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2607 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2608 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2609 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2610 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2611 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2612 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2613 | |
| 2614 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2615 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2616 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2617 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2618 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2619 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2620 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2621 | int64_t writes_size1 = CountWriteBytes(data_writes1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2622 | EXPECT_EQ(writes_size1, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2623 | int64_t reads_size1 = CountReadBytes(data_reads1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2624 | EXPECT_EQ(reads_size1, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2625 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2626 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2627 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2628 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2629 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2630 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2631 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2632 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2633 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2634 | |
| 2635 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2636 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2637 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2638 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2639 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2640 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2641 | // The load timing after restart should have a new socket ID, and times after |
| 2642 | // those of the first load timing. |
| 2643 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2644 | load_timing_info2.connect_timing.connect_start); |
| 2645 | EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 2646 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2647 | int64_t writes_size2 = CountWriteBytes(data_writes2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2648 | EXPECT_EQ(writes_size1 + writes_size2, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2649 | int64_t reads_size2 = CountReadBytes(data_reads2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2650 | EXPECT_EQ(reads_size1 + reads_size2, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2651 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2652 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2653 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2654 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2655 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2656 | } |
| 2657 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2658 | // Test the request-challenge-retry sequence for basic auth. |
| 2659 | // (basic auth is the easiest to mock, because it has no randomness). |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2660 | TEST_F(HttpNetworkTransactionTest, BasicAuthWithAddressChange) { |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2661 | HttpRequestInfo request; |
| 2662 | request.method = "GET"; |
| 2663 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2664 | request.traffic_annotation = |
| 2665 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2666 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2667 | RecordingTestNetLog log; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2668 | MockHostResolver* resolver = new MockHostResolver(); |
| 2669 | session_deps_.net_log = &log; |
| 2670 | session_deps_.host_resolver.reset(resolver); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2671 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2672 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2673 | |
| 2674 | resolver->rules()->ClearRules(); |
| 2675 | resolver->rules()->AddRule("www.example.org", "127.0.0.1"); |
| 2676 | |
| 2677 | MockWrite data_writes1[] = { |
| 2678 | MockWrite("GET / HTTP/1.1\r\n" |
| 2679 | "Host: www.example.org\r\n" |
| 2680 | "Connection: keep-alive\r\n\r\n"), |
| 2681 | }; |
| 2682 | |
| 2683 | MockRead data_reads1[] = { |
| 2684 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2685 | // Give a couple authenticate options (only the middle one is actually |
| 2686 | // supported). |
| 2687 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 2688 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2689 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2690 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2691 | // Large content-length -- won't matter, as connection will be reset. |
| 2692 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 2693 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 2694 | }; |
| 2695 | |
| 2696 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2697 | // be issuing -- the final header line contains the credentials. |
| 2698 | MockWrite data_writes2[] = { |
| 2699 | MockWrite("GET / HTTP/1.1\r\n" |
| 2700 | "Host: www.example.org\r\n" |
| 2701 | "Connection: keep-alive\r\n" |
| 2702 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2703 | }; |
| 2704 | |
| 2705 | // Lastly, the server responds with the actual content. |
| 2706 | MockRead data_reads2[] = { |
| 2707 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2708 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2709 | MockRead("Content-Length: 100\r\n\r\n"), MockRead(SYNCHRONOUS, OK), |
| 2710 | }; |
| 2711 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2712 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 2713 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2714 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 2715 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 2716 | |
| 2717 | TestCompletionCallback callback1; |
| 2718 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2719 | EXPECT_EQ(OK, callback1.GetResult(trans.Start(&request, callback1.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2720 | NetLogWithSource()))); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2721 | |
| 2722 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2723 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2724 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2725 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2726 | int64_t writes_size1 = CountWriteBytes(data_writes1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2727 | EXPECT_EQ(writes_size1, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2728 | int64_t reads_size1 = CountReadBytes(data_reads1); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2729 | EXPECT_EQ(reads_size1, trans.GetTotalReceivedBytes()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2730 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2731 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2732 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2733 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2734 | |
| 2735 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2736 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2737 | ASSERT_FALSE(endpoint.address().empty()); |
| 2738 | EXPECT_EQ("127.0.0.1:80", endpoint.ToString()); |
| 2739 | |
| 2740 | resolver->rules()->ClearRules(); |
| 2741 | resolver->rules()->AddRule("www.example.org", "127.0.0.2"); |
| 2742 | |
| 2743 | TestCompletionCallback callback2; |
| 2744 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2745 | EXPECT_EQ(OK, callback2.GetResult(trans.RestartWithAuth( |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2746 | AuthCredentials(kFoo, kBar), callback2.callback()))); |
| 2747 | |
| 2748 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2749 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2750 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2751 | // The load timing after restart should have a new socket ID, and times after |
| 2752 | // those of the first load timing. |
| 2753 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2754 | load_timing_info2.connect_timing.connect_start); |
| 2755 | EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 2756 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2757 | int64_t writes_size2 = CountWriteBytes(data_writes2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2758 | EXPECT_EQ(writes_size1 + writes_size2, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2759 | int64_t reads_size2 = CountReadBytes(data_reads2); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2760 | EXPECT_EQ(reads_size1 + reads_size2, trans.GetTotalReceivedBytes()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2761 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2762 | response = trans.GetResponseInfo(); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2763 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2764 | EXPECT_FALSE(response->auth_challenge.has_value()); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2765 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2766 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2767 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 2768 | ASSERT_FALSE(endpoint.address().empty()); |
| 2769 | EXPECT_EQ("127.0.0.2:80", endpoint.ToString()); |
| 2770 | } |
| 2771 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2772 | // Test that, if the server requests auth indefinitely, HttpNetworkTransaction |
| 2773 | // will eventually give up. |
| 2774 | TEST_F(HttpNetworkTransactionTest, BasicAuthForever) { |
| 2775 | HttpRequestInfo request; |
| 2776 | request.method = "GET"; |
| 2777 | request.url = GURL("http://www.example.org/"); |
| 2778 | request.traffic_annotation = |
| 2779 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2780 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2781 | RecordingTestNetLog log; |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2782 | session_deps_.net_log = &log; |
| 2783 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 2784 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 2785 | |
| 2786 | MockWrite data_writes[] = { |
| 2787 | MockWrite("GET / HTTP/1.1\r\n" |
| 2788 | "Host: www.example.org\r\n" |
| 2789 | "Connection: keep-alive\r\n\r\n"), |
| 2790 | }; |
| 2791 | |
| 2792 | MockRead data_reads[] = { |
| 2793 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2794 | // Give a couple authenticate options (only the middle one is actually |
| 2795 | // supported). |
| 2796 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 2797 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2798 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2799 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2800 | // Large content-length -- won't matter, as connection will be reset. |
| 2801 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 2802 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 2803 | }; |
| 2804 | |
| 2805 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2806 | // be issuing -- the final header line contains the credentials. |
| 2807 | MockWrite data_writes_restart[] = { |
| 2808 | MockWrite("GET / HTTP/1.1\r\n" |
| 2809 | "Host: www.example.org\r\n" |
| 2810 | "Connection: keep-alive\r\n" |
| 2811 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2812 | }; |
| 2813 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2814 | StaticSocketDataProvider data(data_reads, data_writes); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2815 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 2816 | |
| 2817 | TestCompletionCallback callback; |
| 2818 | int rv = callback.GetResult( |
| 2819 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
| 2820 | |
| 2821 | std::vector<std::unique_ptr<StaticSocketDataProvider>> data_restarts; |
| 2822 | for (int i = 0; i < 32; i++) { |
| 2823 | // Check the previous response was a 401. |
| 2824 | EXPECT_THAT(rv, IsOk()); |
| 2825 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 2826 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2827 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2828 | |
| 2829 | data_restarts.push_back(std::make_unique<StaticSocketDataProvider>( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2830 | data_reads, data_writes_restart)); |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 2831 | session_deps_.socket_factory->AddSocketDataProvider( |
| 2832 | data_restarts.back().get()); |
| 2833 | rv = callback.GetResult(trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 2834 | callback.callback())); |
| 2835 | } |
| 2836 | |
| 2837 | // After too many tries, the transaction should have given up. |
| 2838 | EXPECT_THAT(rv, IsError(ERR_TOO_MANY_RETRIES)); |
| 2839 | } |
| 2840 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2841 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2842 | HttpRequestInfo request; |
| 2843 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2844 | request.url = GURL("http://www.example.org/"); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 2845 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2846 | request.traffic_annotation = |
| 2847 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2848 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2849 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2850 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2851 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2852 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2853 | MockWrite( |
| 2854 | "GET / HTTP/1.1\r\n" |
| 2855 | "Host: www.example.org\r\n" |
| 2856 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2857 | }; |
| 2858 | |
| 2859 | MockRead data_reads[] = { |
| 2860 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2861 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2862 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2863 | // Large content-length -- won't matter, as connection will be reset. |
| 2864 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 2865 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2866 | }; |
| 2867 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2868 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 2869 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2870 | TestCompletionCallback callback; |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2871 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2872 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2873 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2874 | |
| 2875 | rv = callback.WaitForResult(); |
| 2876 | EXPECT_EQ(0, rv); |
| 2877 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2878 | int64_t writes_size = CountWriteBytes(data_writes); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2879 | EXPECT_EQ(writes_size, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2880 | int64_t reads_size = CountReadBytes(data_reads); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2881 | EXPECT_EQ(reads_size, trans.GetTotalReceivedBytes()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 2882 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2883 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 2884 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2885 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2886 | } |
| 2887 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2888 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 2889 | // connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2890 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2891 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 2892 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 2893 | // reused. See http://crbug.com/544255. |
| 2894 | for (int i = 0; i < 2; ++i) { |
| 2895 | HttpRequestInfo request; |
| 2896 | request.method = "GET"; |
| 2897 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2898 | request.traffic_annotation = |
| 2899 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2900 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2901 | RecordingTestNetLog log; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2902 | session_deps_.net_log = &log; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2903 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2904 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2905 | MockWrite data_writes[] = { |
| 2906 | MockWrite(ASYNC, 0, |
| 2907 | "GET / HTTP/1.1\r\n" |
| 2908 | "Host: www.example.org\r\n" |
| 2909 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2910 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2911 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2912 | // be issuing -- the final header line contains the credentials. |
| 2913 | MockWrite(ASYNC, 6, |
| 2914 | "GET / HTTP/1.1\r\n" |
| 2915 | "Host: www.example.org\r\n" |
| 2916 | "Connection: keep-alive\r\n" |
| 2917 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2918 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2919 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2920 | MockRead data_reads[] = { |
| 2921 | MockRead(ASYNC, 1, "HTTP/1.1 401 Unauthorized\r\n"), |
| 2922 | MockRead(ASYNC, 2, "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2923 | MockRead(ASYNC, 3, "Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2924 | MockRead(ASYNC, 4, "Content-Length: 14\r\n\r\n"), |
| 2925 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 5, "Unauthorized\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2926 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2927 | // Lastly, the server responds with the actual content. |
| 2928 | MockRead(ASYNC, 7, "HTTP/1.1 200 OK\r\n"), |
| 2929 | MockRead(ASYNC, 8, "Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2930 | MockRead(ASYNC, 9, "Content-Length: 5\r\n\r\n"), |
| 2931 | MockRead(ASYNC, 10, "Hello"), |
| 2932 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2933 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2934 | SequencedSocketData data(data_reads, data_writes); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2935 | data.set_busy_before_sync_reads(true); |
| 2936 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 2937 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2938 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2939 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2940 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 2941 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2942 | ASSERT_THAT(callback1.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2943 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2944 | LoadTimingInfo load_timing_info1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2945 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info1)); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2946 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2947 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2948 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2949 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2950 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2951 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2952 | TestCompletionCallback callback2; |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2953 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2954 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 2955 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2956 | ASSERT_THAT(callback2.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2957 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2958 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2959 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info2)); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2960 | TestLoadTimingReused(load_timing_info2); |
| 2961 | // The load timing after restart should have the same socket ID, and times |
| 2962 | // those of the first load timing. |
| 2963 | EXPECT_LE(load_timing_info1.receive_headers_end, |
| 2964 | load_timing_info2.send_start); |
| 2965 | EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2966 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2967 | response = trans.GetResponseInfo(); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2968 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 2969 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2970 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2971 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2972 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2973 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2974 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2975 | int64_t writes_size = CountWriteBytes(data_writes); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2976 | EXPECT_EQ(writes_size, trans.GetTotalSentBytes()); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 2977 | int64_t reads_size = CountReadBytes(data_reads); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2978 | EXPECT_EQ(reads_size, trans.GetTotalReceivedBytes()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 2979 | } |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 2983 | // connection and with no response body to drain. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 2984 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2985 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2986 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2987 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 2988 | request.traffic_annotation = |
| 2989 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2990 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 2991 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2992 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2993 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2994 | MockWrite("GET / HTTP/1.1\r\n" |
| 2995 | "Host: www.example.org\r\n" |
| 2996 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 2997 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 2998 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 2999 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3000 | MockWrite("GET / HTTP/1.1\r\n" |
| 3001 | "Host: www.example.org\r\n" |
| 3002 | "Connection: keep-alive\r\n" |
| 3003 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3004 | }; |
| 3005 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3006 | MockRead data_reads1[] = { |
| 3007 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3008 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3009 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3010 | |
| 3011 | // Lastly, the server responds with the actual content. |
| 3012 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3013 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3014 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3015 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3016 | }; |
| 3017 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3018 | // An incorrect reconnect would cause this to be read. |
| 3019 | MockRead data_reads2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3020 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3021 | }; |
| 3022 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3023 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3024 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3025 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3026 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3027 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3028 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3029 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3030 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3031 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3032 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3033 | |
| 3034 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3035 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3036 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3037 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3038 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3039 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3040 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3041 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3042 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3043 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3044 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3045 | |
| 3046 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3047 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3048 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3049 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3050 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3051 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3052 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3056 | // connection and with a large response body to drain. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3057 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3058 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3059 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3060 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3061 | request.traffic_annotation = |
| 3062 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3063 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3064 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3065 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3066 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3067 | MockWrite("GET / HTTP/1.1\r\n" |
| 3068 | "Host: www.example.org\r\n" |
| 3069 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3070 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3071 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3072 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3073 | MockWrite("GET / HTTP/1.1\r\n" |
| 3074 | "Host: www.example.org\r\n" |
| 3075 | "Connection: keep-alive\r\n" |
| 3076 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3077 | }; |
| 3078 | |
| 3079 | // Respond with 5 kb of response body. |
| 3080 | std::string large_body_string("Unauthorized"); |
| 3081 | large_body_string.append(5 * 1024, ' '); |
| 3082 | large_body_string.append("\r\n"); |
| 3083 | |
| 3084 | MockRead data_reads1[] = { |
| 3085 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3086 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3087 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3088 | // 5134 = 12 + 5 * 1024 + 2 |
| 3089 | MockRead("Content-Length: 5134\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3090 | MockRead(ASYNC, large_body_string.data(), large_body_string.size()), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3091 | |
| 3092 | // Lastly, the server responds with the actual content. |
| 3093 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3094 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3095 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3096 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3097 | }; |
| 3098 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3099 | // An incorrect reconnect would cause this to be read. |
| 3100 | MockRead data_reads2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3101 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 3102 | }; |
| 3103 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3104 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3105 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3106 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3107 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3108 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3109 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3110 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3111 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3112 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3113 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3114 | |
| 3115 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3116 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3117 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3118 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3119 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3120 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3121 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3122 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3123 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3124 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3125 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3126 | |
| 3127 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3128 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3129 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3130 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3131 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3132 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3133 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3137 | // connection, but the server gets impatient and closes the connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3138 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3139 | HttpRequestInfo request; |
| 3140 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3141 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3142 | request.traffic_annotation = |
| 3143 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3144 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3145 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3146 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3147 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3148 | MockWrite( |
| 3149 | "GET / HTTP/1.1\r\n" |
| 3150 | "Host: www.example.org\r\n" |
| 3151 | "Connection: keep-alive\r\n\r\n"), |
| 3152 | // This simulates the seemingly successful write to a closed connection |
| 3153 | // if the bug is not fixed. |
| 3154 | MockWrite( |
| 3155 | "GET / HTTP/1.1\r\n" |
| 3156 | "Host: www.example.org\r\n" |
| 3157 | "Connection: keep-alive\r\n" |
| 3158 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3159 | }; |
| 3160 | |
| 3161 | MockRead data_reads1[] = { |
| 3162 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 3163 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3164 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3165 | MockRead("Content-Length: 14\r\n\r\n"), |
| 3166 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3167 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3168 | MockRead("Unauthorized\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3169 | MockRead(SYNCHRONOUS, OK), // The server closes the connection. |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3170 | }; |
| 3171 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3172 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3173 | // be issuing -- the final header line contains the credentials. |
| 3174 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3175 | MockWrite( |
| 3176 | "GET / HTTP/1.1\r\n" |
| 3177 | "Host: www.example.org\r\n" |
| 3178 | "Connection: keep-alive\r\n" |
| 3179 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3180 | }; |
| 3181 | |
| 3182 | // Lastly, the server responds with the actual content. |
| 3183 | MockRead data_reads2[] = { |
| 3184 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3185 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3186 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3187 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3188 | }; |
| 3189 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3190 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 3191 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3192 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3193 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3194 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3195 | TestCompletionCallback callback1; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3196 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3197 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3198 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3199 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3200 | |
| 3201 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3202 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3203 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3204 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3205 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3206 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3207 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3208 | TestCompletionCallback callback2; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3209 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3210 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3211 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3212 | |
| 3213 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3214 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3215 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3216 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3217 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3218 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3219 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3220 | } |
| 3221 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3222 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 3223 | // that requires a restart when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3224 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAliveHttp10) { |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3225 | HttpRequestInfo request; |
| 3226 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3227 | request.url = GURL("https://www.example.org/"); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3228 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3229 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3230 | request.traffic_annotation = |
| 3231 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3232 | |
| 3233 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3234 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3235 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3236 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3237 | RecordingBoundTestNetLog log; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3238 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3239 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3240 | |
| 3241 | // Since we have proxy, should try to establish tunnel. |
| 3242 | MockWrite data_writes1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3243 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3244 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3245 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3246 | }; |
| 3247 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3248 | // The proxy responds to the connect with a 407, using a non-persistent |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3249 | // connection. |
| 3250 | MockRead data_reads1[] = { |
| 3251 | // No credentials. |
| 3252 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n"), |
| 3253 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"), |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3254 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3255 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3256 | // Since the first connection couldn't be reused, need to establish another |
| 3257 | // once given credentials. |
| 3258 | MockWrite data_writes2[] = { |
| 3259 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3260 | // be issuing -- the final header line contains the credentials. |
| 3261 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3262 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3263 | "Proxy-Connection: keep-alive\r\n" |
| 3264 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3265 | |
| 3266 | MockWrite("GET / HTTP/1.1\r\n" |
| 3267 | "Host: www.example.org\r\n" |
| 3268 | "Connection: keep-alive\r\n\r\n"), |
| 3269 | }; |
| 3270 | |
| 3271 | MockRead data_reads2[] = { |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3272 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 3273 | |
| 3274 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3275 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3276 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3277 | MockRead(SYNCHRONOUS, "hello"), |
| 3278 | }; |
| 3279 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3280 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3281 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3282 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3283 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3284 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3285 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 3286 | |
| 3287 | TestCompletionCallback callback1; |
| 3288 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3289 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3290 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3291 | |
| 3292 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3293 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3294 | |
| 3295 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3296 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3297 | auto entries = log.GetEntries(); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3298 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3299 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3300 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3301 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3302 | entries, pos, |
| 3303 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3304 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3305 | |
| 3306 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3307 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3308 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3309 | ASSERT_TRUE(response->headers); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3310 | EXPECT_EQ(407, response->headers->response_code()); |
| 3311 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3312 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3313 | |
| 3314 | LoadTimingInfo load_timing_info; |
| 3315 | // CONNECT requests and responses are handled at the connect job level, so |
| 3316 | // the transaction does not yet have a connection. |
| 3317 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3318 | |
| 3319 | TestCompletionCallback callback2; |
| 3320 | |
| 3321 | rv = |
| 3322 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3323 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3324 | |
| 3325 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3326 | EXPECT_THAT(rv, IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3327 | |
| 3328 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3329 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3330 | |
| 3331 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3332 | EXPECT_EQ(200, response->headers->response_code()); |
| 3333 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 3334 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 3335 | |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3336 | // Check that credentials were successfully cached, with the right target. |
| 3337 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 3338 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 3339 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3340 | ASSERT_TRUE(entry); |
| 3341 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 3342 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 3343 | |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3344 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3345 | EXPECT_FALSE(response->auth_challenge.has_value()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3346 | |
| 3347 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3348 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3349 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3350 | |
| 3351 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3352 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 3356 | // that requires a restart when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3357 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAliveHttp11) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3358 | HttpRequestInfo request; |
| 3359 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3360 | request.url = GURL("https://www.example.org/"); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3361 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3362 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3363 | request.traffic_annotation = |
| 3364 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3365 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3366 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3367 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3368 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3369 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3370 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3371 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3372 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3373 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3374 | // Since we have proxy, should try to establish tunnel. |
| 3375 | MockWrite data_writes1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3376 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3377 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3378 | "Proxy-Connection: keep-alive\r\n\r\n"), |
mmenke | e0b5c88 | 2015-08-26 20:29:11 | [diff] [blame] | 3379 | }; |
| 3380 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3381 | // The proxy responds to the connect with a 407, using a non-persistent |
mmenke | 0fd148d | 2015-09-30 23:00:08 | [diff] [blame] | 3382 | // connection. |
| 3383 | MockRead data_reads1[] = { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3384 | // No credentials. |
| 3385 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3386 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3387 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 3388 | }; |
mmenke | e0b5c88 | 2015-08-26 20:29:11 | [diff] [blame] | 3389 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3390 | MockWrite data_writes2[] = { |
| 3391 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3392 | // be issuing -- the final header line contains the credentials. |
| 3393 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3394 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3395 | "Proxy-Connection: keep-alive\r\n" |
| 3396 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
mmenke | 0fd148d | 2015-09-30 23:00:08 | [diff] [blame] | 3397 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3398 | MockWrite("GET / HTTP/1.1\r\n" |
| 3399 | "Host: www.example.org\r\n" |
| 3400 | "Connection: keep-alive\r\n\r\n"), |
| 3401 | }; |
| 3402 | |
| 3403 | MockRead data_reads2[] = { |
| 3404 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 3405 | |
| 3406 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3407 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3408 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3409 | MockRead(SYNCHRONOUS, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3410 | }; |
| 3411 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3412 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3413 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3414 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3415 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 3416 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3417 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3418 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3419 | TestCompletionCallback callback1; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3420 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3421 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3422 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3423 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3424 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3425 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3426 | |
| 3427 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3428 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3429 | auto entries = log.GetEntries(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3430 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3431 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3432 | NetLogEventPhase::NONE); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3433 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 3434 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3435 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3436 | NetLogEventPhase::NONE); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3437 | |
| 3438 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3439 | ASSERT_TRUE(response); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3440 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3441 | ASSERT_TRUE(response->headers); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3442 | EXPECT_EQ(407, response->headers->response_code()); |
| 3443 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3444 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3445 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 3446 | LoadTimingInfo load_timing_info; |
| 3447 | // CONNECT requests and responses are handled at the connect job level, so |
| 3448 | // the transaction does not yet have a connection. |
| 3449 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3450 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3451 | TestCompletionCallback callback2; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3452 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3453 | rv = trans->RestartWithAuth( |
| 3454 | AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3455 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3456 | |
| 3457 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3458 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3459 | |
| 3460 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 3461 | ASSERT_TRUE(response); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3462 | |
| 3463 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3464 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3465 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3466 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 3467 | |
| 3468 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3469 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3470 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 3471 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3472 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3473 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3474 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3475 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3476 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 3477 | } |
| 3478 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 3479 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3480 | // proxy connection with HTTP/1.0 responses, when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3481 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHttp10) { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3482 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 3483 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 3484 | // reused. See http://crbug.com/544255. |
| 3485 | for (int i = 0; i < 2; ++i) { |
| 3486 | HttpRequestInfo request; |
| 3487 | request.method = "GET"; |
| 3488 | request.url = GURL("https://www.example.org/"); |
| 3489 | // Ensure that proxy authentication is attempted even |
| 3490 | // when the no authentication data flag is set. |
| 3491 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3492 | request.traffic_annotation = |
| 3493 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3494 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3495 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3496 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3497 | ConfiguredProxyResolutionService::CreateFixed( |
| 3498 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3499 | RecordingBoundTestNetLog log; |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3500 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3501 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3502 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3503 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3504 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3505 | // Since we have proxy, should try to establish tunnel. |
| 3506 | MockWrite data_writes1[] = { |
| 3507 | MockWrite(ASYNC, 0, |
| 3508 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3509 | "Host: www.example.org:443\r\n" |
| 3510 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3511 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3512 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3513 | // be issuing -- the final header line contains the credentials. |
| 3514 | MockWrite(ASYNC, 3, |
| 3515 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3516 | "Host: www.example.org:443\r\n" |
| 3517 | "Proxy-Connection: keep-alive\r\n" |
| 3518 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3519 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3520 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3521 | // The proxy responds to the connect with a 407, using a persistent |
| 3522 | // connection. (Since it's HTTP/1.0, keep-alive has to be explicit.) |
| 3523 | MockRead data_reads1[] = { |
| 3524 | // No credentials. |
| 3525 | MockRead(ASYNC, 1, |
| 3526 | "HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 3527 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3528 | "Proxy-Connection: keep-alive\r\n" |
| 3529 | "Content-Length: 10\r\n\r\n"), |
| 3530 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 2, "0123456789"), |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3531 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3532 | // Wrong credentials (wrong password). |
| 3533 | MockRead(ASYNC, 4, |
| 3534 | "HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 3535 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3536 | "Proxy-Connection: keep-alive\r\n" |
| 3537 | "Content-Length: 10\r\n\r\n"), |
| 3538 | // No response body because the test stops reading here. |
| 3539 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 5), |
| 3540 | }; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3541 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3542 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3543 | data1.set_busy_before_sync_reads(true); |
| 3544 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3545 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3546 | TestCompletionCallback callback1; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3547 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3548 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3549 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3550 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3551 | auto entries = log.GetEntries(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3552 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3553 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3554 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3555 | ExpectLogContainsSomewhere( |
| 3556 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3557 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3558 | NetLogEventPhase::NONE); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3559 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3560 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3561 | ASSERT_TRUE(response); |
| 3562 | ASSERT_TRUE(response->headers); |
| 3563 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3564 | EXPECT_EQ(407, response->headers->response_code()); |
| 3565 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3566 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3567 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3568 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3569 | TestCompletionCallback callback2; |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3570 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3571 | // Wrong password (should be "bar"). |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3572 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBaz), |
| 3573 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3574 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3575 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3576 | response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3577 | ASSERT_TRUE(response); |
| 3578 | ASSERT_TRUE(response->headers); |
| 3579 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3580 | EXPECT_EQ(407, response->headers->response_code()); |
| 3581 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3582 | EXPECT_TRUE(HttpVersion(1, 0) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3583 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3584 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3585 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 3586 | // out of scope. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3587 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3588 | } |
ttuttle | 34f63b5 | 2015-03-05 04:33:01 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3592 | // proxy connection with HTTP/1.1 responses, when setting up an SSL tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3593 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHttp11) { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3594 | // On the second pass, the body read of the auth challenge is synchronous, so |
| 3595 | // IsConnectedAndIdle returns false. The socket should still be drained and |
| 3596 | // reused. See http://crbug.com/544255. |
| 3597 | for (int i = 0; i < 2; ++i) { |
| 3598 | HttpRequestInfo request; |
| 3599 | request.method = "GET"; |
| 3600 | request.url = GURL("https://www.example.org/"); |
| 3601 | // Ensure that proxy authentication is attempted even |
| 3602 | // when the no authentication data flag is set. |
| 3603 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3604 | request.traffic_annotation = |
| 3605 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3606 | |
| 3607 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3608 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3609 | ConfiguredProxyResolutionService::CreateFixed( |
| 3610 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3611 | RecordingBoundTestNetLog log; |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3612 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3613 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3614 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3615 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3616 | |
| 3617 | // Since we have proxy, should try to establish tunnel. |
| 3618 | MockWrite data_writes1[] = { |
| 3619 | MockWrite(ASYNC, 0, |
| 3620 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3621 | "Host: www.example.org:443\r\n" |
| 3622 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3623 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3624 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3625 | // be issuing -- the final header line contains the credentials. |
| 3626 | MockWrite(ASYNC, 3, |
| 3627 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3628 | "Host: www.example.org:443\r\n" |
| 3629 | "Proxy-Connection: keep-alive\r\n" |
| 3630 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3631 | }; |
| 3632 | |
| 3633 | // The proxy responds to the connect with a 407, using a persistent |
| 3634 | // connection. (Since it's HTTP/1.0, keep-alive has to be explicit.) |
| 3635 | MockRead data_reads1[] = { |
| 3636 | // No credentials. |
| 3637 | MockRead(ASYNC, 1, |
| 3638 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3639 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3640 | "Content-Length: 10\r\n\r\n"), |
| 3641 | MockRead(i == 0 ? ASYNC : SYNCHRONOUS, 2, "0123456789"), |
| 3642 | |
| 3643 | // Wrong credentials (wrong password). |
| 3644 | MockRead(ASYNC, 4, |
| 3645 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3646 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3647 | "Content-Length: 10\r\n\r\n"), |
| 3648 | // No response body because the test stops reading here. |
| 3649 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 5), |
| 3650 | }; |
| 3651 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3652 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3653 | data1.set_busy_before_sync_reads(true); |
| 3654 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 3655 | |
| 3656 | TestCompletionCallback callback1; |
| 3657 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3658 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3659 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3660 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3661 | auto entries = log.GetEntries(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3662 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3663 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3664 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3665 | ExpectLogContainsSomewhere( |
| 3666 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3667 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3668 | NetLogEventPhase::NONE); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3669 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3670 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3671 | ASSERT_TRUE(response); |
| 3672 | ASSERT_TRUE(response->headers); |
| 3673 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3674 | EXPECT_EQ(407, response->headers->response_code()); |
| 3675 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3676 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3677 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 3678 | EXPECT_FALSE(response->did_use_http_auth); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3679 | |
| 3680 | TestCompletionCallback callback2; |
| 3681 | |
| 3682 | // Wrong password (should be "bar"). |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3683 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBaz), |
| 3684 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3685 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3686 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3687 | response = trans.GetResponseInfo(); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3688 | ASSERT_TRUE(response); |
| 3689 | ASSERT_TRUE(response->headers); |
| 3690 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3691 | EXPECT_EQ(407, response->headers->response_code()); |
| 3692 | EXPECT_EQ(10, response->headers->GetContentLength()); |
| 3693 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3694 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 3695 | EXPECT_TRUE(response->did_use_http_auth); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3696 | |
| 3697 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 3698 | // out of scope. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3699 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 3704 | // proxy connection with HTTP/1.1 responses, when setting up an SSL tunnel, in |
| 3705 | // the case the server sends extra data on the original socket, so it can't be |
| 3706 | // reused. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3707 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveExtraData) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3708 | HttpRequestInfo request; |
| 3709 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3710 | request.url = GURL("https://www.example.org/"); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3711 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 3712 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3713 | request.traffic_annotation = |
| 3714 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3715 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3716 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 3717 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3718 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 3719 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3720 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3721 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3722 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3723 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3724 | // Since we have proxy, should try to establish tunnel. |
| 3725 | MockWrite data_writes1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3726 | MockWrite(ASYNC, 0, |
| 3727 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3728 | "Host: www.example.org:443\r\n" |
| 3729 | "Proxy-Connection: keep-alive\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3730 | }; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3731 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3732 | // The proxy responds to the connect with a 407, using a persistent, but sends |
| 3733 | // extra data, so the socket cannot be reused. |
| 3734 | MockRead data_reads1[] = { |
| 3735 | // No credentials. |
| 3736 | MockRead(ASYNC, 1, |
| 3737 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 3738 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 3739 | "Content-Length: 10\r\n\r\n"), |
| 3740 | MockRead(SYNCHRONOUS, 2, "0123456789"), |
| 3741 | MockRead(SYNCHRONOUS, 3, "I'm broken!"), |
| 3742 | }; |
| 3743 | |
| 3744 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3745 | // After calling trans->RestartWithAuth(), this is the request we should |
| 3746 | // be issuing -- the final header line contains the credentials. |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3747 | MockWrite(ASYNC, 0, |
| 3748 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3749 | "Host: www.example.org:443\r\n" |
| 3750 | "Proxy-Connection: keep-alive\r\n" |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3751 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3752 | |
| 3753 | MockWrite(ASYNC, 2, |
| 3754 | "GET / HTTP/1.1\r\n" |
| 3755 | "Host: www.example.org\r\n" |
| 3756 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3757 | }; |
| 3758 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3759 | MockRead data_reads2[] = { |
| 3760 | MockRead(ASYNC, 1, "HTTP/1.1 200 Connection Established\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3761 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3762 | MockRead(ASYNC, 3, |
| 3763 | "HTTP/1.1 200 OK\r\n" |
| 3764 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 3765 | "Content-Length: 5\r\n\r\n"), |
| 3766 | // No response body because the test stops reading here. |
| 3767 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED, 4), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3768 | }; |
| 3769 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3770 | SequencedSocketData data1(data_reads1, data_writes1); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3771 | data1.set_busy_before_sync_reads(true); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3772 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3773 | SequencedSocketData data2(data_reads2, data_writes2); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3774 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 3775 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3776 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3777 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3778 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3779 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 3780 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 3781 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3782 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3783 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3784 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3785 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 3786 | auto entries = log.GetEntries(); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 3787 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3788 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 3789 | NetLogEventPhase::NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 3790 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 3791 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 3792 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 3793 | NetLogEventPhase::NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3794 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3795 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3796 | ASSERT_TRUE(response); |
| 3797 | ASSERT_TRUE(response->headers); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3798 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3799 | EXPECT_EQ(407, response->headers->response_code()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3800 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3801 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3802 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3803 | LoadTimingInfo load_timing_info; |
| 3804 | // CONNECT requests and responses are handled at the connect job level, so |
| 3805 | // the transaction does not yet have a connection. |
| 3806 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3807 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3808 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3809 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3810 | rv = |
| 3811 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3812 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3813 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3814 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3815 | EXPECT_EQ(200, response->headers->response_code()); |
| 3816 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3817 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 3818 | |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3819 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3820 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3821 | |
| 3822 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 3823 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 3824 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 3825 | |
| 3826 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3827 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 3828 | } |
| 3829 | |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3830 | // Test the case a proxy closes a socket while the challenge body is being |
| 3831 | // drained. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3832 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAliveHangupDuringBody) { |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3833 | HttpRequestInfo request; |
| 3834 | request.method = "GET"; |
| 3835 | request.url = GURL("https://www.example.org/"); |
| 3836 | // Ensure that proxy authentication is attempted even |
| 3837 | // when the no authentication data flag is set. |
| 3838 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3839 | request.traffic_annotation = |
| 3840 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3841 | |
| 3842 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3843 | session_deps_.proxy_resolution_service = |
| 3844 | ConfiguredProxyResolutionService::CreateFixed( |
| 3845 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3846 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3847 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3848 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3849 | |
| 3850 | // Since we have proxy, should try to establish tunnel. |
| 3851 | MockWrite data_writes1[] = { |
| 3852 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3853 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3854 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3855 | }; |
| 3856 | |
| 3857 | // The proxy responds to the connect with a 407, using a persistent |
| 3858 | // connection. |
| 3859 | MockRead data_reads1[] = { |
| 3860 | // No credentials. |
| 3861 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3862 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3863 | MockRead("Content-Length: 10\r\n\r\n"), MockRead("spam!"), |
| 3864 | // Server hands up in the middle of the body. |
| 3865 | MockRead(ASYNC, ERR_CONNECTION_CLOSED), |
| 3866 | }; |
| 3867 | |
| 3868 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3869 | // After calling trans.RestartWithAuth(), this is the request we should |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3870 | // be issuing -- the final header line contains the credentials. |
| 3871 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3872 | "Host: www.example.org:443\r\n" |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3873 | "Proxy-Connection: keep-alive\r\n" |
| 3874 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3875 | |
| 3876 | MockWrite("GET / HTTP/1.1\r\n" |
| 3877 | "Host: www.example.org\r\n" |
| 3878 | "Connection: keep-alive\r\n\r\n"), |
| 3879 | }; |
| 3880 | |
| 3881 | MockRead data_reads2[] = { |
| 3882 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 3883 | |
| 3884 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3885 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3886 | MockRead("Content-Length: 5\r\n\r\n"), |
| 3887 | MockRead(SYNCHRONOUS, "hello"), |
| 3888 | }; |
| 3889 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3890 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3891 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3892 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3893 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 3894 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 3895 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 3896 | |
| 3897 | TestCompletionCallback callback; |
| 3898 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3899 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3900 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3901 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3902 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3903 | ASSERT_TRUE(response); |
| 3904 | ASSERT_TRUE(response->headers); |
| 3905 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3906 | EXPECT_EQ(407, response->headers->response_code()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 3907 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3908 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3909 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3910 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3911 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3912 | response = trans.GetResponseInfo(); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3913 | ASSERT_TRUE(response); |
| 3914 | ASSERT_TRUE(response->headers); |
| 3915 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3916 | EXPECT_EQ(200, response->headers->response_code()); |
| 3917 | std::string body; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3918 | EXPECT_THAT(ReadTransaction(&trans, &body), IsOk()); |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 3919 | EXPECT_EQ("hello", body); |
| 3920 | } |
| 3921 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3922 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 3923 | // even if the user cancels the proxy's auth attempt. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 3924 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3925 | HttpRequestInfo request; |
| 3926 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 3927 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 3928 | request.traffic_annotation = |
| 3929 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3930 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3931 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3932 | session_deps_.proxy_resolution_service = |
| 3933 | ConfiguredProxyResolutionService::CreateFixed( |
| 3934 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3935 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 3936 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3937 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3938 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3939 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3940 | // Since we have proxy, should try to establish tunnel. |
| 3941 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 3942 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 3943 | "Host: www.example.org:443\r\n" |
| 3944 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3945 | }; |
| 3946 | |
| 3947 | // The proxy responds to the connect with a 407. |
| 3948 | MockRead data_reads[] = { |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3949 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 3950 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3951 | MockRead("Content-Length: 10\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 3952 | MockRead("0123456789"), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3953 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3954 | }; |
| 3955 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 3956 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 3957 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3958 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3959 | TestCompletionCallback callback; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3960 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 3961 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3962 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3963 | |
| 3964 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3965 | EXPECT_THAT(rv, IsOk()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3966 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3967 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 3968 | ASSERT_TRUE(response); |
| 3969 | ASSERT_TRUE(response->headers); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3970 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3971 | EXPECT_EQ(407, response->headers->response_code()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3972 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3973 | |
| 3974 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 3975 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3976 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 3977 | |
| 3978 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3979 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 3980 | } |
| 3981 | |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3982 | // Test the no-tunnel HTTP auth case where proxy and server origins and realms |
| 3983 | // are the same, but the user/passwords are different. Serves to verify |
| 3984 | // credentials are correctly separated based on HttpAuth::Target. |
| 3985 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyMatchesServerAuthNoTunnel) { |
| 3986 | HttpRequestInfo request; |
| 3987 | request.method = "GET"; |
| 3988 | request.url = GURL("http://myproxy:70/"); |
| 3989 | request.traffic_annotation = |
| 3990 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 3991 | |
| 3992 | // Proxy matches request URL. |
| 3993 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 3994 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3995 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 3996 | RecordingBoundTestNetLog log; |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 3997 | session_deps_.net_log = log.bound().net_log(); |
| 3998 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 3999 | |
| 4000 | MockWrite data_writes[] = { |
| 4001 | // Initial request gets a proxy auth challenge. |
| 4002 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4003 | "Host: myproxy:70\r\n" |
| 4004 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4005 | // Retry with proxy auth credentials, which will result in a server auth |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4006 | // challenge. |
| 4007 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4008 | "Host: myproxy:70\r\n" |
| 4009 | "Proxy-Connection: keep-alive\r\n" |
| 4010 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4011 | // Retry with proxy and server auth credentials, which gets a response. |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4012 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4013 | "Host: myproxy:70\r\n" |
| 4014 | "Proxy-Connection: keep-alive\r\n" |
| 4015 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4016 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4017 | // A second request should preemptively send the correct proxy and server |
| 4018 | // auth headers. |
| 4019 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4020 | "Host: myproxy:70\r\n" |
| 4021 | "Proxy-Connection: keep-alive\r\n" |
| 4022 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4023 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4024 | }; |
| 4025 | |
| 4026 | MockRead data_reads[] = { |
| 4027 | // Proxy auth challenge. |
| 4028 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4029 | "Proxy-Connection: keep-alive\r\n" |
| 4030 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4031 | "Content-Length: 0\r\n\r\n"), |
| 4032 | // Server auth challenge. |
| 4033 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4034 | "Proxy-Connection: keep-alive\r\n" |
| 4035 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4036 | "Content-Length: 0\r\n\r\n"), |
| 4037 | // Response. |
| 4038 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4039 | "Proxy-Connection: keep-alive\r\n" |
| 4040 | "Content-Length: 5\r\n\r\n" |
| 4041 | "hello"), |
| 4042 | // Response to second request. |
| 4043 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4044 | "Proxy-Connection: keep-alive\r\n" |
| 4045 | "Content-Length: 2\r\n\r\n" |
| 4046 | "hi"), |
| 4047 | }; |
| 4048 | |
| 4049 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4050 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4051 | |
| 4052 | TestCompletionCallback callback; |
| 4053 | |
| 4054 | auto trans = |
| 4055 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4056 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4057 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4058 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4059 | ASSERT_TRUE(response); |
| 4060 | ASSERT_TRUE(response->headers); |
| 4061 | EXPECT_EQ(407, response->headers->response_code()); |
| 4062 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
| 4063 | |
| 4064 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4065 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4066 | response = trans->GetResponseInfo(); |
| 4067 | ASSERT_TRUE(response); |
| 4068 | EXPECT_EQ(401, response->headers->response_code()); |
| 4069 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4070 | EXPECT_EQ("http://myproxy:70", |
| 4071 | response->auth_challenge->challenger.Serialize()); |
| 4072 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4073 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4074 | |
| 4075 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4076 | callback.callback()); |
| 4077 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4078 | response = trans->GetResponseInfo(); |
| 4079 | ASSERT_TRUE(response); |
| 4080 | EXPECT_EQ(200, response->headers->response_code()); |
| 4081 | // The password prompt info should not be set. |
| 4082 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4083 | |
| 4084 | std::string response_data; |
| 4085 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4086 | EXPECT_EQ("hello", response_data); |
| 4087 | |
| 4088 | // Check that the credentials were cached correctly. |
| 4089 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4090 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4091 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4092 | ASSERT_TRUE(entry); |
| 4093 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4094 | ASSERT_EQ(kBar, entry->credentials().password()); |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4095 | entry = session->http_auth_cache()->Lookup( |
| 4096 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4097 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey()); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4098 | ASSERT_TRUE(entry); |
| 4099 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4100 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4101 | |
| 4102 | // Make another request, which should automatically send the correct proxy and |
| 4103 | // server auth credentials and get another response. |
| 4104 | trans = |
| 4105 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4106 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4107 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4108 | response = trans->GetResponseInfo(); |
| 4109 | ASSERT_TRUE(response); |
| 4110 | EXPECT_EQ(200, response->headers->response_code()); |
| 4111 | // The password prompt info should not be set. |
| 4112 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4113 | |
| 4114 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4115 | EXPECT_EQ("hi", response_data); |
| 4116 | |
| 4117 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 4118 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 4119 | } |
| 4120 | |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4121 | // Test the no-tunnel HTTP auth case where proxy and server origins and realms |
| 4122 | // are the same, but the user/passwords are different, and with different |
| 4123 | // NetworkIsolationKeys. Sends one request with a NIK, response to both proxy |
| 4124 | // and auth challenges, sends another request with another NIK, expecting only |
| 4125 | // the proxy credentials to be cached, and thus sees only a server auth |
| 4126 | // challenge. Then sends a request with the original NIK, expecting cached proxy |
| 4127 | // and auth credentials that match the ones used in the first request. |
| 4128 | // |
| 4129 | // Serves to verify credentials are correctly separated based on |
| 4130 | // HttpAuth::Target and NetworkIsolationKeys, but NetworkIsolationKey only |
| 4131 | // affects server credentials, not proxy credentials. |
| 4132 | TEST_F(HttpNetworkTransactionTest, |
| 4133 | BasicAuthProxyMatchesServerAuthWithNetworkIsolationKeyNoTunnel) { |
| 4134 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 4135 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 4136 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 4137 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 4138 | |
| 4139 | // This test would need to use a single socket without this option enabled. |
| 4140 | // Best to use this option when it would affect a test, as it will eventually |
| 4141 | // become the default behavior. |
| 4142 | base::test::ScopedFeatureList feature_list; |
| 4143 | feature_list.InitAndEnableFeature( |
| 4144 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 4145 | |
| 4146 | // Proxy matches request URL. |
| 4147 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4148 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4149 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 4150 | RecordingBoundTestNetLog log; |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4151 | session_deps_.net_log = log.bound().net_log(); |
| 4152 | session_deps_.key_auth_cache_server_entries_by_network_isolation_key = true; |
| 4153 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 4154 | |
| 4155 | MockWrite data_writes[] = { |
| 4156 | // Initial request gets a proxy auth challenge. |
| 4157 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4158 | "Host: myproxy:70\r\n" |
| 4159 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4160 | // Retry with proxy auth credentials, which will result in a server auth |
| 4161 | // challenge. |
| 4162 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4163 | "Host: myproxy:70\r\n" |
| 4164 | "Proxy-Connection: keep-alive\r\n" |
| 4165 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4166 | // Retry with proxy and server auth credentials, which gets a response. |
| 4167 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4168 | "Host: myproxy:70\r\n" |
| 4169 | "Proxy-Connection: keep-alive\r\n" |
| 4170 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4171 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4172 | // Another request to the same server and using the same NIK should |
| 4173 | // preemptively send the correct cached proxy and server |
| 4174 | // auth headers. |
| 4175 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4176 | "Host: myproxy:70\r\n" |
| 4177 | "Proxy-Connection: keep-alive\r\n" |
| 4178 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4179 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4180 | }; |
| 4181 | |
| 4182 | MockRead data_reads[] = { |
| 4183 | // Proxy auth challenge. |
| 4184 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4185 | "Proxy-Connection: keep-alive\r\n" |
| 4186 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4187 | "Content-Length: 0\r\n\r\n"), |
| 4188 | // Server auth challenge. |
| 4189 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4190 | "Proxy-Connection: keep-alive\r\n" |
| 4191 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4192 | "Content-Length: 0\r\n\r\n"), |
| 4193 | // Response. |
| 4194 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4195 | "Proxy-Connection: keep-alive\r\n" |
| 4196 | "Content-Length: 5\r\n\r\n" |
| 4197 | "hello"), |
| 4198 | // Response to second request. |
| 4199 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4200 | "Proxy-Connection: keep-alive\r\n" |
| 4201 | "Content-Length: 2\r\n\r\n" |
| 4202 | "hi"), |
| 4203 | }; |
| 4204 | |
| 4205 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4206 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4207 | |
| 4208 | MockWrite data_writes2[] = { |
| 4209 | // Initial request using a different NetworkIsolationKey includes the |
| 4210 | // cached proxy credentials, but not server credentials. |
| 4211 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4212 | "Host: myproxy:70\r\n" |
| 4213 | "Proxy-Connection: keep-alive\r\n" |
| 4214 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4215 | // Retry with proxy and new server auth credentials, which gets a |
| 4216 | // response. |
| 4217 | MockWrite("GET http://myproxy:70/ HTTP/1.1\r\n" |
| 4218 | "Host: myproxy:70\r\n" |
| 4219 | "Proxy-Connection: keep-alive\r\n" |
| 4220 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 4221 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4222 | }; |
| 4223 | |
| 4224 | MockRead data_reads2[] = { |
| 4225 | // Server auth challenge. |
| 4226 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4227 | "Proxy-Connection: keep-alive\r\n" |
| 4228 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4229 | "Content-Length: 0\r\n\r\n"), |
| 4230 | // Response. |
| 4231 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4232 | "Proxy-Connection: keep-alive\r\n" |
| 4233 | "Content-Length: 9\r\n\r\n" |
| 4234 | "greetings"), |
| 4235 | }; |
| 4236 | |
| 4237 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 4238 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4239 | |
| 4240 | TestCompletionCallback callback; |
| 4241 | |
| 4242 | HttpRequestInfo request; |
| 4243 | request.method = "GET"; |
| 4244 | request.url = GURL("http://myproxy:70/"); |
| 4245 | request.traffic_annotation = |
| 4246 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4247 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4248 | |
| 4249 | auto trans = |
| 4250 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4251 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4252 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4253 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4254 | ASSERT_TRUE(response); |
| 4255 | ASSERT_TRUE(response->headers); |
| 4256 | EXPECT_EQ(407, response->headers->response_code()); |
| 4257 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
| 4258 | |
| 4259 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4260 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4261 | response = trans->GetResponseInfo(); |
| 4262 | ASSERT_TRUE(response); |
| 4263 | EXPECT_EQ(401, response->headers->response_code()); |
| 4264 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4265 | EXPECT_EQ("http://myproxy:70", |
| 4266 | response->auth_challenge->challenger.Serialize()); |
| 4267 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4268 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4269 | |
| 4270 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4271 | callback.callback()); |
| 4272 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4273 | response = trans->GetResponseInfo(); |
| 4274 | ASSERT_TRUE(response); |
| 4275 | EXPECT_EQ(200, response->headers->response_code()); |
| 4276 | // The password prompt info should not be set. |
| 4277 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4278 | std::string response_data; |
| 4279 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4280 | EXPECT_EQ("hello", response_data); |
| 4281 | |
| 4282 | // Check that the proxy credentials were cached correctly. The should be |
| 4283 | // accessible with any NetworkIsolationKey. |
| 4284 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4285 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4286 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4287 | ASSERT_TRUE(entry); |
| 4288 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4289 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4290 | EXPECT_EQ(entry, |
| 4291 | session->http_auth_cache()->Lookup( |
| 4292 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4293 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4294 | |
| 4295 | // Check that the server credentials were cached correctly. The should be |
| 4296 | // accessible with only kNetworkIsolationKey1. |
| 4297 | entry = session->http_auth_cache()->Lookup( |
| 4298 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4299 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4300 | ASSERT_TRUE(entry); |
| 4301 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4302 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4303 | // Looking up the server entry with another NetworkIsolationKey should fail. |
| 4304 | EXPECT_FALSE(session->http_auth_cache()->Lookup( |
| 4305 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4306 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4307 | |
| 4308 | // Make another request with a different NetworkIsolationKey. It should use |
| 4309 | // another socket, reuse the cached proxy credentials, but result in a server |
| 4310 | // auth challenge. |
| 4311 | request.network_isolation_key = kNetworkIsolationKey2; |
| 4312 | trans = |
| 4313 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4314 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4315 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4316 | response = trans->GetResponseInfo(); |
| 4317 | ASSERT_TRUE(response); |
| 4318 | EXPECT_EQ(401, response->headers->response_code()); |
| 4319 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4320 | EXPECT_EQ("http://myproxy:70", |
| 4321 | response->auth_challenge->challenger.Serialize()); |
| 4322 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4323 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4324 | |
| 4325 | rv = trans->RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 4326 | callback.callback()); |
| 4327 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4328 | response = trans->GetResponseInfo(); |
| 4329 | ASSERT_TRUE(response); |
| 4330 | EXPECT_EQ(200, response->headers->response_code()); |
| 4331 | // The password prompt info should not be set. |
| 4332 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4333 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4334 | EXPECT_EQ("greetings", response_data); |
| 4335 | |
| 4336 | // Check that the proxy credentials are still cached. |
| 4337 | entry = session->http_auth_cache()->Lookup( |
| 4338 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4339 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4340 | ASSERT_TRUE(entry); |
| 4341 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4342 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4343 | EXPECT_EQ(entry, |
| 4344 | session->http_auth_cache()->Lookup( |
| 4345 | GURL("http://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4346 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4347 | |
| 4348 | // Check that the correct server credentials are cached for each |
| 4349 | // NetworkIsolationKey. |
| 4350 | entry = session->http_auth_cache()->Lookup( |
| 4351 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4352 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4353 | ASSERT_TRUE(entry); |
| 4354 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4355 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4356 | entry = session->http_auth_cache()->Lookup( |
| 4357 | GURL("http://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4358 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2); |
| 4359 | ASSERT_TRUE(entry); |
| 4360 | ASSERT_EQ(kFoo3, entry->credentials().username()); |
| 4361 | ASSERT_EQ(kBar3, entry->credentials().password()); |
| 4362 | |
| 4363 | // Make a request with the original NetworkIsolationKey. It should reuse the |
| 4364 | // first socket, and the proxy credentials sent on the first socket. |
| 4365 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4366 | trans = |
| 4367 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4368 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4369 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4370 | response = trans->GetResponseInfo(); |
| 4371 | ASSERT_TRUE(response); |
| 4372 | EXPECT_EQ(200, response->headers->response_code()); |
| 4373 | // The password prompt info should not be set. |
| 4374 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4375 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4376 | EXPECT_EQ("hi", response_data); |
| 4377 | |
| 4378 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 4379 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | // Much like the test above, but uses tunnelled connections. |
| 4383 | TEST_F(HttpNetworkTransactionTest, |
| 4384 | BasicAuthProxyMatchesServerAuthWithNetworkIsolationKeyWithTunnel) { |
| 4385 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 4386 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 4387 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 4388 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 4389 | |
| 4390 | // This test would need to use a single socket without this option enabled. |
| 4391 | // Best to use this option when it would affect a test, as it will eventually |
| 4392 | // become the default behavior. |
| 4393 | base::test::ScopedFeatureList feature_list; |
| 4394 | feature_list.InitAndEnableFeature( |
| 4395 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 4396 | |
| 4397 | // Proxy matches request URL. |
| 4398 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4399 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4400 | "HTTPS myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 4401 | RecordingBoundTestNetLog log; |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4402 | session_deps_.net_log = log.bound().net_log(); |
| 4403 | session_deps_.key_auth_cache_server_entries_by_network_isolation_key = true; |
| 4404 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 4405 | |
| 4406 | MockWrite data_writes[] = { |
| 4407 | // Initial tunnel request gets a proxy auth challenge. |
| 4408 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4409 | "Host: myproxy:70\r\n" |
| 4410 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4411 | // Retry with proxy auth credentials, which will result in establishing a |
| 4412 | // tunnel. |
| 4413 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4414 | "Host: myproxy:70\r\n" |
| 4415 | "Proxy-Connection: keep-alive\r\n" |
| 4416 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4417 | // Request over the tunnel, which gets a server auth challenge. |
| 4418 | MockWrite("GET / HTTP/1.1\r\n" |
| 4419 | "Host: myproxy:70\r\n" |
| 4420 | "Connection: keep-alive\r\n\r\n"), |
| 4421 | // Retry with server auth credentials, which gets a response. |
| 4422 | MockWrite("GET / HTTP/1.1\r\n" |
| 4423 | "Host: myproxy:70\r\n" |
| 4424 | "Connection: keep-alive\r\n" |
| 4425 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4426 | // Another request to the same server and using the same NIK should |
| 4427 | // preemptively send the correct cached server |
| 4428 | // auth header. Since a tunnel was already established, the proxy headers |
| 4429 | // won't be sent again except when establishing another tunnel. |
| 4430 | MockWrite("GET / HTTP/1.1\r\n" |
| 4431 | "Host: myproxy:70\r\n" |
| 4432 | "Connection: keep-alive\r\n" |
| 4433 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 4434 | }; |
| 4435 | |
| 4436 | MockRead data_reads[] = { |
| 4437 | // Proxy auth challenge. |
| 4438 | MockRead("HTTP/1.0 407 Proxy Authentication Required\r\n" |
| 4439 | "Proxy-Connection: keep-alive\r\n" |
| 4440 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4441 | "Content-Length: 0\r\n\r\n"), |
| 4442 | // Tunnel success |
| 4443 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4444 | // Server auth challenge. |
| 4445 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4446 | "Connection: keep-alive\r\n" |
| 4447 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4448 | "Content-Length: 0\r\n\r\n"), |
| 4449 | // Response. |
| 4450 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4451 | "Connection: keep-alive\r\n" |
| 4452 | "Content-Length: 5\r\n\r\n" |
| 4453 | "hello"), |
| 4454 | // Response to second request. |
| 4455 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4456 | "Connection: keep-alive\r\n" |
| 4457 | "Content-Length: 2\r\n\r\n" |
| 4458 | "hi"), |
| 4459 | }; |
| 4460 | |
| 4461 | StaticSocketDataProvider data(data_reads, data_writes); |
| 4462 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4463 | // One for the proxy connection, one of the server connection. |
| 4464 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 4465 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 4466 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 4467 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 4468 | |
| 4469 | MockWrite data_writes2[] = { |
| 4470 | // Initial request using a different NetworkIsolationKey includes the |
| 4471 | // cached proxy credentials when establishing a tunnel. |
| 4472 | MockWrite("CONNECT myproxy:70 HTTP/1.1\r\n" |
| 4473 | "Host: myproxy:70\r\n" |
| 4474 | "Proxy-Connection: keep-alive\r\n" |
| 4475 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4476 | // Request over the tunnel, which gets a server auth challenge. Cached |
| 4477 | // credentials cannot be used, since the NIK is different. |
| 4478 | MockWrite("GET / HTTP/1.1\r\n" |
| 4479 | "Host: myproxy:70\r\n" |
| 4480 | "Connection: keep-alive\r\n\r\n"), |
| 4481 | // Retry with server auth credentials, which gets a response. |
| 4482 | MockWrite("GET / HTTP/1.1\r\n" |
| 4483 | "Host: myproxy:70\r\n" |
| 4484 | "Connection: keep-alive\r\n" |
| 4485 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4486 | }; |
| 4487 | |
| 4488 | MockRead data_reads2[] = { |
| 4489 | // Tunnel success |
| 4490 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4491 | // Server auth challenge. |
| 4492 | MockRead("HTTP/1.0 401 Authentication Required\r\n" |
| 4493 | "Connection: keep-alive\r\n" |
| 4494 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 4495 | "Content-Length: 0\r\n\r\n"), |
| 4496 | // Response. |
| 4497 | MockRead("HTTP/1.1 200 OK\r\n" |
| 4498 | "Connection: keep-alive\r\n" |
| 4499 | "Content-Length: 9\r\n\r\n" |
| 4500 | "greetings"), |
| 4501 | }; |
| 4502 | |
| 4503 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 4504 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4505 | // One for the proxy connection, one of the server connection. |
| 4506 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 4507 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 4508 | SSLSocketDataProvider ssl4(ASYNC, OK); |
| 4509 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl4); |
| 4510 | |
| 4511 | TestCompletionCallback callback; |
| 4512 | |
| 4513 | HttpRequestInfo request; |
| 4514 | request.method = "GET"; |
| 4515 | request.url = GURL("https://myproxy:70/"); |
| 4516 | request.traffic_annotation = |
| 4517 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 4518 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4519 | |
| 4520 | auto trans = |
| 4521 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4522 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4523 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4524 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4525 | ASSERT_TRUE(response); |
| 4526 | ASSERT_TRUE(response->headers); |
| 4527 | EXPECT_EQ(407, response->headers->response_code()); |
| 4528 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
| 4529 | |
| 4530 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 4531 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4532 | response = trans->GetResponseInfo(); |
| 4533 | ASSERT_TRUE(response); |
| 4534 | EXPECT_EQ(401, response->headers->response_code()); |
| 4535 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4536 | EXPECT_EQ("https://myproxy:70", |
| 4537 | response->auth_challenge->challenger.Serialize()); |
| 4538 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4539 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4540 | |
| 4541 | rv = trans->RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 4542 | callback.callback()); |
| 4543 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4544 | response = trans->GetResponseInfo(); |
| 4545 | ASSERT_TRUE(response); |
| 4546 | EXPECT_EQ(200, response->headers->response_code()); |
| 4547 | // The password prompt info should not be set. |
| 4548 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4549 | std::string response_data; |
| 4550 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4551 | EXPECT_EQ("hello", response_data); |
| 4552 | |
| 4553 | // Check that the proxy credentials were cached correctly. The should be |
| 4554 | // accessible with any NetworkIsolationKey. |
| 4555 | HttpAuthCache::Entry* entry = session->http_auth_cache()->Lookup( |
| 4556 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4557 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4558 | ASSERT_TRUE(entry); |
| 4559 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4560 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4561 | EXPECT_EQ(entry, |
| 4562 | session->http_auth_cache()->Lookup( |
| 4563 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4564 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4565 | |
| 4566 | // Check that the server credentials were cached correctly. The should be |
| 4567 | // accessible with only kNetworkIsolationKey1. |
| 4568 | entry = session->http_auth_cache()->Lookup( |
| 4569 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4570 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4571 | ASSERT_TRUE(entry); |
| 4572 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4573 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4574 | // Looking up the server entry with another NetworkIsolationKey should fail. |
| 4575 | EXPECT_FALSE(session->http_auth_cache()->Lookup( |
| 4576 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4577 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4578 | |
| 4579 | // Make another request with a different NetworkIsolationKey. It should use |
| 4580 | // another socket, reuse the cached proxy credentials, but result in a server |
| 4581 | // auth challenge. |
| 4582 | request.network_isolation_key = kNetworkIsolationKey2; |
| 4583 | trans = |
| 4584 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4585 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4586 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4587 | response = trans->GetResponseInfo(); |
| 4588 | ASSERT_TRUE(response); |
| 4589 | EXPECT_EQ(401, response->headers->response_code()); |
| 4590 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 4591 | EXPECT_EQ("https://myproxy:70", |
| 4592 | response->auth_challenge->challenger.Serialize()); |
| 4593 | EXPECT_EQ("MyRealm1", response->auth_challenge->realm); |
| 4594 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
| 4595 | |
| 4596 | rv = trans->RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 4597 | callback.callback()); |
| 4598 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4599 | response = trans->GetResponseInfo(); |
| 4600 | ASSERT_TRUE(response); |
| 4601 | EXPECT_EQ(200, response->headers->response_code()); |
| 4602 | // The password prompt info should not be set. |
| 4603 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4604 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4605 | EXPECT_EQ("greetings", response_data); |
| 4606 | |
| 4607 | // Check that the proxy credentials are still cached. |
| 4608 | entry = session->http_auth_cache()->Lookup( |
| 4609 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4610 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4611 | ASSERT_TRUE(entry); |
| 4612 | ASSERT_EQ(kFoo, entry->credentials().username()); |
| 4613 | ASSERT_EQ(kBar, entry->credentials().password()); |
| 4614 | EXPECT_EQ(entry, |
| 4615 | session->http_auth_cache()->Lookup( |
| 4616 | GURL("https://myproxy:70"), HttpAuth::AUTH_PROXY, "MyRealm1", |
| 4617 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2)); |
| 4618 | |
| 4619 | // Check that the correct server credentials are cached for each |
| 4620 | // NetworkIsolationKey. |
| 4621 | entry = session->http_auth_cache()->Lookup( |
| 4622 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4623 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey1); |
| 4624 | ASSERT_TRUE(entry); |
| 4625 | ASSERT_EQ(kFoo2, entry->credentials().username()); |
| 4626 | ASSERT_EQ(kBar2, entry->credentials().password()); |
| 4627 | entry = session->http_auth_cache()->Lookup( |
| 4628 | GURL("https://myproxy:70"), HttpAuth::AUTH_SERVER, "MyRealm1", |
| 4629 | HttpAuth::AUTH_SCHEME_BASIC, kNetworkIsolationKey2); |
| 4630 | ASSERT_TRUE(entry); |
| 4631 | ASSERT_EQ(kFoo3, entry->credentials().username()); |
| 4632 | ASSERT_EQ(kBar3, entry->credentials().password()); |
| 4633 | |
| 4634 | // Make a request with the original NetworkIsolationKey. It should reuse the |
| 4635 | // first socket, and the proxy credentials sent on the first socket. |
| 4636 | request.network_isolation_key = kNetworkIsolationKey1; |
| 4637 | trans = |
| 4638 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 4639 | rv = trans->Start(&request, callback.callback(), log.bound()); |
| 4640 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 4641 | response = trans->GetResponseInfo(); |
| 4642 | ASSERT_TRUE(response); |
| 4643 | EXPECT_EQ(200, response->headers->response_code()); |
| 4644 | // The password prompt info should not be set. |
| 4645 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 4646 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 4647 | EXPECT_EQ("hi", response_data); |
| 4648 | |
| 4649 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 4650 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 4651 | } |
| 4652 | |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4653 | // Test that we don't pass extraneous headers from the proxy's response to the |
| 4654 | // caller when the proxy responds to CONNECT with 407. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4655 | TEST_F(HttpNetworkTransactionTest, SanitizeProxyAuthHeaders) { |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4656 | HttpRequestInfo request; |
| 4657 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4658 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 4659 | request.traffic_annotation = |
| 4660 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4661 | |
| 4662 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4663 | session_deps_.proxy_resolution_service = |
| 4664 | ConfiguredProxyResolutionService::CreateFixed( |
| 4665 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4666 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4667 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4668 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4669 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4670 | |
| 4671 | // Since we have proxy, should try to establish tunnel. |
| 4672 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4673 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 4674 | "Host: www.example.org:443\r\n" |
| 4675 | "Proxy-Connection: keep-alive\r\n\r\n"), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4676 | }; |
| 4677 | |
| 4678 | // The proxy responds to the connect with a 407. |
| 4679 | MockRead data_reads[] = { |
| 4680 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4681 | MockRead("X-Foo: bar\r\n"), |
| 4682 | MockRead("Set-Cookie: foo=bar\r\n"), |
| 4683 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4684 | MockRead("Content-Length: 10\r\n\r\n"), |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 4685 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4686 | }; |
| 4687 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4688 | StaticSocketDataProvider data(data_reads, data_writes); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4689 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 4690 | |
| 4691 | TestCompletionCallback callback; |
| 4692 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4693 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4694 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4695 | |
| 4696 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4697 | EXPECT_THAT(rv, IsOk()); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4698 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4699 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4700 | ASSERT_TRUE(response); |
| 4701 | ASSERT_TRUE(response->headers); |
| 4702 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4703 | EXPECT_EQ(407, response->headers->response_code()); |
| 4704 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4705 | EXPECT_FALSE(response->headers->HasHeader("X-Foo")); |
| 4706 | EXPECT_FALSE(response->headers->HasHeader("Set-Cookie")); |
| 4707 | |
| 4708 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4709 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4710 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4711 | |
| 4712 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 4713 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
ttuttle | 7933c11 | 2015-01-06 00:55:24 | [diff] [blame] | 4714 | } |
| 4715 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4716 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 4717 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4718 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4719 | HttpRequestInfo request; |
| 4720 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4721 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 4722 | request.traffic_annotation = |
| 4723 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4724 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4725 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4726 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4727 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4728 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4729 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4730 | MockWrite( |
| 4731 | "GET / HTTP/1.1\r\n" |
| 4732 | "Host: www.example.org\r\n" |
| 4733 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4734 | }; |
| 4735 | |
| 4736 | MockRead data_reads1[] = { |
| 4737 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 4738 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4739 | // Large content-length -- won't matter, as connection will be reset. |
| 4740 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4741 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4742 | }; |
| 4743 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4744 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4745 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4746 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4747 | TestCompletionCallback callback; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4748 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4749 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4750 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4751 | |
| 4752 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4753 | EXPECT_THAT(rv, IsError(ERR_UNEXPECTED_PROXY_AUTH)); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 4754 | } |
| 4755 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4756 | // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) |
| 4757 | // through a non-authenticating proxy. The request should fail with |
| 4758 | // ERR_UNEXPECTED_PROXY_AUTH. |
| 4759 | // Note that it is impossible to detect if an HTTP server returns a 407 through |
| 4760 | // a non-authenticating proxy - there is nothing to indicate whether the |
| 4761 | // response came from the proxy or the server, so it is treated as if the proxy |
| 4762 | // issued the challenge. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4763 | TEST_F(HttpNetworkTransactionTest, HttpsServerRequestsProxyAuthThroughProxy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4764 | HttpRequestInfo request; |
| 4765 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 4766 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 4767 | request.traffic_annotation = |
| 4768 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4769 | |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4770 | session_deps_.proxy_resolution_service = |
| 4771 | ConfiguredProxyResolutionService::CreateFixed( |
| 4772 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 4773 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4774 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4775 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4776 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4777 | // Since we have proxy, should try to establish tunnel. |
| 4778 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4779 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 4780 | "Host: www.example.org:443\r\n" |
| 4781 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4782 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4783 | MockWrite("GET / HTTP/1.1\r\n" |
| 4784 | "Host: www.example.org\r\n" |
| 4785 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4786 | }; |
| 4787 | |
| 4788 | MockRead data_reads1[] = { |
| 4789 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 4790 | |
| 4791 | MockRead("HTTP/1.1 407 Unauthorized\r\n"), |
| 4792 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4793 | MockRead("\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4794 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4795 | }; |
| 4796 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4797 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4798 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 4799 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 4800 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4801 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4802 | TestCompletionCallback callback1; |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4803 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4804 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4805 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 4806 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4807 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4808 | |
| 4809 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4810 | EXPECT_THAT(rv, IsError(ERR_UNEXPECTED_PROXY_AUTH)); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 4811 | auto entries = log.GetEntries(); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4812 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 4813 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 4814 | NetLogEventPhase::NONE); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4815 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 4816 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 4817 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 4818 | NetLogEventPhase::NONE); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 4819 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4820 | |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4821 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 4822 | // that uses non-persistent connections. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4823 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4824 | AuthAllowsDefaultCredentialsTunnelConnectionClose) { |
| 4825 | HttpRequestInfo request; |
| 4826 | request.method = "GET"; |
| 4827 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 4828 | request.traffic_annotation = |
| 4829 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4830 | |
| 4831 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 4832 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4833 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4834 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4835 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4836 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4837 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4838 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4839 | mock_handler->set_allows_default_credentials(true); |
| 4840 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 4841 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4842 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4843 | |
| 4844 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 4845 | session_deps_.net_log = NetLog::Get(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4846 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4847 | |
| 4848 | // Since we have proxy, should try to establish tunnel. |
| 4849 | MockWrite data_writes1[] = { |
| 4850 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4851 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4852 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4853 | }; |
| 4854 | |
| 4855 | // The proxy responds to the connect with a 407, using a non-persistent |
| 4856 | // connection. |
| 4857 | MockRead data_reads1[] = { |
| 4858 | // No credentials. |
| 4859 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4860 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 4861 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4862 | }; |
| 4863 | |
| 4864 | // Since the first connection couldn't be reused, need to establish another |
| 4865 | // once given credentials. |
| 4866 | MockWrite data_writes2[] = { |
| 4867 | // After calling trans->RestartWithAuth(), this is the request we should |
| 4868 | // be issuing -- the final header line contains the credentials. |
| 4869 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4870 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4871 | "Proxy-Connection: keep-alive\r\n" |
| 4872 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 4873 | |
| 4874 | MockWrite("GET / HTTP/1.1\r\n" |
| 4875 | "Host: www.example.org\r\n" |
| 4876 | "Connection: keep-alive\r\n\r\n"), |
| 4877 | }; |
| 4878 | |
| 4879 | MockRead data_reads2[] = { |
| 4880 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 4881 | |
| 4882 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4883 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4884 | MockRead("Content-Length: 5\r\n\r\n"), |
| 4885 | MockRead(SYNCHRONOUS, "hello"), |
| 4886 | }; |
| 4887 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4888 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4889 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 4890 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4891 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 4892 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 4893 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 4894 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 4895 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4896 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4897 | |
| 4898 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 4899 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4900 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4901 | |
| 4902 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4903 | ASSERT_TRUE(response); |
| 4904 | ASSERT_TRUE(response->headers); |
| 4905 | EXPECT_FALSE(response->headers->IsKeepAlive()); |
| 4906 | EXPECT_EQ(407, response->headers->response_code()); |
| 4907 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4908 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 4909 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4910 | |
| 4911 | LoadTimingInfo load_timing_info; |
| 4912 | // CONNECT requests and responses are handled at the connect job level, so |
| 4913 | // the transaction does not yet have a connection. |
| 4914 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 4915 | |
| 4916 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4917 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4918 | response = trans->GetResponseInfo(); |
| 4919 | ASSERT_TRUE(response); |
| 4920 | ASSERT_TRUE(response->headers); |
| 4921 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4922 | EXPECT_EQ(200, response->headers->response_code()); |
| 4923 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 4924 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4925 | |
| 4926 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 4927 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4928 | |
| 4929 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 4930 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 4931 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 4932 | |
| 4933 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 4934 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 4938 | // that hangs up when credentials are initially sent. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 4939 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4940 | AuthAllowsDefaultCredentialsTunnelServerClosesConnection) { |
| 4941 | HttpRequestInfo request; |
| 4942 | request.method = "GET"; |
| 4943 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 4944 | request.traffic_annotation = |
| 4945 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4946 | |
| 4947 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 4948 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 4949 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 4950 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4951 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4952 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4953 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 4954 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4955 | mock_handler->set_allows_default_credentials(true); |
| 4956 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 4957 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4958 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4959 | |
| 4960 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 4961 | session_deps_.net_log = NetLog::Get(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 4962 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4963 | |
| 4964 | // Should try to establish tunnel. |
| 4965 | MockWrite data_writes1[] = { |
| 4966 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4967 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4968 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4969 | |
| 4970 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4971 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4972 | "Proxy-Connection: keep-alive\r\n" |
| 4973 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 4974 | }; |
| 4975 | |
| 4976 | // The proxy responds to the connect with a 407, using a non-persistent |
| 4977 | // connection. |
| 4978 | MockRead data_reads1[] = { |
| 4979 | // No credentials. |
| 4980 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4981 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 4982 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 4983 | }; |
| 4984 | |
| 4985 | // Since the first connection was closed, need to establish another once given |
| 4986 | // credentials. |
| 4987 | MockWrite data_writes2[] = { |
| 4988 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 4989 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 4990 | "Proxy-Connection: keep-alive\r\n" |
| 4991 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 4992 | |
| 4993 | MockWrite("GET / HTTP/1.1\r\n" |
| 4994 | "Host: www.example.org\r\n" |
| 4995 | "Connection: keep-alive\r\n\r\n"), |
| 4996 | }; |
| 4997 | |
| 4998 | MockRead data_reads2[] = { |
| 4999 | MockRead("HTTP/1.0 200 Connection Established\r\n\r\n"), |
| 5000 | |
| 5001 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5002 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5003 | MockRead("Content-Length: 5\r\n\r\n"), |
| 5004 | MockRead(SYNCHRONOUS, "hello"), |
| 5005 | }; |
| 5006 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5007 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5008 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5009 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5010 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5011 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 5012 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5013 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5014 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5015 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5016 | |
| 5017 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5018 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5019 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5020 | |
| 5021 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5022 | ASSERT_TRUE(response); |
| 5023 | ASSERT_TRUE(response->headers); |
| 5024 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5025 | EXPECT_EQ(407, response->headers->response_code()); |
| 5026 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5027 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5028 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5029 | |
| 5030 | LoadTimingInfo load_timing_info; |
| 5031 | // CONNECT requests and responses are handled at the connect job level, so |
| 5032 | // the transaction does not yet have a connection. |
| 5033 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5034 | |
| 5035 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5036 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5037 | |
| 5038 | response = trans->GetResponseInfo(); |
| 5039 | ASSERT_TRUE(response); |
| 5040 | ASSERT_TRUE(response->headers); |
| 5041 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5042 | EXPECT_EQ(200, response->headers->response_code()); |
| 5043 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 5044 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5045 | |
| 5046 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5047 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5048 | |
| 5049 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5050 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 5051 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 5052 | |
| 5053 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5054 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 5058 | // that hangs up when credentials are initially sent, and hangs up again when |
| 5059 | // they are retried. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5060 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5061 | AuthAllowsDefaultCredentialsTunnelServerClosesConnectionTwice) { |
| 5062 | HttpRequestInfo request; |
| 5063 | request.method = "GET"; |
| 5064 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5065 | request.traffic_annotation = |
| 5066 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5067 | |
| 5068 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5069 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5070 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5071 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5072 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5073 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5074 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5075 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5076 | mock_handler->set_allows_default_credentials(true); |
| 5077 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5078 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5079 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5080 | |
| 5081 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 5082 | session_deps_.net_log = NetLog::Get(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5083 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5084 | |
| 5085 | // Should try to establish tunnel. |
| 5086 | MockWrite data_writes1[] = { |
| 5087 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5088 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5089 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5090 | |
| 5091 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5092 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5093 | "Proxy-Connection: keep-alive\r\n" |
| 5094 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5095 | }; |
| 5096 | |
| 5097 | // The proxy responds to the connect with a 407, and then hangs up after the |
| 5098 | // second request is sent. |
| 5099 | MockRead data_reads1[] = { |
| 5100 | // No credentials. |
| 5101 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5102 | MockRead("Content-Length: 0\r\n"), |
| 5103 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5104 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 5105 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5106 | }; |
| 5107 | |
| 5108 | // HttpNetworkTransaction sees a reused connection that was closed with |
| 5109 | // ERR_CONNECTION_CLOSED, realized it might have been a race, so retries the |
| 5110 | // request. |
| 5111 | MockWrite data_writes2[] = { |
| 5112 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5113 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5114 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5115 | }; |
| 5116 | |
| 5117 | // The proxy, having had more than enough of us, just hangs up. |
| 5118 | MockRead data_reads2[] = { |
| 5119 | // No credentials. |
| 5120 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5121 | }; |
| 5122 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5123 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5124 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5125 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5126 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5127 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5128 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5129 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5130 | |
| 5131 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5132 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5133 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5134 | |
| 5135 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5136 | ASSERT_TRUE(response); |
| 5137 | ASSERT_TRUE(response->headers); |
| 5138 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 5139 | EXPECT_EQ(407, response->headers->response_code()); |
| 5140 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5141 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5142 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5143 | |
| 5144 | LoadTimingInfo load_timing_info; |
| 5145 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5146 | |
| 5147 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5148 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_EMPTY_RESPONSE)); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5149 | |
| 5150 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5151 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5152 | } |
| 5153 | |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5154 | // This test exercises an odd edge case where the proxy closes the connection |
| 5155 | // after the authentication handshake is complete. Presumably this technique is |
| 5156 | // used in lieu of returning a 403 or 5xx status code when the authentication |
| 5157 | // succeeds, but the user is not authorized to connect to the destination |
| 5158 | // server. There's no standard for what a proxy should do to indicate a blocked |
| 5159 | // site. |
| 5160 | TEST_F(HttpNetworkTransactionTest, |
| 5161 | AuthAllowsDefaultCredentialsTunnelConnectionClosesBeforeBody) { |
| 5162 | HttpRequestInfo request; |
| 5163 | request.method = "GET"; |
| 5164 | request.url = GURL("https://www.example.org/"); |
| 5165 | request.traffic_annotation = |
| 5166 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 5167 | |
| 5168 | // Configure against proxy server "myproxy:70". |
| 5169 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5170 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5171 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 5172 | |
| 5173 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
| 5174 | auth_handler_factory->set_do_init_from_challenge(true); |
| 5175 | |
| 5176 | // Create two mock AuthHandlers. This is because the transaction gets retried |
| 5177 | // after the first ERR_CONNECTION_CLOSED since it's ambiguous whether there |
| 5178 | // was a real network error. |
| 5179 | // |
| 5180 | // The handlers support both default and explicit credentials. The retry |
| 5181 | // mentioned above should be able to reuse the default identity. Thus there |
| 5182 | // should never be a need to prompt for explicit credentials. |
| 5183 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
| 5184 | mock_handler->set_allows_default_credentials(true); |
| 5185 | mock_handler->set_allows_explicit_credentials(true); |
| 5186 | mock_handler->set_connection_based(true); |
| 5187 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5188 | HttpAuth::AUTH_PROXY); |
| 5189 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
| 5190 | mock_handler->set_allows_default_credentials(true); |
| 5191 | mock_handler->set_allows_explicit_credentials(true); |
| 5192 | mock_handler->set_connection_based(true); |
| 5193 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5194 | HttpAuth::AUTH_PROXY); |
| 5195 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
| 5196 | |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 5197 | session_deps_.net_log = NetLog::Get(); |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5198 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5199 | |
| 5200 | // Data for both sockets. |
| 5201 | // |
| 5202 | // Writes are for the tunnel establishment attempts and the |
| 5203 | // authentication handshake. |
| 5204 | MockWrite data_writes1[] = { |
| 5205 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5206 | "Host: www.example.org:443\r\n" |
| 5207 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5208 | |
| 5209 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5210 | "Host: www.example.org:443\r\n" |
| 5211 | "Proxy-Connection: keep-alive\r\n" |
| 5212 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5213 | |
| 5214 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5215 | "Host: www.example.org:443\r\n" |
| 5216 | "Proxy-Connection: keep-alive\r\n" |
| 5217 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5218 | }; |
| 5219 | |
| 5220 | // The server side of the authentication handshake. Note that the response to |
| 5221 | // the final CONNECT request is ERR_CONNECTION_CLOSED. |
| 5222 | MockRead data_reads1[] = { |
| 5223 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5224 | MockRead("Content-Length: 0\r\n"), |
| 5225 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5226 | MockRead("Proxy-Authenticate: Mock\r\n\r\n"), |
| 5227 | |
| 5228 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5229 | MockRead("Content-Length: 0\r\n"), |
| 5230 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 5231 | MockRead("Proxy-Authenticate: Mock foo\r\n\r\n"), |
| 5232 | |
| 5233 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED), |
| 5234 | }; |
| 5235 | |
| 5236 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 5237 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 5238 | |
| 5239 | // The second socket is for the reconnection attempt. Data is identical to the |
| 5240 | // first attempt. |
| 5241 | StaticSocketDataProvider data2(data_reads1, data_writes1); |
| 5242 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5243 | |
| 5244 | auto trans = |
| 5245 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 5246 | |
| 5247 | TestCompletionCallback callback; |
| 5248 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5249 | |
| 5250 | // Two rounds per handshake. After one retry, the error is propagated up the |
| 5251 | // stack. |
| 5252 | for (int i = 0; i < 4; ++i) { |
| 5253 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5254 | |
| 5255 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5256 | ASSERT_TRUE(response); |
| 5257 | ASSERT_TRUE(response->headers); |
| 5258 | EXPECT_EQ(407, response->headers->response_code()); |
| 5259 | ASSERT_TRUE(trans->IsReadyToRestartForAuth()); |
| 5260 | |
| 5261 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5262 | } |
| 5263 | |
| 5264 | // One shall be the number thou shalt retry, and the number of the retrying |
| 5265 | // shall be one. Two shalt thou not retry, neither retry thou zero, excepting |
| 5266 | // that thou then proceed to one. Three is right out. Once the number one, |
| 5267 | // being the first number, be reached, then lobbest thou thy |
| 5268 | // ERR_CONNECTION_CLOSED towards they network transaction, who shall snuff it. |
| 5269 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.GetResult(rv)); |
| 5270 | |
| 5271 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5272 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Asanka Herath | bc3f8f6 | 2018-11-16 23:08:30 | [diff] [blame] | 5273 | } |
| 5274 | |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5275 | // Test a proxy auth scheme that allows default credentials and a proxy server |
| 5276 | // that hangs up when credentials are initially sent, and sends a challenge |
| 5277 | // again they are retried. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5278 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5279 | AuthAllowsDefaultCredentialsTunnelServerChallengesTwice) { |
| 5280 | HttpRequestInfo request; |
| 5281 | request.method = "GET"; |
| 5282 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5283 | request.traffic_annotation = |
| 5284 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5285 | |
| 5286 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5287 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5288 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5289 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5290 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5291 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5292 | auth_handler_factory->set_do_init_from_challenge(true); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5293 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5294 | mock_handler->set_allows_default_credentials(true); |
| 5295 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5296 | HttpAuth::AUTH_PROXY); |
| 5297 | // Add another handler for the second challenge. It supports default |
| 5298 | // credentials, but they shouldn't be used, since they were already tried. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5299 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5300 | mock_handler->set_allows_default_credentials(true); |
| 5301 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5302 | HttpAuth::AUTH_PROXY); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5303 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5304 | |
| 5305 | // Add NetLog just so can verify load timing information gets a NetLog ID. |
Matt Mueller | de5dadf | 2019-11-27 20:11:58 | [diff] [blame] | 5306 | session_deps_.net_log = NetLog::Get(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5307 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5308 | |
| 5309 | // Should try to establish tunnel. |
| 5310 | MockWrite data_writes1[] = { |
| 5311 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5312 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5313 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5314 | }; |
| 5315 | |
| 5316 | // The proxy responds to the connect with a 407, using a non-persistent |
| 5317 | // connection. |
| 5318 | MockRead data_reads1[] = { |
| 5319 | // No credentials. |
| 5320 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5321 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 5322 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 5323 | }; |
| 5324 | |
| 5325 | // Since the first connection was closed, need to establish another once given |
| 5326 | // credentials. |
| 5327 | MockWrite data_writes2[] = { |
| 5328 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5329 | "Host: www.example.org:443\r\n" |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5330 | "Proxy-Connection: keep-alive\r\n" |
| 5331 | "Proxy-Authorization: auth_token\r\n\r\n"), |
| 5332 | }; |
| 5333 | |
| 5334 | MockRead data_reads2[] = { |
| 5335 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 5336 | MockRead("Proxy-Authenticate: Mock\r\n"), |
| 5337 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 5338 | }; |
| 5339 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5340 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5341 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5342 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5343 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5344 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 5345 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5346 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5347 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5348 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5349 | |
| 5350 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 5351 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5352 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5353 | |
| 5354 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5355 | ASSERT_TRUE(response); |
| 5356 | ASSERT_TRUE(response->headers); |
| 5357 | EXPECT_EQ(407, response->headers->response_code()); |
| 5358 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5359 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5360 | EXPECT_FALSE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5361 | |
| 5362 | LoadTimingInfo load_timing_info; |
| 5363 | EXPECT_FALSE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 5364 | |
| 5365 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5366 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5367 | response = trans->GetResponseInfo(); |
| 5368 | ASSERT_TRUE(response); |
| 5369 | ASSERT_TRUE(response->headers); |
| 5370 | EXPECT_EQ(407, response->headers->response_code()); |
| 5371 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5372 | EXPECT_TRUE(response->auth_challenge.has_value()); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5373 | |
| 5374 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5375 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
mmenke | 2a1781d | 2015-10-07 19:25:33 | [diff] [blame] | 5376 | } |
| 5377 | |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5378 | // A more nuanced test than GenerateAuthToken test which asserts that |
| 5379 | // ERR_INVALID_AUTH_CREDENTIALS does not cause the auth scheme to be |
| 5380 | // unnecessarily invalidated, and that if the server co-operates, the |
| 5381 | // authentication handshake can continue with the same scheme but with a |
| 5382 | // different identity. |
| 5383 | TEST_F(HttpNetworkTransactionTest, NonPermanentGenerateAuthTokenError) { |
| 5384 | HttpRequestInfo request; |
| 5385 | request.method = "GET"; |
| 5386 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5387 | request.traffic_annotation = |
| 5388 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5389 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5390 | auto auth_handler_factory = std::make_unique<HttpAuthHandlerMock::Factory>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5391 | auth_handler_factory->set_do_init_from_challenge(true); |
| 5392 | |
| 5393 | // First handler. Uses default credentials, but barfs at generate auth token. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5394 | auto mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5395 | mock_handler->set_allows_default_credentials(true); |
| 5396 | mock_handler->set_allows_explicit_credentials(true); |
| 5397 | mock_handler->set_connection_based(true); |
| 5398 | mock_handler->SetGenerateExpectation(true, ERR_INVALID_AUTH_CREDENTIALS); |
| 5399 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5400 | HttpAuth::AUTH_SERVER); |
| 5401 | |
| 5402 | // Add another handler for the second challenge. It supports default |
| 5403 | // credentials, but they shouldn't be used, since they were already tried. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5404 | mock_handler = std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5405 | mock_handler->set_allows_default_credentials(true); |
| 5406 | mock_handler->set_allows_explicit_credentials(true); |
| 5407 | mock_handler->set_connection_based(true); |
| 5408 | auth_handler_factory->AddMockHandler(mock_handler.release(), |
| 5409 | HttpAuth::AUTH_SERVER); |
| 5410 | session_deps_.http_auth_handler_factory = std::move(auth_handler_factory); |
| 5411 | |
| 5412 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5413 | |
| 5414 | MockWrite data_writes1[] = { |
| 5415 | MockWrite("GET / HTTP/1.1\r\n" |
| 5416 | "Host: www.example.org\r\n" |
| 5417 | "Connection: keep-alive\r\n\r\n"), |
| 5418 | }; |
| 5419 | |
| 5420 | MockRead data_reads1[] = { |
| 5421 | MockRead("HTTP/1.1 401 Authentication Required\r\n" |
| 5422 | "WWW-Authenticate: Mock\r\n" |
| 5423 | "Connection: keep-alive\r\n\r\n"), |
| 5424 | }; |
| 5425 | |
| 5426 | // Identical to data_writes1[]. The AuthHandler encounters a |
| 5427 | // ERR_INVALID_AUTH_CREDENTIALS during the GenerateAuthToken stage, so the |
| 5428 | // transaction procceds without an authorization header. |
| 5429 | MockWrite data_writes2[] = { |
| 5430 | MockWrite("GET / HTTP/1.1\r\n" |
| 5431 | "Host: www.example.org\r\n" |
| 5432 | "Connection: keep-alive\r\n\r\n"), |
| 5433 | }; |
| 5434 | |
| 5435 | MockRead data_reads2[] = { |
| 5436 | MockRead("HTTP/1.1 401 Authentication Required\r\n" |
| 5437 | "WWW-Authenticate: Mock\r\n" |
| 5438 | "Connection: keep-alive\r\n\r\n"), |
| 5439 | }; |
| 5440 | |
| 5441 | MockWrite data_writes3[] = { |
| 5442 | MockWrite("GET / HTTP/1.1\r\n" |
| 5443 | "Host: www.example.org\r\n" |
| 5444 | "Connection: keep-alive\r\n" |
| 5445 | "Authorization: auth_token\r\n\r\n"), |
| 5446 | }; |
| 5447 | |
| 5448 | MockRead data_reads3[] = { |
| 5449 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5450 | "Content-Length: 5\r\n" |
| 5451 | "Content-Type: text/plain\r\n" |
| 5452 | "Connection: keep-alive\r\n\r\n" |
| 5453 | "Hello"), |
| 5454 | }; |
| 5455 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5456 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5457 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 5458 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5459 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5460 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 5461 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5462 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5463 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 5464 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5465 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5466 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5467 | |
| 5468 | TestCompletionCallback callback; |
| 5469 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5470 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5471 | |
| 5472 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5473 | ASSERT_TRUE(response); |
| 5474 | ASSERT_TRUE(response->headers); |
| 5475 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 5476 | |
| 5477 | // The following three tests assert that an authentication challenge was |
| 5478 | // received and that the stack is ready to respond to the challenge using |
| 5479 | // ambient credentials. |
| 5480 | EXPECT_EQ(401, response->headers->response_code()); |
| 5481 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5482 | EXPECT_FALSE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5483 | |
| 5484 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5485 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5486 | response = trans->GetResponseInfo(); |
| 5487 | ASSERT_TRUE(response); |
| 5488 | ASSERT_TRUE(response->headers); |
| 5489 | |
| 5490 | // The following three tests assert that an authentication challenge was |
| 5491 | // received and that the stack needs explicit credentials before it is ready |
| 5492 | // to respond to the challenge. |
| 5493 | EXPECT_EQ(401, response->headers->response_code()); |
| 5494 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 5495 | EXPECT_TRUE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5496 | |
| 5497 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
| 5498 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5499 | response = trans->GetResponseInfo(); |
| 5500 | ASSERT_TRUE(response); |
| 5501 | ASSERT_TRUE(response->headers); |
| 5502 | EXPECT_EQ(200, response->headers->response_code()); |
| 5503 | |
| 5504 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5505 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 5506 | } |
| 5507 | |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5508 | // Proxy resolver that returns a proxy with the same host and port for different |
| 5509 | // schemes, based on the path of the URL being requests. |
| 5510 | class SameProxyWithDifferentSchemesProxyResolver : public ProxyResolver { |
| 5511 | public: |
| 5512 | SameProxyWithDifferentSchemesProxyResolver() {} |
| 5513 | ~SameProxyWithDifferentSchemesProxyResolver() override {} |
| 5514 | |
| 5515 | static std::string ProxyHostPortPairAsString() { return "proxy.test:10000"; } |
| 5516 | |
| 5517 | static HostPortPair ProxyHostPortPair() { |
| 5518 | return HostPortPair::FromString(ProxyHostPortPairAsString()); |
| 5519 | } |
| 5520 | |
| 5521 | // ProxyResolver implementation. |
| 5522 | int GetProxyForURL(const GURL& url, |
Matt Menke | cd522ee0 | 2019-11-13 19:53:39 | [diff] [blame] | 5523 | const NetworkIsolationKey& network_isolation_key, |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5524 | ProxyInfo* results, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 5525 | CompletionOnceCallback callback, |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5526 | std::unique_ptr<Request>* request, |
| 5527 | const NetLogWithSource& /*net_log*/) override { |
| 5528 | *results = ProxyInfo(); |
Ramin Halavati | 921731ea | 2018-03-16 08:24:57 | [diff] [blame] | 5529 | results->set_traffic_annotation( |
| 5530 | MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5531 | if (url.path() == "/socks4") { |
| 5532 | results->UsePacString("SOCKS " + ProxyHostPortPairAsString()); |
| 5533 | return OK; |
| 5534 | } |
| 5535 | if (url.path() == "/socks5") { |
| 5536 | results->UsePacString("SOCKS5 " + ProxyHostPortPairAsString()); |
| 5537 | return OK; |
| 5538 | } |
| 5539 | if (url.path() == "/http") { |
| 5540 | results->UsePacString("PROXY " + ProxyHostPortPairAsString()); |
| 5541 | return OK; |
| 5542 | } |
| 5543 | if (url.path() == "/https") { |
| 5544 | results->UsePacString("HTTPS " + ProxyHostPortPairAsString()); |
| 5545 | return OK; |
| 5546 | } |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5547 | if (url.path() == "/https_trusted") { |
| 5548 | results->UseProxyServer(ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5549 | ProxyHostPortPair(), |
| 5550 | true /* is_trusted_proxy */)); |
| 5551 | return OK; |
| 5552 | } |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5553 | NOTREACHED(); |
| 5554 | return ERR_NOT_IMPLEMENTED; |
| 5555 | } |
| 5556 | |
| 5557 | private: |
| 5558 | DISALLOW_COPY_AND_ASSIGN(SameProxyWithDifferentSchemesProxyResolver); |
| 5559 | }; |
| 5560 | |
| 5561 | class SameProxyWithDifferentSchemesProxyResolverFactory |
| 5562 | : public ProxyResolverFactory { |
| 5563 | public: |
| 5564 | SameProxyWithDifferentSchemesProxyResolverFactory() |
| 5565 | : ProxyResolverFactory(false) {} |
| 5566 | |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 5567 | int CreateProxyResolver(const scoped_refptr<PacFileData>& pac_script, |
| 5568 | std::unique_ptr<ProxyResolver>* resolver, |
Bence Béky | cc5b88a | 2018-05-25 20:24:17 | [diff] [blame] | 5569 | CompletionOnceCallback callback, |
Lily Houghton | 9959786 | 2018-03-07 16:40:42 | [diff] [blame] | 5570 | std::unique_ptr<Request>* request) override { |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5571 | *resolver = std::make_unique<SameProxyWithDifferentSchemesProxyResolver>(); |
| 5572 | return OK; |
| 5573 | } |
| 5574 | |
| 5575 | private: |
| 5576 | DISALLOW_COPY_AND_ASSIGN(SameProxyWithDifferentSchemesProxyResolverFactory); |
| 5577 | }; |
| 5578 | |
| 5579 | // Check that when different proxy schemes are all applied to a proxy at the |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5580 | // same address, the connections are not grouped together. i.e., a request to |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5581 | // foo.com using proxy.com as an HTTPS proxy won't use the same socket as a |
| 5582 | // request to foo.com using proxy.com as an HTTP proxy. |
| 5583 | TEST_F(HttpNetworkTransactionTest, SameDestinationForDifferentProxyTypes) { |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5584 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5585 | std::make_unique<ConfiguredProxyResolutionService>( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5586 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 5587 | ProxyConfig::CreateAutoDetect(), TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 5588 | std::make_unique<SameProxyWithDifferentSchemesProxyResolverFactory>(), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 5589 | nullptr, /*quick_check_enabled=*/true); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5590 | |
| 5591 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 5592 | |
| 5593 | MockWrite socks_writes[] = { |
| 5594 | MockWrite(SYNCHRONOUS, kSOCKS4OkRequestLocalHostPort80, |
| 5595 | kSOCKS4OkRequestLocalHostPort80Length), |
| 5596 | MockWrite(SYNCHRONOUS, |
| 5597 | "GET /socks4 HTTP/1.1\r\n" |
| 5598 | "Host: test\r\n" |
| 5599 | "Connection: keep-alive\r\n\r\n"), |
| 5600 | }; |
| 5601 | MockRead socks_reads[] = { |
| 5602 | MockRead(SYNCHRONOUS, kSOCKS4OkReply, kSOCKS4OkReplyLength), |
| 5603 | MockRead("HTTP/1.0 200 OK\r\n" |
| 5604 | "Connection: keep-alive\r\n" |
| 5605 | "Content-Length: 15\r\n\r\n" |
| 5606 | "SOCKS4 Response"), |
| 5607 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5608 | StaticSocketDataProvider socks_data(socks_reads, socks_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5609 | session_deps_.socket_factory->AddSocketDataProvider(&socks_data); |
| 5610 | |
| 5611 | const char kSOCKS5Request[] = { |
| 5612 | 0x05, // Version |
| 5613 | 0x01, // Command (CONNECT) |
| 5614 | 0x00, // Reserved |
| 5615 | 0x03, // Address type (DOMAINNAME) |
| 5616 | 0x04, // Length of domain (4) |
| 5617 | 't', 'e', 's', 't', // Domain string |
| 5618 | 0x00, 0x50, // 16-bit port (80) |
| 5619 | }; |
| 5620 | MockWrite socks5_writes[] = { |
| 5621 | MockWrite(ASYNC, kSOCKS5GreetRequest, kSOCKS5GreetRequestLength), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5622 | MockWrite(ASYNC, kSOCKS5Request, base::size(kSOCKS5Request)), |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5623 | MockWrite(SYNCHRONOUS, |
| 5624 | "GET /socks5 HTTP/1.1\r\n" |
| 5625 | "Host: test\r\n" |
| 5626 | "Connection: keep-alive\r\n\r\n"), |
| 5627 | }; |
| 5628 | MockRead socks5_reads[] = { |
| 5629 | MockRead(ASYNC, kSOCKS5GreetResponse, kSOCKS5GreetResponseLength), |
| 5630 | MockRead(ASYNC, kSOCKS5OkResponse, kSOCKS5OkResponseLength), |
| 5631 | MockRead("HTTP/1.0 200 OK\r\n" |
| 5632 | "Connection: keep-alive\r\n" |
| 5633 | "Content-Length: 15\r\n\r\n" |
| 5634 | "SOCKS5 Response"), |
| 5635 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5636 | StaticSocketDataProvider socks5_data(socks5_reads, socks5_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5637 | session_deps_.socket_factory->AddSocketDataProvider(&socks5_data); |
| 5638 | |
| 5639 | MockWrite http_writes[] = { |
| 5640 | MockWrite(SYNCHRONOUS, |
| 5641 | "GET http://test/http HTTP/1.1\r\n" |
| 5642 | "Host: test\r\n" |
| 5643 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5644 | }; |
| 5645 | MockRead http_reads[] = { |
| 5646 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5647 | "Proxy-Connection: keep-alive\r\n" |
| 5648 | "Content-Length: 13\r\n\r\n" |
| 5649 | "HTTP Response"), |
| 5650 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5651 | StaticSocketDataProvider http_data(http_reads, http_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5652 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 5653 | |
| 5654 | MockWrite https_writes[] = { |
| 5655 | MockWrite(SYNCHRONOUS, |
| 5656 | "GET http://test/https HTTP/1.1\r\n" |
| 5657 | "Host: test\r\n" |
| 5658 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5659 | }; |
| 5660 | MockRead https_reads[] = { |
| 5661 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5662 | "Proxy-Connection: keep-alive\r\n" |
| 5663 | "Content-Length: 14\r\n\r\n" |
| 5664 | "HTTPS Response"), |
| 5665 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5666 | StaticSocketDataProvider https_data(https_reads, https_writes); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5667 | session_deps_.socket_factory->AddSocketDataProvider(&https_data); |
| 5668 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 5669 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 5670 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5671 | MockWrite https_trusted_writes[] = { |
| 5672 | MockWrite(SYNCHRONOUS, |
| 5673 | "GET http://test/https_trusted HTTP/1.1\r\n" |
| 5674 | "Host: test\r\n" |
| 5675 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 5676 | }; |
| 5677 | MockRead https_trusted_reads[] = { |
| 5678 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5679 | "Proxy-Connection: keep-alive\r\n" |
| 5680 | "Content-Length: 22\r\n\r\n" |
| 5681 | "HTTPS Trusted Response"), |
| 5682 | }; |
| 5683 | StaticSocketDataProvider trusted_https_data(https_trusted_reads, |
| 5684 | https_trusted_writes); |
| 5685 | session_deps_.socket_factory->AddSocketDataProvider(&trusted_https_data); |
| 5686 | SSLSocketDataProvider ssl2(SYNCHRONOUS, OK); |
| 5687 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 5688 | |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5689 | struct TestCase { |
| 5690 | GURL url; |
| 5691 | std::string expected_response; |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5692 | // How many idle sockets there should be in the SOCKS 4/5 proxy socket pools |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5693 | // after the test. |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5694 | int expected_idle_socks4_sockets; |
| 5695 | int expected_idle_socks5_sockets; |
| 5696 | // How many idle sockets there should be in the HTTP/HTTPS proxy socket |
| 5697 | // pools after the test. |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5698 | int expected_idle_http_sockets; |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5699 | int expected_idle_https_sockets; |
| 5700 | // How many idle sockets there should be in the HTTPS proxy socket pool with |
| 5701 | // the ProxyServer's |is_trusted_proxy| bit set after the test. |
| 5702 | int expected_idle_trusted_https_sockets; |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5703 | } const kTestCases[] = { |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5704 | {GURL("http://test/socks4"), "SOCKS4 Response", 1, 0, 0, 0, 0}, |
| 5705 | {GURL("http://test/socks5"), "SOCKS5 Response", 1, 1, 0, 0, 0}, |
| 5706 | {GURL("http://test/http"), "HTTP Response", 1, 1, 1, 0, 0}, |
| 5707 | {GURL("http://test/https"), "HTTPS Response", 1, 1, 1, 1, 0}, |
| 5708 | {GURL("http://test/https_trusted"), "HTTPS Trusted Response", 1, 1, 1, 1, |
| 5709 | 1}, |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5710 | }; |
| 5711 | |
| 5712 | for (const auto& test_case : kTestCases) { |
| 5713 | HttpRequestInfo request; |
| 5714 | request.method = "GET"; |
| 5715 | request.url = test_case.url; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5716 | request.traffic_annotation = |
| 5717 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5718 | std::unique_ptr<HttpNetworkTransaction> trans = |
| 5719 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 5720 | session.get()); |
| 5721 | TestCompletionCallback callback; |
| 5722 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 5723 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 5724 | |
| 5725 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5726 | ASSERT_TRUE(response); |
| 5727 | ASSERT_TRUE(response->headers); |
| 5728 | EXPECT_EQ(200, response->headers->response_code()); |
| 5729 | std::string response_data; |
| 5730 | EXPECT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 5731 | EXPECT_EQ(test_case.expected_response, response_data); |
| 5732 | |
| 5733 | // Return the socket to the socket pool, so can make sure it's not used for |
| 5734 | // the next requests. |
| 5735 | trans.reset(); |
| 5736 | base::RunLoop().RunUntilIdle(); |
| 5737 | |
| 5738 | // Check the number of idle sockets in the pool, to make sure that used |
| 5739 | // sockets are indeed being returned to the socket pool. If each request |
| 5740 | // doesn't return an idle socket to the pool, the test would incorrectly |
| 5741 | // pass. |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5742 | EXPECT_EQ(test_case.expected_idle_socks4_sockets, |
| 5743 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5744 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5745 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5746 | ProxyServer(ProxyServer::SCHEME_SOCKS4, |
| 5747 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5748 | ProxyHostPortPair())) |
| 5749 | ->IdleSocketCount()); |
| 5750 | EXPECT_EQ(test_case.expected_idle_socks5_sockets, |
| 5751 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5752 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5753 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5754 | ProxyServer(ProxyServer::SCHEME_SOCKS5, |
| 5755 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5756 | ProxyHostPortPair())) |
| 5757 | ->IdleSocketCount()); |
| 5758 | EXPECT_EQ(test_case.expected_idle_http_sockets, |
| 5759 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5760 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5761 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5762 | ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5763 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5764 | ProxyHostPortPair())) |
| 5765 | ->IdleSocketCount()); |
| 5766 | EXPECT_EQ(test_case.expected_idle_https_sockets, |
| 5767 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5768 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5769 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5770 | ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5771 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5772 | ProxyHostPortPair())) |
| 5773 | ->IdleSocketCount()); |
| 5774 | EXPECT_EQ(test_case.expected_idle_trusted_https_sockets, |
| 5775 | session |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 5776 | ->GetSocketPool( |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 5777 | HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 5778 | ProxyServer(ProxyServer::SCHEME_HTTPS, |
| 5779 | SameProxyWithDifferentSchemesProxyResolver:: |
| 5780 | ProxyHostPortPair(), |
| 5781 | true /* is_trusted_proxy */)) |
| 5782 | ->IdleSocketCount()); |
Matt Menke | d1eb6d4 | 2018-01-17 04:54:06 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5786 | // Test the load timing for HTTPS requests with an HTTP proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5787 | TEST_F(HttpNetworkTransactionTest, HttpProxyLoadTimingNoPacTwoRequests) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5788 | HttpRequestInfo request1; |
| 5789 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5790 | request1.url = GURL("https://www.example.org/1"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5791 | request1.traffic_annotation = |
| 5792 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5793 | |
| 5794 | HttpRequestInfo request2; |
| 5795 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5796 | request2.url = GURL("https://www.example.org/2"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5797 | request2.traffic_annotation = |
| 5798 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5799 | |
| 5800 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5801 | session_deps_.proxy_resolution_service = |
| 5802 | ConfiguredProxyResolutionService::CreateFixed( |
| 5803 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 5804 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5805 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5806 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5807 | |
| 5808 | // Since we have proxy, should try to establish tunnel. |
| 5809 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5810 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5811 | "Host: www.example.org:443\r\n" |
| 5812 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5813 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5814 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 5815 | "Host: www.example.org\r\n" |
| 5816 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5817 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5818 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 5819 | "Host: www.example.org\r\n" |
| 5820 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5821 | }; |
| 5822 | |
| 5823 | // The proxy responds to the connect with a 407, using a persistent |
| 5824 | // connection. |
| 5825 | MockRead data_reads1[] = { |
| 5826 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 5827 | |
| 5828 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5829 | MockRead("Content-Length: 1\r\n\r\n"), |
| 5830 | MockRead(SYNCHRONOUS, "1"), |
| 5831 | |
| 5832 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5833 | MockRead("Content-Length: 2\r\n\r\n"), |
| 5834 | MockRead(SYNCHRONOUS, "22"), |
| 5835 | }; |
| 5836 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5837 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5838 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5839 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5840 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5841 | |
| 5842 | TestCompletionCallback callback1; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5843 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5844 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5845 | |
| 5846 | int rv = trans1->Start(&request1, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5847 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5848 | |
| 5849 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5850 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5851 | |
| 5852 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5853 | ASSERT_TRUE(response1); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5854 | EXPECT_TRUE(response1->proxy_server.is_http()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5855 | ASSERT_TRUE(response1->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5856 | EXPECT_EQ(1, response1->headers->GetContentLength()); |
| 5857 | |
| 5858 | LoadTimingInfo load_timing_info1; |
| 5859 | EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); |
| 5860 | TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_SSL_TIMES); |
| 5861 | |
| 5862 | trans1.reset(); |
| 5863 | |
| 5864 | TestCompletionCallback callback2; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5865 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5866 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5867 | |
| 5868 | rv = trans2->Start(&request2, callback2.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5869 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5870 | |
| 5871 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5872 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5873 | |
| 5874 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5875 | ASSERT_TRUE(response2); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5876 | EXPECT_TRUE(response2->proxy_server.is_http()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5877 | ASSERT_TRUE(response2->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5878 | EXPECT_EQ(2, response2->headers->GetContentLength()); |
| 5879 | |
| 5880 | LoadTimingInfo load_timing_info2; |
| 5881 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 5882 | TestLoadTimingReused(load_timing_info2); |
| 5883 | |
| 5884 | EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 5885 | |
| 5886 | trans2.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5887 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | // Test the load timing for HTTPS requests with an HTTP proxy and a PAC script. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 5891 | TEST_F(HttpNetworkTransactionTest, HttpProxyLoadTimingWithPacTwoRequests) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5892 | HttpRequestInfo request1; |
| 5893 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5894 | request1.url = GURL("https://www.example.org/1"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5895 | request1.traffic_annotation = |
| 5896 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5897 | |
| 5898 | HttpRequestInfo request2; |
| 5899 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 5900 | request2.url = GURL("https://www.example.org/2"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 5901 | request2.traffic_annotation = |
| 5902 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5903 | |
| 5904 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 5905 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 5906 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 5907 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 5908 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5909 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 5910 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5911 | |
| 5912 | // Since we have proxy, should try to establish tunnel. |
| 5913 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5914 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 5915 | "Host: www.example.org:443\r\n" |
| 5916 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5917 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5918 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 5919 | "Host: www.example.org\r\n" |
| 5920 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5921 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 5922 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 5923 | "Host: www.example.org\r\n" |
| 5924 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5925 | }; |
| 5926 | |
| 5927 | // The proxy responds to the connect with a 407, using a persistent |
| 5928 | // connection. |
| 5929 | MockRead data_reads1[] = { |
| 5930 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 5931 | |
| 5932 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5933 | MockRead("Content-Length: 1\r\n\r\n"), |
| 5934 | MockRead(SYNCHRONOUS, "1"), |
| 5935 | |
| 5936 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5937 | MockRead("Content-Length: 2\r\n\r\n"), |
| 5938 | MockRead(SYNCHRONOUS, "22"), |
| 5939 | }; |
| 5940 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 5941 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5942 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5943 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 5944 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5945 | |
| 5946 | TestCompletionCallback callback1; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5947 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5948 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5949 | |
| 5950 | int rv = trans1->Start(&request1, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5951 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5952 | |
| 5953 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5954 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5955 | |
| 5956 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5957 | ASSERT_TRUE(response1); |
| 5958 | ASSERT_TRUE(response1->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5959 | EXPECT_EQ(1, response1->headers->GetContentLength()); |
| 5960 | |
| 5961 | LoadTimingInfo load_timing_info1; |
| 5962 | EXPECT_TRUE(trans1->GetLoadTimingInfo(&load_timing_info1)); |
| 5963 | TestLoadTimingNotReusedWithPac(load_timing_info1, |
| 5964 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 5965 | |
| 5966 | trans1.reset(); |
| 5967 | |
| 5968 | TestCompletionCallback callback2; |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 5969 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 5970 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5971 | |
| 5972 | rv = trans2->Start(&request2, callback2.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5973 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5974 | |
| 5975 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5976 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5977 | |
| 5978 | const HttpResponseInfo* response2 = trans2->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 5979 | ASSERT_TRUE(response2); |
| 5980 | ASSERT_TRUE(response2->headers); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5981 | EXPECT_EQ(2, response2->headers->GetContentLength()); |
| 5982 | |
| 5983 | LoadTimingInfo load_timing_info2; |
| 5984 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 5985 | TestLoadTimingReusedWithPac(load_timing_info2); |
| 5986 | |
| 5987 | EXPECT_EQ(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
| 5988 | |
| 5989 | trans2.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5990 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 5991 | } |
| 5992 | |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 5993 | // Make sure that NetworkIsolationKeys are passed down to the proxy layer. |
| 5994 | TEST_F(HttpNetworkTransactionTest, ProxyResolvedWithNetworkIsolationKey) { |
| 5995 | const url::Origin kOrigin = url::Origin::Create(GURL("https://foo.test/")); |
| 5996 | const net::NetworkIsolationKey kNetworkIsolationKey(kOrigin, kOrigin); |
| 5997 | |
| 5998 | ProxyConfig proxy_config; |
| 5999 | proxy_config.set_auto_detect(true); |
| 6000 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 6001 | |
| 6002 | CapturingProxyResolver capturing_proxy_resolver; |
| 6003 | capturing_proxy_resolver.set_proxy_server(ProxyServer::Direct()); |
| 6004 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6005 | std::make_unique<ConfiguredProxyResolutionService>( |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 6006 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 6007 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 6008 | std::make_unique<CapturingProxyResolverFactory>( |
| 6009 | &capturing_proxy_resolver), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 6010 | nullptr, /*quick_check_enabled=*/true); |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 6011 | |
| 6012 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 6013 | |
| 6014 | // No need to continue with the network request - proxy resolution occurs |
| 6015 | // before establishing a data. |
| 6016 | StaticSocketDataProvider data{base::span<MockRead>(), |
| 6017 | base::span<MockWrite>()}; |
| 6018 | data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_FAILED)); |
| 6019 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 6020 | |
| 6021 | // Run first request until an auth challenge is observed. |
| 6022 | HttpRequestInfo request; |
| 6023 | request.method = "GET"; |
| 6024 | request.url = GURL("http://foo.test/"); |
| 6025 | request.traffic_annotation = |
| 6026 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6027 | HttpNetworkTransaction trans(LOWEST, session.get()); |
| 6028 | TestCompletionCallback callback; |
| 6029 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 6030 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); |
| 6031 | } |
| 6032 | |
dalyk | 6d7a8c5 | 2019-12-18 21:43:01 | [diff] [blame] | 6033 | // Test that a failure in resolving the proxy hostname is retrievable. |
| 6034 | TEST_F(HttpNetworkTransactionTest, ProxyHostResolutionFailure) { |
| 6035 | HttpRequestInfo request; |
| 6036 | request.method = "GET"; |
| 6037 | request.url = GURL("http://www.example.org/"); |
| 6038 | request.traffic_annotation = |
| 6039 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6040 | |
| 6041 | RecordingTestNetLog log; |
| 6042 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6043 | session_deps_.proxy_resolution_service = |
| 6044 | ConfiguredProxyResolutionService::CreateFixed( |
| 6045 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
dalyk | 6d7a8c5 | 2019-12-18 21:43:01 | [diff] [blame] | 6046 | MockHostResolver* resolver = new MockHostResolver(); |
| 6047 | resolver->rules()->AddSimulatedTimeoutFailure("proxy"); |
| 6048 | session_deps_.net_log = &log; |
| 6049 | session_deps_.host_resolver.reset(resolver); |
| 6050 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 6051 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 6052 | TestCompletionCallback callback; |
| 6053 | |
| 6054 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 6055 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 6056 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_CONNECTION_FAILED)); |
| 6057 | |
| 6058 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 6059 | ASSERT_TRUE(response); |
| 6060 | EXPECT_THAT(response->resolve_error_info.error, IsError(ERR_DNS_TIMED_OUT)); |
| 6061 | } |
| 6062 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6063 | // Test a simple get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6064 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6065 | HttpRequestInfo request; |
| 6066 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6067 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6068 | request.traffic_annotation = |
| 6069 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6070 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6071 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6072 | session_deps_.proxy_resolution_service = |
| 6073 | ConfiguredProxyResolutionService::CreateFixed( |
| 6074 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6075 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6076 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6077 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6078 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6079 | // Since we have proxy, should use full url |
| 6080 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6081 | MockWrite( |
| 6082 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 6083 | "Host: www.example.org\r\n" |
| 6084 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6085 | }; |
| 6086 | |
| 6087 | MockRead data_reads1[] = { |
| 6088 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6089 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 6090 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6091 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6092 | }; |
| 6093 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6094 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6095 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6096 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6097 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6098 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6099 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6100 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6101 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6102 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6103 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6104 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6105 | |
| 6106 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6107 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6108 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6109 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6110 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6111 | TestLoadTimingNotReused(load_timing_info, |
| 6112 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 6113 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6114 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6115 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6116 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 6117 | EXPECT_TRUE(response->proxy_server.is_https()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6118 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 6119 | EXPECT_EQ(200, response->headers->response_code()); |
| 6120 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 6121 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 6122 | |
| 6123 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6124 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 6125 | } |
| 6126 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6127 | // Test a SPDY get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6128 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6129 | HttpRequestInfo request; |
| 6130 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6131 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6132 | request.traffic_annotation = |
| 6133 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6134 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6135 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6136 | session_deps_.proxy_resolution_service = |
| 6137 | ConfiguredProxyResolutionService::CreateFixed( |
| 6138 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6139 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6140 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6141 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6142 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6143 | // fetch http://www.example.org/ via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6144 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6145 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6146 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6147 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6148 | spdy::SpdySerializedFrame resp( |
| 6149 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 6150 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6151 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6152 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6153 | }; |
| 6154 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6155 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6156 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6157 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6158 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6159 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6160 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6161 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6162 | TestCompletionCallback callback1; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6163 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6164 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6165 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6166 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6167 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6168 | |
| 6169 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6170 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6171 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6172 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6173 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6174 | TestLoadTimingNotReused(load_timing_info, |
| 6175 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 6176 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6177 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6178 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 6179 | EXPECT_TRUE(response->proxy_server.is_https()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6180 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6181 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6182 | |
| 6183 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6184 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 6185 | EXPECT_EQ(kUploadData, response_data); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 6186 | } |
| 6187 | |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6188 | // Verifies that a session which races and wins against the owning transaction |
| 6189 | // (completing prior to host resolution), doesn't fail the transaction. |
| 6190 | // Regression test for crbug.com/334413. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6191 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithSessionRace) { |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6192 | HttpRequestInfo request; |
| 6193 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6194 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6195 | request.traffic_annotation = |
| 6196 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6197 | |
| 6198 | // Configure SPDY proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6199 | session_deps_.proxy_resolution_service = |
| 6200 | ConfiguredProxyResolutionService::CreateFixed( |
| 6201 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6202 | RecordingBoundTestNetLog log; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6203 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6204 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6205 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6206 | // Fetch http://www.example.org/ through the SPDY proxy. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6207 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6208 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6209 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6210 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6211 | spdy::SpdySerializedFrame resp( |
| 6212 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6213 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6214 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6215 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6216 | }; |
| 6217 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6218 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6219 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 6220 | |
| 6221 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6222 | ssl.next_proto = kProtoHTTP2; |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6223 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 6224 | |
| 6225 | TestCompletionCallback callback1; |
| 6226 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6227 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6228 | |
| 6229 | // Stall the hostname resolution begun by the transaction. |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6230 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 6231 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6232 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6233 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6234 | |
| 6235 | // Race a session to the proxy, which completes first. |
| 6236 | session_deps_.host_resolver->set_ondemand_mode(false); |
Paul Jensen | a457017a | 2018-01-19 23:52:04 | [diff] [blame] | 6237 | SpdySessionKey key(HostPortPair("proxy", 70), ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 6238 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 6239 | SpdySessionKey::IsProxySession::kTrue, SocketTag(), |
| 6240 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6241 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 6242 | CreateSpdySession(session.get(), key, log.bound()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6243 | |
| 6244 | // Unstall the resolution begun by the transaction. |
| 6245 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 6246 | session_deps_.host_resolver->ResolveAllPending(); |
| 6247 | |
| 6248 | EXPECT_FALSE(callback1.have_result()); |
| 6249 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6250 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6251 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6252 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6253 | ASSERT_TRUE(response); |
| 6254 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6255 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6256 | |
| 6257 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6258 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 1c17385 | 2014-06-19 12:51:50 | [diff] [blame] | 6259 | EXPECT_EQ(kUploadData, response_data); |
| 6260 | } |
| 6261 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6262 | // Test a SPDY get through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6263 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithProxyAuth) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6264 | HttpRequestInfo request; |
| 6265 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6266 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6267 | request.traffic_annotation = |
| 6268 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6269 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6270 | // Configure against https proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6271 | session_deps_.proxy_resolution_service = |
| 6272 | ConfiguredProxyResolutionService::CreateFixed( |
| 6273 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6274 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6275 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6276 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6277 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6278 | // The first request will be a bare GET, the second request will be a |
| 6279 | // GET with a Proxy-Authorization header. |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 6280 | spdy_util_.set_default_url(request.url); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6281 | spdy::SpdySerializedFrame req_get( |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 6282 | spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 6283 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6284 | const char* const kExtraAuthorizationHeaders[] = { |
[email protected] | cdf8f7e7 | 2013-05-23 10:56:46 | [diff] [blame] | 6285 | "proxy-authorization", "Basic Zm9vOmJhcg==" |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6286 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6287 | spdy::SpdySerializedFrame req_get_authorization(spdy_util_.ConstructSpdyGet( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 6288 | kExtraAuthorizationHeaders, base::size(kExtraAuthorizationHeaders) / 2, 3, |
Bence Béky | 27ad0a1 | 2018-02-08 00:35:48 | [diff] [blame] | 6289 | LOWEST)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6290 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6291 | CreateMockWrite(req_get, 0), CreateMockWrite(req_get_authorization, 3), |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6292 | }; |
| 6293 | |
| 6294 | // The first response is a 407 proxy authentication challenge, and the second |
| 6295 | // response will be a 200 response since the second request includes a valid |
| 6296 | // Authorization header. |
| 6297 | const char* const kExtraAuthenticationHeaders[] = { |
[email protected] | cdf8f7e7 | 2013-05-23 10:56:46 | [diff] [blame] | 6298 | "proxy-authenticate", "Basic realm=\"MyRealm1\"" |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6299 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6300 | spdy::SpdySerializedFrame resp_authentication( |
| 6301 | spdy_util_.ConstructSpdyReplyError( |
| 6302 | "407", kExtraAuthenticationHeaders, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 6303 | base::size(kExtraAuthenticationHeaders) / 2, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6304 | spdy::SpdySerializedFrame body_authentication( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6305 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6306 | spdy::SpdySerializedFrame resp_data( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6307 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6308 | spdy::SpdySerializedFrame body_data( |
| 6309 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6310 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6311 | CreateMockRead(resp_authentication, 1), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6312 | CreateMockRead(body_authentication, 2, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6313 | CreateMockRead(resp_data, 4), |
| 6314 | CreateMockRead(body_data, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6315 | MockRead(ASYNC, 0, 6), |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6316 | }; |
| 6317 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6318 | SequencedSocketData data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6319 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6320 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6321 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6322 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6323 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6324 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6325 | TestCompletionCallback callback1; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6326 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6327 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6328 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6329 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6330 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6331 | |
| 6332 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6333 | EXPECT_THAT(rv, IsOk()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6334 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6335 | const HttpResponseInfo* const response = trans.GetResponseInfo(); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6336 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6337 | ASSERT_TRUE(response); |
| 6338 | ASSERT_TRUE(response->headers); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6339 | EXPECT_EQ(407, response->headers->response_code()); |
| 6340 | EXPECT_TRUE(response->was_fetched_via_spdy); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6341 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6342 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6343 | TestCompletionCallback callback2; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6344 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6345 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6346 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6347 | |
| 6348 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6349 | EXPECT_THAT(rv, IsOk()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6350 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6351 | const HttpResponseInfo* const response_restart = trans.GetResponseInfo(); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6352 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6353 | ASSERT_TRUE(response_restart); |
| 6354 | ASSERT_TRUE(response_restart->headers); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6355 | EXPECT_EQ(200, response_restart->headers->response_code()); |
| 6356 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6357 | EXPECT_FALSE(response_restart->auth_challenge.has_value()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 6358 | } |
| 6359 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6360 | // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6361 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectHttps) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6362 | HttpRequestInfo request; |
| 6363 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6364 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6365 | request.traffic_annotation = |
| 6366 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6367 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6368 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6369 | session_deps_.proxy_resolution_service = |
| 6370 | ConfiguredProxyResolutionService::CreateFixed( |
| 6371 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6372 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6373 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6374 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6375 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6376 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6377 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6378 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6379 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6380 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6381 | HostPortPair("www.example.org", 443))); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6382 | // fetch https://www.example.org/ via HTTP |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6383 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6384 | const char get[] = |
| 6385 | "GET / HTTP/1.1\r\n" |
| 6386 | "Host: www.example.org\r\n" |
| 6387 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6388 | spdy::SpdySerializedFrame wrapped_get( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6389 | spdy_util_.ConstructSpdyDataFrame(1, get, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6390 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6391 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6392 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 6393 | "Content-Length: 10\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6394 | spdy::SpdySerializedFrame wrapped_get_resp( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6395 | spdy_util_.ConstructSpdyDataFrame(1, resp, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6396 | spdy::SpdySerializedFrame wrapped_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6397 | spdy_util_.ConstructSpdyDataFrame(1, "1234567890", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6398 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6399 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6400 | |
| 6401 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6402 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_get, 2), |
| 6403 | CreateMockWrite(window_update, 6), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6404 | }; |
| 6405 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6406 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6407 | CreateMockRead(conn_resp, 1, ASYNC), |
| 6408 | CreateMockRead(wrapped_get_resp, 3, ASYNC), |
| 6409 | CreateMockRead(wrapped_body, 4, ASYNC), |
| 6410 | CreateMockRead(wrapped_body, 5, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6411 | MockRead(ASYNC, 0, 7), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6412 | }; |
| 6413 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6414 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6415 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6416 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6417 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6418 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6419 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6420 | SSLSocketDataProvider ssl2(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6421 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6422 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6423 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6424 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6425 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6426 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6427 | |
| 6428 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6429 | ASSERT_THAT(rv, IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6430 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6431 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6432 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6433 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6434 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6435 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6436 | ASSERT_TRUE(response); |
| 6437 | ASSERT_TRUE(response->headers); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6438 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6439 | |
| 6440 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6441 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6442 | EXPECT_EQ("1234567890", response_data); |
| 6443 | } |
| 6444 | |
| 6445 | // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6446 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectSpdy) { |
| 6447 | SpdyTestUtil spdy_util_wrapped; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 6448 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6449 | HttpRequestInfo request; |
| 6450 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6451 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6452 | request.traffic_annotation = |
| 6453 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6454 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6455 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6456 | session_deps_.proxy_resolution_service = |
| 6457 | ConfiguredProxyResolutionService::CreateFixed( |
| 6458 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6459 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6460 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6461 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6462 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6463 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6464 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6465 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6466 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6467 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6468 | HostPortPair("www.example.org", 443))); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6469 | // fetch https://www.example.org/ via SPDY |
| 6470 | const char kMyUrl[] = "https://www.example.org/"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6471 | spdy::SpdySerializedFrame get( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 6472 | spdy_util_wrapped.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6473 | spdy::SpdySerializedFrame wrapped_get( |
| 6474 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 6475 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6476 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6477 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6478 | spdy_util_wrapped.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6479 | spdy::SpdySerializedFrame wrapped_get_resp( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 6480 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6481 | spdy::SpdySerializedFrame body( |
| 6482 | spdy_util_wrapped.ConstructSpdyDataFrame(1, true)); |
| 6483 | spdy::SpdySerializedFrame wrapped_body( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 6484 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6485 | spdy::SpdySerializedFrame window_update_get_resp( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6486 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6487 | spdy::SpdySerializedFrame window_update_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6488 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6489 | |
| 6490 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6491 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_get, 2), |
| 6492 | CreateMockWrite(window_update_get_resp, 6), |
| 6493 | CreateMockWrite(window_update_body, 7), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6494 | }; |
| 6495 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6496 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6497 | CreateMockRead(conn_resp, 1, ASYNC), |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6498 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6499 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 6500 | CreateMockRead(wrapped_body, 5, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 6501 | MockRead(ASYNC, 0, 8), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6502 | }; |
| 6503 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6504 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6505 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6506 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6507 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6508 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6509 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6510 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6511 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6512 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6513 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6514 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6515 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6516 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6517 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6518 | |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6519 | // Allow the SpdyProxyClientSocket's write callback to complete. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 6520 | base::RunLoop().RunUntilIdle(); |
rch | 3232084 | 2015-05-16 15:57:09 | [diff] [blame] | 6521 | // Now allow the read of the response to complete. |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 6522 | spdy_data.Resume(); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6523 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6524 | EXPECT_THAT(rv, IsOk()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6525 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6526 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6527 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6528 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6529 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6530 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6531 | ASSERT_TRUE(response); |
| 6532 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 6533 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6534 | |
| 6535 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6536 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 6537 | EXPECT_EQ(kUploadData, response_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | // Test a SPDY CONNECT failure through an HTTPS Proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6541 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectFailure) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6542 | HttpRequestInfo request; |
| 6543 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6544 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6545 | request.traffic_annotation = |
| 6546 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6547 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6548 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6549 | session_deps_.proxy_resolution_service = |
| 6550 | ConfiguredProxyResolutionService::CreateFixed( |
| 6551 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6552 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6553 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6554 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6555 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6556 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6557 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6558 | // CONNECT to www.example.org:443 via SPDY |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6559 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6560 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6561 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6562 | spdy::SpdySerializedFrame get( |
| 6563 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6564 | |
| 6565 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6566 | CreateMockWrite(connect, 0), CreateMockWrite(get, 2), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6567 | }; |
| 6568 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6569 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError(1)); |
| 6570 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6571 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6572 | CreateMockRead(resp, 1, ASYNC), MockRead(ASYNC, 0, 3), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6573 | }; |
| 6574 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6575 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6576 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6577 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6578 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6579 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6580 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 6581 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6582 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6583 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6584 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6585 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6586 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6587 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6588 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6589 | |
| 6590 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6591 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6592 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 6593 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 6594 | } |
| 6595 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6596 | // Test the case where a proxied H2 session doesn't exist when an auth challenge |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6597 | // is observed, but does exist by the time auth credentials are provided. In |
| 6598 | // this case, auth and SSL are fully negotated on the second request, but then |
| 6599 | // the socket is discarded to use the shared session. |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6600 | TEST_F(HttpNetworkTransactionTest, ProxiedH2SessionAppearsDuringAuth) { |
| 6601 | ProxyConfig proxy_config; |
| 6602 | proxy_config.set_auto_detect(true); |
| 6603 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 6604 | |
| 6605 | CapturingProxyResolver capturing_proxy_resolver; |
| 6606 | capturing_proxy_resolver.set_proxy_server( |
| 6607 | ProxyServer(ProxyServer::SCHEME_HTTP, HostPortPair("myproxy", 70))); |
| 6608 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6609 | std::make_unique<ConfiguredProxyResolutionService>( |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6610 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 6611 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 6612 | std::make_unique<CapturingProxyResolverFactory>( |
| 6613 | &capturing_proxy_resolver), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 6614 | nullptr, /*quick_check_enabled=*/true); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6615 | |
| 6616 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 6617 | |
| 6618 | const char kMyUrl[] = "https://www.example.org/"; |
| 6619 | spdy::SpdySerializedFrame get(spdy_util_.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 6620 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6621 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6622 | spdy::SpdySerializedFrame body(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 6623 | |
| 6624 | spdy_util_.UpdateWithStreamDestruction(1); |
| 6625 | spdy::SpdySerializedFrame get2( |
| 6626 | spdy_util_.ConstructSpdyGet(kMyUrl, 3, LOWEST)); |
| 6627 | spdy::SpdySerializedFrame get_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6628 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6629 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
| 6630 | |
| 6631 | MockWrite auth_challenge_writes[] = { |
| 6632 | MockWrite(ASYNC, 0, |
| 6633 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6634 | "Host: www.example.org:443\r\n" |
| 6635 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6636 | MockWrite(ASYNC, 2, |
| 6637 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6638 | "Host: www.example.org:443\r\n" |
| 6639 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6640 | }; |
| 6641 | |
| 6642 | MockRead auth_challenge_reads[] = { |
| 6643 | MockRead(ASYNC, 1, |
| 6644 | "HTTP/1.1 407 Authentication Required\r\n" |
| 6645 | "Content-Length: 0\r\n" |
| 6646 | "Proxy-Connection: close\r\n" |
| 6647 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"), |
| 6648 | }; |
| 6649 | |
| 6650 | MockWrite spdy_writes[] = { |
| 6651 | MockWrite(ASYNC, 0, |
| 6652 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6653 | "Host: www.example.org:443\r\n" |
| 6654 | "Proxy-Connection: keep-alive\r\n" |
| 6655 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6656 | CreateMockWrite(get, 2), |
| 6657 | CreateMockWrite(get2, 5), |
| 6658 | }; |
| 6659 | |
| 6660 | MockRead spdy_reads[] = { |
| 6661 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n\r\n"), |
| 6662 | CreateMockRead(get_resp, 3, ASYNC), |
| 6663 | CreateMockRead(body, 4, ASYNC), |
| 6664 | CreateMockRead(get_resp2, 6, ASYNC), |
| 6665 | CreateMockRead(body2, 7, ASYNC), |
| 6666 | |
| 6667 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 8), |
| 6668 | }; |
| 6669 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6670 | MockWrite auth_response_writes_discarded_socket[] = { |
| 6671 | MockWrite(ASYNC, 0, |
| 6672 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 6673 | "Host: www.example.org:443\r\n" |
| 6674 | "Proxy-Connection: keep-alive\r\n" |
| 6675 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6676 | }; |
| 6677 | |
| 6678 | MockRead auth_response_reads_discarded_socket[] = { |
| 6679 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\n\r\n"), |
| 6680 | }; |
| 6681 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6682 | SequencedSocketData auth_challenge1(auth_challenge_reads, |
| 6683 | auth_challenge_writes); |
| 6684 | session_deps_.socket_factory->AddSocketDataProvider(&auth_challenge1); |
| 6685 | |
| 6686 | SequencedSocketData auth_challenge2(auth_challenge_reads, |
| 6687 | auth_challenge_writes); |
| 6688 | session_deps_.socket_factory->AddSocketDataProvider(&auth_challenge2); |
| 6689 | |
| 6690 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
| 6691 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 6692 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6693 | SequencedSocketData auth_response_discarded_socket( |
| 6694 | auth_response_reads_discarded_socket, |
| 6695 | auth_response_writes_discarded_socket); |
| 6696 | session_deps_.socket_factory->AddSocketDataProvider( |
| 6697 | &auth_response_discarded_socket); |
| 6698 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6699 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 6700 | ssl.next_proto = kProtoHTTP2; |
| 6701 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 6702 | |
Matt Menke | 5062be2 | 2019-05-01 17:50:24 | [diff] [blame] | 6703 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 6704 | ssl2.next_proto = kProtoHTTP2; |
| 6705 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 6706 | |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6707 | TestCompletionCallback callback; |
| 6708 | std::string response_data; |
| 6709 | |
| 6710 | // Run first request until an auth challenge is observed. |
| 6711 | HttpRequestInfo request1; |
| 6712 | request1.method = "GET"; |
| 6713 | request1.url = GURL(kMyUrl); |
| 6714 | request1.traffic_annotation = |
| 6715 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6716 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 6717 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 6718 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6719 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 6720 | ASSERT_TRUE(response); |
| 6721 | ASSERT_TRUE(response->headers); |
| 6722 | EXPECT_EQ(407, response->headers->response_code()); |
| 6723 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6724 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6725 | |
| 6726 | // Run second request until an auth challenge is observed. |
| 6727 | HttpRequestInfo request2; |
| 6728 | request2.method = "GET"; |
| 6729 | request2.url = GURL(kMyUrl); |
| 6730 | request2.traffic_annotation = |
| 6731 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 6732 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 6733 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
| 6734 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6735 | response = trans2.GetResponseInfo(); |
| 6736 | ASSERT_TRUE(response); |
| 6737 | ASSERT_TRUE(response->headers); |
| 6738 | EXPECT_EQ(407, response->headers->response_code()); |
| 6739 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 6740 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
Matt Menke | cb2cd098 | 2018-12-19 17:54:04 | [diff] [blame] | 6741 | |
| 6742 | // Now provide credentials for the first request, and wait for it to complete. |
| 6743 | rv = trans1.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 6744 | rv = callback.GetResult(rv); |
| 6745 | EXPECT_THAT(rv, IsOk()); |
| 6746 | response = trans1.GetResponseInfo(); |
| 6747 | ASSERT_TRUE(response); |
| 6748 | ASSERT_TRUE(response->headers); |
| 6749 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 6750 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 6751 | EXPECT_EQ(kUploadData, response_data); |
| 6752 | |
| 6753 | // Now provide credentials for the second request. It should notice the |
| 6754 | // existing session, and reuse it. |
| 6755 | rv = trans2.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
| 6756 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 6757 | response = trans2.GetResponseInfo(); |
| 6758 | ASSERT_TRUE(response); |
| 6759 | ASSERT_TRUE(response->headers); |
| 6760 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 6761 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 6762 | EXPECT_EQ(kUploadData, response_data); |
| 6763 | } |
| 6764 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6765 | // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY |
| 6766 | // HTTPS Proxy to different servers. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6767 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6768 | HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsTwoServers) { |
| 6769 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6770 | session_deps_.proxy_resolution_service = |
| 6771 | ConfiguredProxyResolutionService::CreateFixed( |
| 6772 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6773 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6774 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6775 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6776 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6777 | |
| 6778 | HttpRequestInfo request1; |
| 6779 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6780 | request1.url = GURL("https://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6781 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6782 | request1.traffic_annotation = |
| 6783 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6784 | |
| 6785 | HttpRequestInfo request2; |
| 6786 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6787 | request2.url = GURL("https://mail.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6788 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6789 | request2.traffic_annotation = |
| 6790 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6791 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6792 | // CONNECT to www.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6793 | spdy::SpdySerializedFrame connect1(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6794 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6795 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6796 | spdy::SpdySerializedFrame conn_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6797 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6798 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6799 | // Fetch https://www.example.org/ via HTTP. |
| 6800 | const char get1[] = |
| 6801 | "GET / HTTP/1.1\r\n" |
| 6802 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6803 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6804 | spdy::SpdySerializedFrame wrapped_get1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6805 | spdy_util_.ConstructSpdyDataFrame(1, get1, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6806 | const char resp1[] = "HTTP/1.1 200 OK\r\n" |
| 6807 | "Content-Length: 1\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6808 | spdy::SpdySerializedFrame wrapped_get_resp1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6809 | spdy_util_.ConstructSpdyDataFrame(1, resp1, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6810 | spdy::SpdySerializedFrame wrapped_body1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6811 | spdy_util_.ConstructSpdyDataFrame(1, "1", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6812 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6813 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1.size())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6814 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6815 | // CONNECT to mail.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6816 | spdy::SpdyHeaderBlock connect2_block; |
| 6817 | connect2_block[spdy::kHttp2MethodHeader] = "CONNECT"; |
| 6818 | connect2_block[spdy::kHttp2AuthorityHeader] = "mail.example.org:443"; |
| 6819 | spdy::SpdySerializedFrame connect2(spdy_util_.ConstructSpdyHeaders( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6820 | 3, std::move(connect2_block), HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6821 | false)); |
[email protected] | 601e03f1 | 2014-04-06 16:26:39 | [diff] [blame] | 6822 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6823 | spdy::SpdySerializedFrame conn_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6824 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6825 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6826 | // Fetch https://mail.example.org/ via HTTP. |
| 6827 | const char get2[] = |
| 6828 | "GET / HTTP/1.1\r\n" |
| 6829 | "Host: mail.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6830 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6831 | spdy::SpdySerializedFrame wrapped_get2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6832 | spdy_util_.ConstructSpdyDataFrame(3, get2, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6833 | const char resp2[] = "HTTP/1.1 200 OK\r\n" |
| 6834 | "Content-Length: 2\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6835 | spdy::SpdySerializedFrame wrapped_get_resp2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6836 | spdy_util_.ConstructSpdyDataFrame(3, resp2, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6837 | spdy::SpdySerializedFrame wrapped_body2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6838 | spdy_util_.ConstructSpdyDataFrame(3, "22", false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6839 | |
| 6840 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6841 | CreateMockWrite(connect1, 0), CreateMockWrite(wrapped_get1, 2), |
| 6842 | CreateMockWrite(connect2, 5), CreateMockWrite(wrapped_get2, 7), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6843 | }; |
| 6844 | |
| 6845 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6846 | CreateMockRead(conn_resp1, 1, ASYNC), |
| 6847 | CreateMockRead(wrapped_get_resp1, 3, ASYNC), |
| 6848 | CreateMockRead(wrapped_body1, 4, ASYNC), |
| 6849 | CreateMockRead(conn_resp2, 6, ASYNC), |
| 6850 | CreateMockRead(wrapped_get_resp2, 8, ASYNC), |
| 6851 | CreateMockRead(wrapped_body2, 9, ASYNC), |
| 6852 | MockRead(ASYNC, 0, 10), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6853 | }; |
| 6854 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6855 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6856 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6857 | |
| 6858 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6859 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6860 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6861 | SSLSocketDataProvider ssl2(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6862 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6863 | SSLSocketDataProvider ssl3(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6864 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6865 | |
| 6866 | TestCompletionCallback callback; |
| 6867 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6868 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6869 | int rv = trans.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6870 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6871 | |
| 6872 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6873 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6874 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6875 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6876 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 6877 | ASSERT_TRUE(response); |
| 6878 | ASSERT_TRUE(response->headers); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6879 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6880 | |
| 6881 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 6882 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6883 | rv = trans.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6884 | EXPECT_EQ(1, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6885 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6886 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6887 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6888 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6889 | |
| 6890 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6891 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6892 | // Even though the SPDY connection is reused, a new tunnelled connection has |
| 6893 | // to be created, so the socket's load timing looks like a fresh connection. |
| 6894 | TestLoadTimingNotReused(load_timing_info2, CONNECT_TIMING_HAS_SSL_TIMES); |
| 6895 | |
| 6896 | // The requests should have different IDs, since they each are using their own |
| 6897 | // separate stream. |
| 6898 | EXPECT_NE(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 6899 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 6900 | rv = trans2.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6901 | EXPECT_EQ(2, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | // Test load timing in the case of two HTTPS (non-SPDY) requests through a SPDY |
| 6905 | // HTTPS Proxy to the same server. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 6906 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6907 | HttpsProxySpdyConnectHttpsLoadTimingTwoRequestsSameServer) { |
| 6908 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 6909 | session_deps_.proxy_resolution_service = |
| 6910 | ConfiguredProxyResolutionService::CreateFixed( |
| 6911 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 6912 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 6913 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 6914 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6915 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6916 | |
| 6917 | HttpRequestInfo request1; |
| 6918 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6919 | request1.url = GURL("https://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6920 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6921 | request1.traffic_annotation = |
| 6922 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6923 | |
| 6924 | HttpRequestInfo request2; |
| 6925 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6926 | request2.url = GURL("https://www.example.org/2"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6927 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 6928 | request2.traffic_annotation = |
| 6929 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6930 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6931 | // CONNECT to www.example.org:443 via SPDY. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6932 | spdy::SpdySerializedFrame connect1(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 6933 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 6934 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6935 | spdy::SpdySerializedFrame conn_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6936 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6937 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6938 | // Fetch https://www.example.org/ via HTTP. |
| 6939 | const char get1[] = |
| 6940 | "GET / HTTP/1.1\r\n" |
| 6941 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6942 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6943 | spdy::SpdySerializedFrame wrapped_get1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6944 | spdy_util_.ConstructSpdyDataFrame(1, get1, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6945 | const char resp1[] = "HTTP/1.1 200 OK\r\n" |
| 6946 | "Content-Length: 1\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6947 | spdy::SpdySerializedFrame wrapped_get_resp1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6948 | spdy_util_.ConstructSpdyDataFrame(1, resp1, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6949 | spdy::SpdySerializedFrame wrapped_body1( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6950 | spdy_util_.ConstructSpdyDataFrame(1, "1", false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6951 | spdy::SpdySerializedFrame window_update( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6952 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp1.size())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6953 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 6954 | // Fetch https://www.example.org/2 via HTTP. |
| 6955 | const char get2[] = |
| 6956 | "GET /2 HTTP/1.1\r\n" |
| 6957 | "Host: www.example.org\r\n" |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6958 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6959 | spdy::SpdySerializedFrame wrapped_get2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6960 | spdy_util_.ConstructSpdyDataFrame(1, get2, false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6961 | const char resp2[] = "HTTP/1.1 200 OK\r\n" |
| 6962 | "Content-Length: 2\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6963 | spdy::SpdySerializedFrame wrapped_get_resp2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6964 | spdy_util_.ConstructSpdyDataFrame(1, resp2, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 6965 | spdy::SpdySerializedFrame wrapped_body2( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 6966 | spdy_util_.ConstructSpdyDataFrame(1, "22", false)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6967 | |
| 6968 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6969 | CreateMockWrite(connect1, 0), CreateMockWrite(wrapped_get1, 2), |
| 6970 | CreateMockWrite(wrapped_get2, 5), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6971 | }; |
| 6972 | |
| 6973 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6974 | CreateMockRead(conn_resp1, 1, ASYNC), |
| 6975 | CreateMockRead(wrapped_get_resp1, 3, ASYNC), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6976 | CreateMockRead(wrapped_body1, 4, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6977 | CreateMockRead(wrapped_get_resp2, 6, ASYNC), |
bnc | eb9aa711 | 2017-01-05 01:03:46 | [diff] [blame] | 6978 | CreateMockRead(wrapped_body2, 7, SYNCHRONOUS), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 6979 | MockRead(ASYNC, 0, 8), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6980 | }; |
| 6981 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 6982 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6983 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6984 | |
| 6985 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 6986 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6987 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6988 | SSLSocketDataProvider ssl2(ASYNC, OK); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 6989 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6990 | |
| 6991 | TestCompletionCallback callback; |
| 6992 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 6993 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6994 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6995 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6996 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 6997 | |
| 6998 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 6999 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7000 | |
| 7001 | LoadTimingInfo load_timing_info; |
| 7002 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 7003 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_SSL_TIMES); |
| 7004 | |
| 7005 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7006 | ASSERT_TRUE(response); |
| 7007 | ASSERT_TRUE(response->headers); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7008 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7009 | |
| 7010 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 7011 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7012 | EXPECT_EQ(1, trans->Read(buf.get(), 256, callback.callback())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7013 | trans.reset(); |
| 7014 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 7015 | auto trans2 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 7016 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7017 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7018 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7019 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7020 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7021 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7022 | |
| 7023 | LoadTimingInfo load_timing_info2; |
| 7024 | EXPECT_TRUE(trans2->GetLoadTimingInfo(&load_timing_info2)); |
| 7025 | TestLoadTimingReused(load_timing_info2); |
| 7026 | |
| 7027 | // The requests should have the same ID. |
| 7028 | EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 7029 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7030 | EXPECT_EQ(2, trans2->Read(buf.get(), 256, callback.callback())); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | // Test load timing in the case of of two HTTP requests through a SPDY HTTPS |
| 7034 | // Proxy to different servers. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7035 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyLoadTimingTwoHttpRequests) { |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7036 | // Configure against https proxy server "proxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7037 | session_deps_.proxy_resolution_service = |
| 7038 | ConfiguredProxyResolutionService::CreateFixed( |
| 7039 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 7040 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7041 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7042 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7043 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7044 | |
| 7045 | HttpRequestInfo request1; |
| 7046 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7047 | request1.url = GURL("http://www.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7048 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7049 | request1.traffic_annotation = |
| 7050 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7051 | |
| 7052 | HttpRequestInfo request2; |
| 7053 | request2.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7054 | request2.url = GURL("http://mail.example.org/"); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7055 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7056 | request2.traffic_annotation = |
| 7057 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7058 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7059 | // http://www.example.org/ |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7060 | spdy::SpdyHeaderBlock headers( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7061 | spdy_util_.ConstructGetHeaderBlockForProxy("http://www.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7062 | spdy::SpdySerializedFrame get1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 7063 | spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7064 | spdy::SpdySerializedFrame get_resp1( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7065 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7066 | spdy::SpdySerializedFrame body1( |
| 7067 | spdy_util_.ConstructSpdyDataFrame(1, "1", true)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 7068 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7069 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7070 | // http://mail.example.org/ |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7071 | spdy::SpdyHeaderBlock headers2( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7072 | spdy_util_.ConstructGetHeaderBlockForProxy("http://mail.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7073 | spdy::SpdySerializedFrame get2( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 7074 | spdy_util_.ConstructSpdyHeaders(3, std::move(headers2), LOWEST, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7075 | spdy::SpdySerializedFrame get_resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7076 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 7077 | spdy::SpdySerializedFrame body2( |
| 7078 | spdy_util_.ConstructSpdyDataFrame(3, "22", true)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7079 | |
| 7080 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 7081 | CreateMockWrite(get1, 0), CreateMockWrite(get2, 3), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7082 | }; |
| 7083 | |
| 7084 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 7085 | CreateMockRead(get_resp1, 1, ASYNC), |
| 7086 | CreateMockRead(body1, 2, ASYNC), |
| 7087 | CreateMockRead(get_resp2, 4, ASYNC), |
| 7088 | CreateMockRead(body2, 5, ASYNC), |
| 7089 | MockRead(ASYNC, 0, 6), |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7090 | }; |
| 7091 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7092 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7093 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7094 | |
| 7095 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 7096 | ssl.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7097 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7098 | |
| 7099 | TestCompletionCallback callback; |
| 7100 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 7101 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 7102 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7103 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7104 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7105 | |
| 7106 | LoadTimingInfo load_timing_info; |
| 7107 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 7108 | TestLoadTimingNotReused(load_timing_info, |
| 7109 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 7110 | |
| 7111 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7112 | ASSERT_TRUE(response); |
| 7113 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 7114 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7115 | |
| 7116 | std::string response_data; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 7117 | scoped_refptr<IOBuffer> buf = base::MakeRefCounted<IOBuffer>(256); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7118 | rv = trans->Read(buf.get(), 256, callback.callback()); |
| 7119 | EXPECT_EQ(1, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7120 | // Delete the first request, so the second one can reuse the socket. |
| 7121 | trans.reset(); |
| 7122 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7123 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7124 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7125 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7126 | |
| 7127 | LoadTimingInfo load_timing_info2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7128 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7129 | TestLoadTimingReused(load_timing_info2); |
| 7130 | |
| 7131 | // The requests should have the same ID. |
| 7132 | EXPECT_EQ(load_timing_info.socket_log_id, load_timing_info2.socket_log_id); |
| 7133 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7134 | rv = trans2.Read(buf.get(), 256, callback.callback()); |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 7135 | EXPECT_EQ(2, callback.GetResult(rv)); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 7136 | } |
| 7137 | |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7138 | // Test that an HTTP/2 CONNECT through an HTTPS Proxy to a HTTP/2 server and a |
| 7139 | // direct (non-proxied) request to the proxy server are not pooled, as that |
| 7140 | // would break socket pool isolation. |
| 7141 | TEST_F(HttpNetworkTransactionTest, SpdyProxyIsolation1) { |
| 7142 | ProxyConfig proxy_config; |
| 7143 | proxy_config.set_auto_detect(true); |
| 7144 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 7145 | |
| 7146 | CapturingProxyResolver capturing_proxy_resolver; |
| 7147 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7148 | std::make_unique<ConfiguredProxyResolutionService>( |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7149 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 7150 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 7151 | std::make_unique<CapturingProxyResolverFactory>( |
| 7152 | &capturing_proxy_resolver), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 7153 | nullptr, /*quick_check_enabled=*/true); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7154 | |
| 7155 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 7156 | |
| 7157 | SpdyTestUtil spdy_util1; |
| 7158 | // CONNECT to www.example.org:443 via HTTP/2. |
| 7159 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 7160 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 7161 | HostPortPair("www.example.org", 443))); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7162 | // fetch https://www.example.org/ via HTTP/2. |
| 7163 | const char kMyUrl[] = "https://www.example.org/"; |
| 7164 | spdy::SpdySerializedFrame get(spdy_util1.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 7165 | spdy::SpdySerializedFrame wrapped_get( |
| 7166 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 7167 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7168 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7169 | spdy::SpdySerializedFrame get_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 7170 | spdy_util1.ConstructSpdyGetReply(nullptr, 0, 1)); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7171 | spdy::SpdySerializedFrame wrapped_get_resp( |
| 7172 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
| 7173 | spdy::SpdySerializedFrame body(spdy_util1.ConstructSpdyDataFrame(1, true)); |
| 7174 | spdy::SpdySerializedFrame wrapped_body( |
| 7175 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
| 7176 | spdy::SpdySerializedFrame window_update_get_resp( |
| 7177 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
| 7178 | spdy::SpdySerializedFrame window_update_body( |
| 7179 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
| 7180 | |
| 7181 | MockWrite spdy_writes1[] = { |
| 7182 | CreateMockWrite(connect, 0), |
| 7183 | CreateMockWrite(wrapped_get, 2), |
| 7184 | CreateMockWrite(window_update_get_resp, 6), |
| 7185 | CreateMockWrite(window_update_body, 7), |
| 7186 | }; |
| 7187 | |
| 7188 | MockRead spdy_reads1[] = { |
| 7189 | CreateMockRead(conn_resp, 1, ASYNC), |
| 7190 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 7191 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 7192 | CreateMockRead(wrapped_body, 5, ASYNC), |
| 7193 | MockRead(ASYNC, 0, 8), |
| 7194 | }; |
| 7195 | |
| 7196 | SequencedSocketData spdy_data1(spdy_reads1, spdy_writes1); |
| 7197 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data1); |
| 7198 | |
| 7199 | // Fetch https://proxy:70/ via HTTP/2. Needs a new SpdyTestUtil, since it uses |
| 7200 | // a new pipe. |
| 7201 | SpdyTestUtil spdy_util2; |
| 7202 | spdy::SpdySerializedFrame req( |
| 7203 | spdy_util2.ConstructSpdyGet("https://proxy:70/", 1, LOWEST)); |
| 7204 | MockWrite spdy_writes2[] = {CreateMockWrite(req, 0)}; |
| 7205 | |
| 7206 | spdy::SpdySerializedFrame resp( |
| 7207 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7208 | spdy::SpdySerializedFrame data(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 7209 | MockRead spdy_reads2[] = { |
| 7210 | CreateMockRead(resp, 1), |
| 7211 | CreateMockRead(data, 2), |
| 7212 | MockRead(ASYNC, 0, 3), |
| 7213 | }; |
| 7214 | SequencedSocketData spdy_data2(spdy_reads2, spdy_writes2); |
| 7215 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data2); |
| 7216 | |
| 7217 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 7218 | ssl.next_proto = kProtoHTTP2; |
| 7219 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 7220 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7221 | ssl2.next_proto = kProtoHTTP2; |
| 7222 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7223 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 7224 | ssl3.next_proto = kProtoHTTP2; |
| 7225 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 7226 | |
| 7227 | TestCompletionCallback callback; |
| 7228 | std::string response_data; |
| 7229 | |
| 7230 | // Make a request using proxy:70 as a HTTP/2 proxy. |
| 7231 | capturing_proxy_resolver.set_proxy_server( |
| 7232 | ProxyServer(ProxyServer::SCHEME_HTTPS, HostPortPair("proxy", 70))); |
| 7233 | HttpRequestInfo request1; |
| 7234 | request1.method = "GET"; |
| 7235 | request1.url = GURL("https://www.example.org/"); |
| 7236 | request1.traffic_annotation = |
| 7237 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7238 | |
| 7239 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 7240 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 7241 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 7242 | |
| 7243 | // Allow the SpdyProxyClientSocket's write callback to complete. |
| 7244 | base::RunLoop().RunUntilIdle(); |
| 7245 | // Now allow the read of the response to complete. |
| 7246 | spdy_data1.Resume(); |
| 7247 | rv = callback.WaitForResult(); |
| 7248 | EXPECT_THAT(rv, IsOk()); |
| 7249 | |
| 7250 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 7251 | ASSERT_TRUE(response); |
| 7252 | ASSERT_TRUE(response->headers); |
| 7253 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 7254 | |
| 7255 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 7256 | EXPECT_EQ(kUploadData, response_data); |
| 7257 | RunUntilIdle(); |
| 7258 | |
| 7259 | // Make a direct HTTP/2 request to proxy:70. |
| 7260 | capturing_proxy_resolver.set_proxy_server(ProxyServer::Direct()); |
| 7261 | HttpRequestInfo request2; |
| 7262 | request2.method = "GET"; |
| 7263 | request2.url = GURL("https://proxy:70/"); |
| 7264 | request2.traffic_annotation = |
| 7265 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7266 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 7267 | EXPECT_THAT(callback.GetResult(trans2.Start(&request2, callback.callback(), |
| 7268 | NetLogWithSource())), |
| 7269 | IsOk()); |
| 7270 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 7271 | } |
| 7272 | |
| 7273 | // Same as above, but reverse request order, since the code to check for an |
| 7274 | // existing session is different for tunnels and direct connections. |
| 7275 | TEST_F(HttpNetworkTransactionTest, SpdyProxyIsolation2) { |
| 7276 | // Configure against https proxy server "myproxy:80". |
| 7277 | ProxyConfig proxy_config; |
| 7278 | proxy_config.set_auto_detect(true); |
| 7279 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 7280 | |
| 7281 | CapturingProxyResolver capturing_proxy_resolver; |
| 7282 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7283 | std::make_unique<ConfiguredProxyResolutionService>( |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7284 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 7285 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 7286 | std::make_unique<CapturingProxyResolverFactory>( |
| 7287 | &capturing_proxy_resolver), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 7288 | nullptr, /*quick_check_enabled=*/true); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7289 | |
| 7290 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 7291 | // Fetch https://proxy:70/ via HTTP/2. |
| 7292 | SpdyTestUtil spdy_util1; |
| 7293 | spdy::SpdySerializedFrame req( |
| 7294 | spdy_util1.ConstructSpdyGet("https://proxy:70/", 1, LOWEST)); |
| 7295 | MockWrite spdy_writes1[] = {CreateMockWrite(req, 0)}; |
| 7296 | |
| 7297 | spdy::SpdySerializedFrame resp( |
| 7298 | spdy_util1.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7299 | spdy::SpdySerializedFrame data(spdy_util1.ConstructSpdyDataFrame(1, true)); |
| 7300 | MockRead spdy_reads1[] = { |
| 7301 | CreateMockRead(resp, 1), |
| 7302 | CreateMockRead(data, 2), |
| 7303 | MockRead(ASYNC, 0, 3), |
| 7304 | }; |
| 7305 | SequencedSocketData spdy_data1(spdy_reads1, spdy_writes1); |
| 7306 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data1); |
| 7307 | |
| 7308 | SpdyTestUtil spdy_util2; |
| 7309 | // CONNECT to www.example.org:443 via HTTP/2. |
| 7310 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 7311 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 7312 | HostPortPair("www.example.org", 443))); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 7313 | // fetch https://www.example.org/ via HTTP/2. |
| 7314 | const char kMyUrl[] = "https://www.example.org/"; |
| 7315 | spdy::SpdySerializedFrame get(spdy_util2.ConstructSpdyGet(kMyUrl, 1, LOWEST)); |
| 7316 | spdy::SpdySerializedFrame wrapped_get( |
| 7317 | spdy_util_.ConstructWrappedSpdyFrame(get, 1)); |
| 7318 | spdy::SpdySerializedFrame conn_resp( |
| 7319 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7320 | spdy::SpdySerializedFrame get_resp( |
| 7321 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 7322 | spdy::SpdySerializedFrame wrapped_get_resp( |
| 7323 | spdy_util_.ConstructWrappedSpdyFrame(get_resp, 1)); |
| 7324 | spdy::SpdySerializedFrame body(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 7325 | spdy::SpdySerializedFrame wrapped_body( |
| 7326 | spdy_util_.ConstructWrappedSpdyFrame(body, 1)); |
| 7327 | spdy::SpdySerializedFrame window_update_get_resp( |
| 7328 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_get_resp.size())); |
| 7329 | spdy::SpdySerializedFrame window_update_body( |
| 7330 | spdy_util_.ConstructSpdyWindowUpdate(1, wrapped_body.size())); |
| 7331 | |
| 7332 | MockWrite spdy_writes2[] = { |
| 7333 | CreateMockWrite(connect, 0), |
| 7334 | CreateMockWrite(wrapped_get, 2), |
| 7335 | CreateMockWrite(window_update_get_resp, 6), |
| 7336 | CreateMockWrite(window_update_body, 7), |
| 7337 | }; |
| 7338 | |
| 7339 | MockRead spdy_reads2[] = { |
| 7340 | CreateMockRead(conn_resp, 1, ASYNC), |
| 7341 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 7342 | CreateMockRead(wrapped_get_resp, 4, ASYNC), |
| 7343 | CreateMockRead(wrapped_body, 5, ASYNC), |
| 7344 | MockRead(ASYNC, 0, 8), |
| 7345 | }; |
| 7346 | |
| 7347 | SequencedSocketData spdy_data2(spdy_reads2, spdy_writes2); |
| 7348 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data2); |
| 7349 | |
| 7350 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 7351 | ssl.next_proto = kProtoHTTP2; |
| 7352 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 7353 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7354 | ssl2.next_proto = kProtoHTTP2; |
| 7355 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7356 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 7357 | ssl3.next_proto = kProtoHTTP2; |
| 7358 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 7359 | |
| 7360 | TestCompletionCallback callback; |
| 7361 | std::string response_data; |
| 7362 | |
| 7363 | // Make a direct HTTP/2 request to proxy:70. |
| 7364 | capturing_proxy_resolver.set_proxy_server(ProxyServer::Direct()); |
| 7365 | HttpRequestInfo request1; |
| 7366 | request1.method = "GET"; |
| 7367 | request1.url = GURL("https://proxy:70/"); |
| 7368 | request1.traffic_annotation = |
| 7369 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7370 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
| 7371 | EXPECT_THAT(callback.GetResult(trans1.Start(&request1, callback.callback(), |
| 7372 | NetLogWithSource())), |
| 7373 | IsOk()); |
| 7374 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 7375 | RunUntilIdle(); |
| 7376 | |
| 7377 | // Make a request using proxy:70 as a HTTP/2 proxy. |
| 7378 | capturing_proxy_resolver.set_proxy_server( |
| 7379 | ProxyServer(ProxyServer::SCHEME_HTTPS, HostPortPair("proxy", 70))); |
| 7380 | HttpRequestInfo request2; |
| 7381 | request2.method = "GET"; |
| 7382 | request2.url = GURL("https://www.example.org/"); |
| 7383 | request2.traffic_annotation = |
| 7384 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7385 | |
| 7386 | HttpNetworkTransaction trans2(LOWEST, session.get()); |
| 7387 | int rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
| 7388 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 7389 | |
| 7390 | // Allow the SpdyProxyClientSocket's write callback to complete. |
| 7391 | base::RunLoop().RunUntilIdle(); |
| 7392 | // Now allow the read of the response to complete. |
| 7393 | spdy_data2.Resume(); |
| 7394 | rv = callback.WaitForResult(); |
| 7395 | EXPECT_THAT(rv, IsOk()); |
| 7396 | |
| 7397 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
| 7398 | ASSERT_TRUE(response2); |
| 7399 | ASSERT_TRUE(response2->headers); |
| 7400 | EXPECT_EQ("HTTP/1.1 200", response2->headers->GetStatusLine()); |
| 7401 | |
| 7402 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 7403 | EXPECT_EQ(kUploadData, response_data); |
| 7404 | } |
| 7405 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7406 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7407 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7408 | HttpRequestInfo request; |
| 7409 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7410 | request.url = GURL("http://www.example.org/"); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7411 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 7412 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7413 | request.traffic_annotation = |
| 7414 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7415 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 7416 | // Configure against https proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7417 | session_deps_.proxy_resolution_service = |
| 7418 | ConfiguredProxyResolutionService::CreateFixed( |
| 7419 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 7420 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7421 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7422 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7423 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7424 | // Since we have proxy, should use full url |
| 7425 | MockWrite data_writes1[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7426 | MockWrite("GET http://www.example.org/ HTTP/1.1\r\n" |
| 7427 | "Host: www.example.org\r\n" |
| 7428 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7429 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7430 | // After calling trans.RestartWithAuth(), this is the request we should |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7431 | // be issuing -- the final header line contains the credentials. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7432 | MockWrite("GET http://www.example.org/ HTTP/1.1\r\n" |
| 7433 | "Host: www.example.org\r\n" |
| 7434 | "Proxy-Connection: keep-alive\r\n" |
| 7435 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7436 | }; |
| 7437 | |
| 7438 | // The proxy responds to the GET with a 407, using a persistent |
| 7439 | // connection. |
| 7440 | MockRead data_reads1[] = { |
| 7441 | // No credentials. |
| 7442 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 7443 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7444 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 7445 | MockRead("Content-Length: 0\r\n\r\n"), |
| 7446 | |
| 7447 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7448 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7449 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7450 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7451 | }; |
| 7452 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7453 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7454 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7455 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7456 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7457 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7458 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7459 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7460 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7461 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7462 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7463 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7464 | |
| 7465 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7466 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7467 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7468 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7469 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7470 | TestLoadTimingNotReused(load_timing_info, |
| 7471 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 7472 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7473 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7474 | ASSERT_TRUE(response); |
| 7475 | ASSERT_TRUE(response->headers); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7476 | EXPECT_EQ(407, response->headers->response_code()); |
| 7477 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7478 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 7479 | EXPECT_FALSE(response->did_use_http_auth); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7480 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7481 | TestCompletionCallback callback2; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7482 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7483 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7484 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7485 | |
| 7486 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7487 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7488 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7489 | load_timing_info = LoadTimingInfo(); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7490 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7491 | // Retrying with HTTP AUTH is considered to be reusing a socket. |
| 7492 | TestLoadTimingReused(load_timing_info); |
| 7493 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7494 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7495 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7496 | |
| 7497 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7498 | EXPECT_EQ(200, response->headers->response_code()); |
| 7499 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7500 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 7501 | EXPECT_TRUE(response->did_use_http_auth); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7502 | |
| 7503 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7504 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 7505 | } |
| 7506 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 7507 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7508 | const MockRead& status, int expected_status) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7509 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7510 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7511 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7512 | request.traffic_annotation = |
| 7513 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7514 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7515 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7516 | session_deps_.proxy_resolution_service = |
| 7517 | ConfiguredProxyResolutionService::CreateFixed( |
| 7518 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7519 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7520 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7521 | // Since we have proxy, should try to establish tunnel. |
| 7522 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 7523 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 7524 | "Host: www.example.org:443\r\n" |
| 7525 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7526 | }; |
| 7527 | |
| 7528 | MockRead data_reads[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 7529 | status, MockRead("Content-Length: 10\r\n\r\n"), |
| 7530 | // No response body because the test stops reading here. |
| 7531 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7532 | }; |
| 7533 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7534 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7535 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7536 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7537 | TestCompletionCallback callback; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7538 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7539 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7540 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7541 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7542 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7543 | |
| 7544 | rv = callback.WaitForResult(); |
| 7545 | EXPECT_EQ(expected_status, rv); |
| 7546 | } |
| 7547 | |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 7548 | void HttpNetworkTransactionTest::ConnectStatusHelper( |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 7549 | const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7550 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7551 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7552 | } |
| 7553 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7554 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7555 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 7556 | } |
| 7557 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7558 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7559 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 7560 | } |
| 7561 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7562 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7563 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 7564 | } |
| 7565 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7566 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7567 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 7568 | } |
| 7569 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7570 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7571 | ConnectStatusHelper( |
| 7572 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 7573 | } |
| 7574 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7575 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7576 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 7577 | } |
| 7578 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7579 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7580 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 7581 | } |
| 7582 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7583 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7584 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 7585 | } |
| 7586 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7587 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7588 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 7589 | } |
| 7590 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7591 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7592 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 7593 | } |
| 7594 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7595 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7596 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 7597 | } |
| 7598 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7599 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7600 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 7601 | } |
| 7602 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7603 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7604 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 7605 | } |
| 7606 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7607 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7608 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 7609 | } |
| 7610 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7611 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7612 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 7613 | } |
| 7614 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7615 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7616 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 7617 | } |
| 7618 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7619 | TEST_F(HttpNetworkTransactionTest, ConnectStatus308) { |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7620 | ConnectStatusHelper(MockRead("HTTP/1.1 308 Permanent Redirect\r\n")); |
| 7621 | } |
| 7622 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7623 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7624 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 7625 | } |
| 7626 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7627 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7628 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 7629 | } |
| 7630 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7631 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7632 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 7633 | } |
| 7634 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7635 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7636 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 7637 | } |
| 7638 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7639 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7640 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 7641 | } |
| 7642 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7643 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7644 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 7645 | } |
| 7646 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7647 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7648 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 7649 | } |
| 7650 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7651 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7652 | ConnectStatusHelperWithExpectedStatus( |
| 7653 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 7654 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7655 | } |
| 7656 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7657 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7658 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 7659 | } |
| 7660 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7661 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7662 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 7663 | } |
| 7664 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7665 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7666 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 7667 | } |
| 7668 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7669 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7670 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 7671 | } |
| 7672 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7673 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7674 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 7675 | } |
| 7676 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7677 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7678 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 7679 | } |
| 7680 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7681 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7682 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 7683 | } |
| 7684 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7685 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7686 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 7687 | } |
| 7688 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7689 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7690 | ConnectStatusHelper( |
| 7691 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 7692 | } |
| 7693 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7694 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7695 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 7696 | } |
| 7697 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7698 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7699 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 7700 | } |
| 7701 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7702 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7703 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 7704 | } |
| 7705 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7706 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7707 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 7708 | } |
| 7709 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7710 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7711 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 7712 | } |
| 7713 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7714 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7715 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 7716 | } |
| 7717 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7718 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 7719 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 7720 | } |
| 7721 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7722 | // Test the flow when both the proxy server AND origin server require |
| 7723 | // authentication. Again, this uses basic auth for both since that is |
| 7724 | // the simplest to mock. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 7725 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7726 | HttpRequestInfo request; |
| 7727 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7728 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7729 | request.traffic_annotation = |
| 7730 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7731 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7732 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 7733 | session_deps_.proxy_resolution_service = |
| 7734 | ConfiguredProxyResolutionService::CreateFixed( |
| 7735 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7736 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3fe8d2f8 | 2013-10-17 08:56:07 | [diff] [blame] | 7737 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7738 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7739 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 7740 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7741 | MockWrite( |
| 7742 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7743 | "Host: www.example.org\r\n" |
| 7744 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 7745 | }; |
| 7746 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7747 | MockRead data_reads1[] = { |
| 7748 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 7749 | // Give a couple authenticate options (only the middle one is actually |
| 7750 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 7751 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7752 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7753 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 7754 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7755 | // Large content-length -- won't matter, as connection will be reset. |
| 7756 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7757 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7758 | }; |
| 7759 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7760 | // After calling trans.RestartWithAuth() the first time, this is the |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7761 | // request we should be issuing -- the final header line contains the |
| 7762 | // proxy's credentials. |
| 7763 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7764 | MockWrite( |
| 7765 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7766 | "Host: www.example.org\r\n" |
| 7767 | "Proxy-Connection: keep-alive\r\n" |
| 7768 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7769 | }; |
| 7770 | |
| 7771 | // Now the proxy server lets the request pass through to origin server. |
| 7772 | // The origin server responds with a 401. |
| 7773 | MockRead data_reads2[] = { |
| 7774 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 7775 | // Note: We are using the same realm-name as the proxy server. This is |
| 7776 | // completely valid, as realms are unique across hosts. |
| 7777 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 7778 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7779 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7780 | MockRead(SYNCHRONOUS, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7781 | }; |
| 7782 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7783 | // After calling trans.RestartWithAuth() the second time, we should send |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7784 | // the credentials for both the proxy and origin server. |
| 7785 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 7786 | MockWrite( |
| 7787 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 7788 | "Host: www.example.org\r\n" |
| 7789 | "Proxy-Connection: keep-alive\r\n" |
| 7790 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 7791 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7792 | }; |
| 7793 | |
| 7794 | // Lastly we get the desired content. |
| 7795 | MockRead data_reads3[] = { |
| 7796 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 7797 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7798 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 7799 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7800 | }; |
| 7801 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7802 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 7803 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 7804 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7805 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 7806 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 7807 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7808 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7809 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7810 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7811 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7812 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7813 | |
| 7814 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7815 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7816 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7817 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7818 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7819 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7820 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7821 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7822 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7823 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7824 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7825 | |
| 7826 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7827 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7828 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7829 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7830 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7831 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7832 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7833 | TestCompletionCallback callback3; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7834 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7835 | rv = trans.RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 7836 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7837 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7838 | |
| 7839 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7840 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7841 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7842 | response = trans.GetResponseInfo(); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7843 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7844 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 7845 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 7846 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 7847 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 7848 | // can't hook into its internals to cause it to generate predictable NTLM |
| 7849 | // authorization headers. |
| 7850 | #if defined(NTLM_PORTABLE) |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7851 | // The NTLM authentication unit tests are based on known test data from the |
| 7852 | // [MS-NLMP] Specification [1]. These tests are primarily of the authentication |
| 7853 | // flow rather than the implementation of the NTLM protocol. See net/ntlm |
| 7854 | // for the implementation and testing of the protocol. |
| 7855 | // |
| 7856 | // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 7857 | |
| 7858 | // Enter the correct password and authenticate successfully. |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7859 | TEST_F(HttpNetworkTransactionTest, NTLMAuthV2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 7860 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7861 | request.method = "GET"; |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7862 | request.url = GURL("https://server/kids/login.aspx"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 7863 | request.traffic_annotation = |
| 7864 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 7865 | |
| 7866 | // Ensure load is not disrupted by flags which suppress behaviour specific |
| 7867 | // to other auth schemes. |
| 7868 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7869 | |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 7870 | HttpAuthNtlmMechanism::ScopedProcSetter proc_setter( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7871 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 7872 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7873 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7874 | // Generate the NTLM messages based on known test data. |
| 7875 | std::string negotiate_msg; |
| 7876 | std::string challenge_msg; |
| 7877 | std::string authenticate_msg; |
| 7878 | base::Base64Encode( |
| 7879 | base::StringPiece( |
| 7880 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7881 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7882 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7883 | base::Base64Encode( |
| 7884 | base::StringPiece( |
| 7885 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7886 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7887 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7888 | base::Base64Encode( |
| 7889 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 7890 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7891 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7892 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7893 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7894 | &authenticate_msg); |
| 7895 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7896 | MockWrite data_writes1[] = { |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7897 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 7898 | "Host: server\r\n" |
| 7899 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7900 | }; |
| 7901 | |
| 7902 | MockRead data_reads1[] = { |
| 7903 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 7904 | // Negotiate and NTLM are often requested together. However, we only want |
| 7905 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 7906 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7907 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 7908 | MockRead("Connection: close\r\n"), |
| 7909 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 7910 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7911 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7912 | }; |
| 7913 | |
| 7914 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7915 | // After restarting with a null identity, this is the |
| 7916 | // request we should be issuing -- the final header line contains a Type |
| 7917 | // 1 message. |
| 7918 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7919 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7920 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7921 | "Authorization: NTLM "), |
| 7922 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7923 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7924 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7925 | // (using correct credentials). The second request continues on the |
| 7926 | // same connection. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7927 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 7928 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7929 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7930 | "Authorization: NTLM "), |
| 7931 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7932 | }; |
| 7933 | |
| 7934 | MockRead data_reads2[] = { |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7935 | // The origin server responds with a Type 2 message. |
| 7936 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7937 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 7938 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7939 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 7940 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7941 | |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 7942 | // Lastly we get the desired content. |
| 7943 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7944 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 7945 | MockRead("Content-Length: 14\r\n\r\n"), MockRead("Please Login\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7946 | }; |
| 7947 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 7948 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 7949 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 7950 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 7951 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7952 | |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 7953 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 7954 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 7955 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 7956 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 7957 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7958 | TestCompletionCallback callback1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7959 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7960 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7961 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7962 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7963 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7964 | |
| 7965 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7966 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7967 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7968 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 7969 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7970 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7971 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7972 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7973 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7974 | TestCompletionCallback callback2; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7975 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 7976 | rv = trans.RestartWithAuth( |
| 7977 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 7978 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7979 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7980 | |
| 7981 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7982 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7983 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7984 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7985 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7986 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7987 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 7988 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 7989 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7990 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7991 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7992 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7993 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7994 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 7995 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 7996 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 7997 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 7998 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 7999 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8000 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8001 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 8002 | |
| 8003 | std::string response_data; |
| 8004 | rv = ReadTransaction(&trans, &response_data); |
| 8005 | EXPECT_THAT(rv, IsOk()); |
| 8006 | EXPECT_EQ("Please Login\r\n", response_data); |
| 8007 | |
| 8008 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 8009 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
| 8010 | EXPECT_TRUE(data2.AllReadDataConsumed()); |
| 8011 | EXPECT_TRUE(data2.AllWriteDataConsumed()); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 8012 | } |
| 8013 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8014 | // Enter a wrong password, and then the correct one. |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8015 | TEST_F(HttpNetworkTransactionTest, NTLMAuthV2WrongThenRightPassword) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 8016 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8017 | request.method = "GET"; |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8018 | request.url = GURL("https://server/kids/login.aspx"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8019 | request.traffic_annotation = |
| 8020 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8021 | |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 8022 | HttpAuthNtlmMechanism::ScopedProcSetter proc_setter( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8023 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8024 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8025 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8026 | // Generate the NTLM messages based on known test data. |
| 8027 | std::string negotiate_msg; |
| 8028 | std::string challenge_msg; |
| 8029 | std::string authenticate_msg; |
| 8030 | base::Base64Encode( |
| 8031 | base::StringPiece( |
| 8032 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8033 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8034 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8035 | base::Base64Encode( |
| 8036 | base::StringPiece( |
| 8037 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8038 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8039 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8040 | base::Base64Encode( |
| 8041 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8042 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8043 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8044 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8045 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8046 | &authenticate_msg); |
| 8047 | |
| 8048 | // The authenticate message when |kWrongPassword| is sent. |
| 8049 | std::string wrong_password_authenticate_msg( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8050 | "TlRMTVNTUAADAAAAGAAYAFgAAACKAIoAcAAAAAwADAD6AAAACAAIAAYBAAAQABAADgEAAAAA" |
| 8051 | "AABYAAAAA4IIAAAAAAAAAAAAAPknEYqtJQtusopDRSfYzAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 8052 | "AAAAAOtVz38osnFdRRggUQHUJ3EBAQAAAAAAAIALyP0A1NIBqqqqqqqqqqoAAAAAAgAMAEQA" |
| 8053 | "bwBtAGEAaQBuAAEADABTAGUAcgB2AGUAcgAGAAQAAgAAAAoAEAAAAAAAAAAAAAAAAAAAAAAA" |
| 8054 | "CQAWAEgAVABUAFAALwBzAGUAcgB2AGUAcgAAAAAAAAAAAEQAbwBtAGEAaQBuAFUAcwBlAHIA" |
| 8055 | "QwBPAE0AUABVAFQARQBSAA=="); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8056 | |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8057 | // Sanity check that it's the same length as the correct authenticate message |
| 8058 | // and that it's different. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8059 | ASSERT_EQ(authenticate_msg.length(), |
| 8060 | wrong_password_authenticate_msg.length()); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8061 | ASSERT_NE(authenticate_msg, wrong_password_authenticate_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8062 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8063 | MockWrite data_writes1[] = { |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8064 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 8065 | "Host: server\r\n" |
| 8066 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8067 | }; |
| 8068 | |
| 8069 | MockRead data_reads1[] = { |
| 8070 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 8071 | // Negotiate and NTLM are often requested together. However, we only want |
| 8072 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 8073 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8074 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 8075 | MockRead("Connection: close\r\n"), |
| 8076 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 8077 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8078 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8079 | }; |
| 8080 | |
| 8081 | MockWrite data_writes2[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8082 | // After restarting with a null identity, this is the |
| 8083 | // request we should be issuing -- the final header line contains a Type |
| 8084 | // 1 message. |
| 8085 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8086 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8087 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8088 | "Authorization: NTLM "), |
| 8089 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8090 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8091 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8092 | // (using incorrect credentials). The second request continues on the |
| 8093 | // same connection. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8094 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8095 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8096 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8097 | "Authorization: NTLM "), |
| 8098 | MockWrite(wrong_password_authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8099 | }; |
| 8100 | |
| 8101 | MockRead data_reads2[] = { |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8102 | // The origin server responds with a Type 2 message. |
| 8103 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8104 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8105 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
| 8106 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8107 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8108 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8109 | // Wrong password. |
| 8110 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8111 | MockRead("WWW-Authenticate: NTLM\r\n"), MockRead("Connection: close\r\n"), |
| 8112 | MockRead("Content-Length: 42\r\n"), |
| 8113 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8114 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8115 | }; |
| 8116 | |
| 8117 | MockWrite data_writes3[] = { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8118 | // After restarting with a null identity, this is the |
| 8119 | // request we should be issuing -- the final header line contains a Type |
| 8120 | // 1 message. |
| 8121 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8122 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8123 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8124 | "Authorization: NTLM "), |
| 8125 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8126 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8127 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8128 | // (the credentials for the origin server). The second request continues |
| 8129 | // on the same connection. |
| 8130 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8131 | "Host: server\r\n" |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8132 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8133 | "Authorization: NTLM "), |
| 8134 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8135 | }; |
| 8136 | |
| 8137 | MockRead data_reads3[] = { |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8138 | // The origin server responds with a Type 2 message. |
| 8139 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8140 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8141 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8142 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8143 | MockRead("You are not authorized to view this page\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8144 | |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8145 | // Lastly we get the desired content. |
| 8146 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8147 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 8148 | MockRead("Content-Length: 14\r\n\r\n"), MockRead("Please Login\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8149 | }; |
| 8150 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8151 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 8152 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 8153 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8154 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8155 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 8156 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8157 | |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8158 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 8159 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8160 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 8161 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 8162 | SSLSocketDataProvider ssl3(ASYNC, OK); |
| 8163 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl3); |
| 8164 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8165 | TestCompletionCallback callback1; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8166 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8167 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8168 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8169 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8170 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8171 | |
| 8172 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8173 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8174 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8175 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8176 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8177 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8178 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8179 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8180 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8181 | TestCompletionCallback callback2; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8182 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8183 | // Enter the wrong password. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8184 | rv = trans.RestartWithAuth( |
| 8185 | AuthCredentials(ntlm::test::kDomainUserCombined, kWrongPassword), |
| 8186 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8187 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8188 | |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8189 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8190 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8191 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8192 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8193 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8194 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8195 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8196 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8197 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8198 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8199 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8200 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8201 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8202 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8203 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8204 | TestCompletionCallback callback4; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8205 | |
| 8206 | // Now enter the right password. |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8207 | rv = trans.RestartWithAuth( |
| 8208 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8209 | callback4.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8210 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8211 | |
| 8212 | rv = callback4.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8213 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8214 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8215 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8216 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8217 | TestCompletionCallback callback5; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 8218 | |
| 8219 | // One more roundtrip |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8220 | rv = trans.RestartWithAuth(AuthCredentials(), callback5.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8221 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8222 | |
| 8223 | rv = callback5.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8224 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 8225 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8226 | response = trans.GetResponseInfo(); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8227 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8228 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 8229 | |
| 8230 | std::string response_data; |
| 8231 | rv = ReadTransaction(&trans, &response_data); |
| 8232 | EXPECT_THAT(rv, IsOk()); |
| 8233 | EXPECT_EQ("Please Login\r\n", response_data); |
| 8234 | |
| 8235 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 8236 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
| 8237 | EXPECT_TRUE(data2.AllReadDataConsumed()); |
| 8238 | EXPECT_TRUE(data2.AllWriteDataConsumed()); |
| 8239 | EXPECT_TRUE(data3.AllReadDataConsumed()); |
| 8240 | EXPECT_TRUE(data3.AllWriteDataConsumed()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8241 | } |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8242 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 8243 | // Server requests NTLM authentication, which is not supported over HTTP/2. |
| 8244 | // Subsequent request with authorization header should be sent over HTTP/1.1. |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8245 | TEST_F(HttpNetworkTransactionTest, NTLMOverHttp2) { |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 8246 | HttpAuthNtlmMechanism::ScopedProcSetter proc_setter( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8247 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8248 | |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8249 | const char* kUrl = "https://server/kids/login.aspx"; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8250 | |
| 8251 | HttpRequestInfo request; |
| 8252 | request.method = "GET"; |
| 8253 | request.url = GURL(kUrl); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8254 | request.traffic_annotation = |
| 8255 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8256 | |
| 8257 | // First request without credentials. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8258 | spdy::SpdyHeaderBlock request_headers0( |
| 8259 | spdy_util_.ConstructGetHeaderBlock(kUrl)); |
| 8260 | spdy::SpdySerializedFrame request0(spdy_util_.ConstructSpdyHeaders( |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8261 | 1, std::move(request_headers0), LOWEST, true)); |
| 8262 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8263 | spdy::SpdyHeaderBlock response_headers0; |
| 8264 | response_headers0[spdy::kHttp2StatusHeader] = "401"; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8265 | response_headers0["www-authenticate"] = "NTLM"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 8266 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyResponseHeaders( |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8267 | 1, std::move(response_headers0), true)); |
| 8268 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8269 | // Stream 1 is closed. |
| 8270 | spdy_util_.UpdateWithStreamDestruction(1); |
| 8271 | |
| 8272 | // Generate the NTLM messages based on known test data. |
| 8273 | std::string negotiate_msg; |
| 8274 | std::string challenge_msg; |
| 8275 | std::string authenticate_msg; |
| 8276 | base::Base64Encode( |
| 8277 | base::StringPiece( |
| 8278 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8279 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8280 | &negotiate_msg); |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8281 | base::Base64Encode( |
| 8282 | base::StringPiece( |
| 8283 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8284 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8285 | &challenge_msg); |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8286 | base::Base64Encode( |
| 8287 | base::StringPiece( |
Zentaro Kavanagh | 6ccee51 | 2017-09-28 18:34:09 | [diff] [blame] | 8288 | reinterpret_cast<const char*>( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8289 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8290 | base::size( |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8291 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8292 | &authenticate_msg); |
| 8293 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 8294 | MockWrite writes0[] = {CreateMockWrite(request0, 0)}; |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8295 | MockRead reads0[] = {CreateMockRead(resp, 1), |
| 8296 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 2)}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8297 | |
| 8298 | // Retry yet again using HTTP/1.1. |
| 8299 | MockWrite writes1[] = { |
| 8300 | // After restarting with a null identity, this is the |
| 8301 | // request we should be issuing -- the final header line contains a Type |
| 8302 | // 1 message. |
| 8303 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8304 | "Host: server\r\n" |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8305 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8306 | "Authorization: NTLM "), |
| 8307 | MockWrite(negotiate_msg.c_str()), MockWrite("\r\n\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8308 | |
| 8309 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8310 | // (the credentials for the origin server). The second request continues |
| 8311 | // on the same connection. |
| 8312 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
Zentaro Kavanagh | 1890a3d | 2018-01-29 19:52:55 | [diff] [blame] | 8313 | "Host: server\r\n" |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8314 | "Connection: keep-alive\r\n" |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8315 | "Authorization: NTLM "), |
| 8316 | MockWrite(authenticate_msg.c_str()), MockWrite("\r\n\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8317 | }; |
| 8318 | |
| 8319 | MockRead reads1[] = { |
| 8320 | // The origin server responds with a Type 2 message. |
| 8321 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8322 | MockRead("WWW-Authenticate: NTLM "), MockRead(challenge_msg.c_str()), |
| 8323 | MockRead("\r\n"), MockRead("Content-Length: 42\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8324 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8325 | MockRead("You are not authorized to view this page\r\n"), |
| 8326 | |
| 8327 | // Lastly we get the desired content. |
| 8328 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8329 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8330 | MockRead("Content-Length: 14\r\n\r\n"), MockRead("Please Login\r\n"), |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8331 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8332 | SequencedSocketData data0(reads0, writes0); |
| 8333 | StaticSocketDataProvider data1(reads1, writes1); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8334 | session_deps_.socket_factory->AddSocketDataProvider(&data0); |
| 8335 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8336 | |
| 8337 | SSLSocketDataProvider ssl0(ASYNC, OK); |
| 8338 | ssl0.next_proto = kProtoHTTP2; |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8339 | ssl0.next_protos_expected_in_ssl_config = |
| 8340 | NextProtoVector{kProtoHTTP2, kProtoHTTP11}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8341 | SSLSocketDataProvider ssl1(ASYNC, OK); |
Matt Menke | 6082f95 | 2019-08-21 16:23:16 | [diff] [blame] | 8342 | // When creating the second connection, only HTTP/1.1 should be allowed. |
| 8343 | ssl1.next_protos_expected_in_ssl_config = NextProtoVector{kProtoHTTP11}; |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8344 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl0); |
| 8345 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8346 | |
| 8347 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8348 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 8349 | |
| 8350 | TestCompletionCallback callback1; |
| 8351 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
| 8352 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8353 | |
| 8354 | rv = callback1.WaitForResult(); |
| 8355 | EXPECT_THAT(rv, IsOk()); |
| 8356 | |
| 8357 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
| 8358 | |
| 8359 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 8360 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8361 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8362 | |
| 8363 | TestCompletionCallback callback2; |
| 8364 | |
Zentaro Kavanagh | 5b27a6e2 | 2017-09-25 23:00:37 | [diff] [blame] | 8365 | rv = trans.RestartWithAuth( |
| 8366 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8367 | callback2.callback()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8368 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8369 | |
| 8370 | rv = callback2.WaitForResult(); |
| 8371 | EXPECT_THAT(rv, IsOk()); |
| 8372 | |
| 8373 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8374 | |
| 8375 | response = trans.GetResponseInfo(); |
| 8376 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8377 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8378 | |
| 8379 | TestCompletionCallback callback3; |
| 8380 | |
| 8381 | rv = trans.RestartWithAuth(AuthCredentials(), callback3.callback()); |
| 8382 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8383 | |
| 8384 | rv = callback3.WaitForResult(); |
| 8385 | EXPECT_THAT(rv, IsOk()); |
| 8386 | |
| 8387 | response = trans.GetResponseInfo(); |
| 8388 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8389 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Bence Béky | 1e4ef19 | 2017-09-18 19:58:02 | [diff] [blame] | 8390 | EXPECT_EQ(14, response->headers->GetContentLength()); |
| 8391 | |
| 8392 | std::string response_data; |
| 8393 | rv = ReadTransaction(&trans, &response_data); |
| 8394 | EXPECT_THAT(rv, IsOk()); |
| 8395 | EXPECT_EQ("Please Login\r\n", response_data); |
| 8396 | |
| 8397 | EXPECT_TRUE(data0.AllReadDataConsumed()); |
| 8398 | EXPECT_TRUE(data0.AllWriteDataConsumed()); |
| 8399 | EXPECT_TRUE(data1.AllReadDataConsumed()); |
| 8400 | EXPECT_TRUE(data1.AllWriteDataConsumed()); |
Bence Béky | 83eb351 | 2017-09-05 12:56:09 | [diff] [blame] | 8401 | } |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8402 | |
Matt Menke | f2ee07c | 2019-08-29 02:10:36 | [diff] [blame] | 8403 | #if BUILDFLAG(ENABLE_WEBSOCKETS) |
| 8404 | |
| 8405 | // Variant of above test using WebSockets. |
| 8406 | TEST_F(HttpNetworkTransactionTest, NTLMOverHttp2WithWebsockets) { |
| 8407 | const GURL kInitialUrl("https://server/"); |
| 8408 | const GURL kWebSocketUrl("wss://server/"); |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 8409 | HttpAuthNtlmMechanism::ScopedProcSetter proc_setter( |
Matt Menke | f2ee07c | 2019-08-29 02:10:36 | [diff] [blame] | 8410 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
| 8411 | |
| 8412 | // Initial request establishes an H2 connection, which will then be reused for |
| 8413 | // WebSockets. This is needed since WebSockets will reuse H2 connections, but |
| 8414 | // it won't create a new one. |
| 8415 | spdy::SpdyHeaderBlock initial_request_headers( |
| 8416 | spdy_util_.ConstructGetHeaderBlock(kInitialUrl.spec())); |
| 8417 | spdy::SpdySerializedFrame initial_request(spdy_util_.ConstructSpdyHeaders( |
| 8418 | 1, std::move(initial_request_headers), DEFAULT_PRIORITY, true)); |
| 8419 | spdy::SpdySerializedFrame settings_ack(spdy_util_.ConstructSpdySettingsAck()); |
| 8420 | |
| 8421 | // Settings frame, indicating WebSockets is supported. |
| 8422 | spdy::SettingsMap settings; |
| 8423 | settings[spdy::SETTINGS_ENABLE_CONNECT_PROTOCOL] = 1; |
| 8424 | spdy::SpdySerializedFrame settings_frame( |
| 8425 | spdy_util_.ConstructSpdySettings(settings)); |
| 8426 | |
| 8427 | // Response headers for first request. Body is never received, but that |
| 8428 | // shouldn't matter for the purposes of this test. |
| 8429 | spdy::SpdySerializedFrame initial_response( |
| 8430 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 8431 | |
| 8432 | // First WebSocket request, which has no credentials. |
| 8433 | spdy::SpdyHeaderBlock websocket_request_headers; |
| 8434 | websocket_request_headers[spdy::kHttp2MethodHeader] = "CONNECT"; |
| 8435 | websocket_request_headers[spdy::kHttp2AuthorityHeader] = "server"; |
| 8436 | websocket_request_headers[spdy::kHttp2SchemeHeader] = "https"; |
| 8437 | websocket_request_headers[spdy::kHttp2PathHeader] = "/"; |
| 8438 | websocket_request_headers[spdy::kHttp2ProtocolHeader] = "websocket"; |
| 8439 | websocket_request_headers["origin"] = "http://server"; |
| 8440 | websocket_request_headers["sec-websocket-version"] = "13"; |
| 8441 | websocket_request_headers["sec-websocket-extensions"] = |
| 8442 | "permessage-deflate; client_max_window_bits"; |
| 8443 | spdy::SpdySerializedFrame websocket_request(spdy_util_.ConstructSpdyHeaders( |
| 8444 | 3, std::move(websocket_request_headers), MEDIUM, false)); |
| 8445 | |
| 8446 | // Auth challenge to WebSocket request. |
| 8447 | spdy::SpdyHeaderBlock auth_challenge_headers; |
| 8448 | auth_challenge_headers[spdy::kHttp2StatusHeader] = "401"; |
| 8449 | auth_challenge_headers["www-authenticate"] = "NTLM"; |
| 8450 | spdy::SpdySerializedFrame websocket_auth_challenge( |
| 8451 | spdy_util_.ConstructSpdyResponseHeaders( |
| 8452 | 3, std::move(auth_challenge_headers), true)); |
| 8453 | |
| 8454 | MockWrite writes0[] = {CreateMockWrite(initial_request, 0), |
| 8455 | CreateMockWrite(settings_ack, 2), |
| 8456 | CreateMockWrite(websocket_request, 4), |
| 8457 | MockWrite(SYNCHRONOUS, ERR_IO_PENDING, 7)}; |
| 8458 | MockRead reads0[] = {CreateMockRead(settings_frame, 1), |
| 8459 | CreateMockRead(initial_response, 3), |
| 8460 | CreateMockRead(websocket_auth_challenge, 5), |
| 8461 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6)}; |
| 8462 | |
| 8463 | // Generate the NTLM messages based on known test data. |
| 8464 | std::string negotiate_msg; |
| 8465 | std::string challenge_msg; |
| 8466 | std::string authenticate_msg; |
| 8467 | base::Base64Encode( |
| 8468 | base::StringPiece( |
| 8469 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
| 8470 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
| 8471 | &negotiate_msg); |
| 8472 | base::Base64Encode( |
| 8473 | base::StringPiece( |
| 8474 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
| 8475 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
| 8476 | &challenge_msg); |
| 8477 | base::Base64Encode( |
| 8478 | base::StringPiece( |
| 8479 | reinterpret_cast<const char*>( |
| 8480 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
| 8481 | base::size( |
| 8482 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
| 8483 | &authenticate_msg); |
| 8484 | |
| 8485 | // Retry yet again using HTTP/1.1. |
| 8486 | MockWrite writes1[] = { |
| 8487 | // After restarting with a null identity, this is the |
| 8488 | // request we should be issuing -- the final header line contains a Type |
| 8489 | // 1 message. |
| 8490 | MockWrite("GET / HTTP/1.1\r\n" |
| 8491 | "Host: server\r\n" |
| 8492 | "Connection: Upgrade\r\n" |
| 8493 | "Authorization: NTLM "), |
| 8494 | MockWrite(negotiate_msg.c_str()), |
| 8495 | MockWrite("\r\n"), |
| 8496 | MockWrite("Origin: http://server\r\n" |
| 8497 | "Sec-WebSocket-Version: 13\r\n" |
| 8498 | "Upgrade: websocket\r\n" |
| 8499 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 8500 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 8501 | "client_max_window_bits\r\n\r\n"), |
| 8502 | |
| 8503 | // After calling trans.RestartWithAuth(), we should send a Type 3 message |
| 8504 | // (the credentials for the origin server). The second request continues |
| 8505 | // on the same connection. |
| 8506 | MockWrite("GET / HTTP/1.1\r\n" |
| 8507 | "Host: server\r\n" |
| 8508 | "Connection: Upgrade\r\n" |
| 8509 | "Authorization: NTLM "), |
| 8510 | MockWrite(authenticate_msg.c_str()), |
| 8511 | MockWrite("\r\n"), |
| 8512 | MockWrite("Origin: http://server\r\n" |
| 8513 | "Sec-WebSocket-Version: 13\r\n" |
| 8514 | "Upgrade: websocket\r\n" |
| 8515 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 8516 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 8517 | "client_max_window_bits\r\n\r\n"), |
| 8518 | }; |
| 8519 | |
| 8520 | MockRead reads1[] = { |
| 8521 | // The origin server responds with a Type 2 message. |
| 8522 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 8523 | MockRead("WWW-Authenticate: NTLM "), |
| 8524 | MockRead(challenge_msg.c_str()), |
| 8525 | MockRead("\r\n"), |
| 8526 | MockRead("Content-Length: 42\r\n"), |
| 8527 | MockRead("Content-Type: text/html\r\n\r\n"), |
| 8528 | MockRead("You are not authorized to view this page\r\n"), |
| 8529 | |
| 8530 | // Lastly we get the desired content. |
| 8531 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 8532 | "Upgrade: websocket\r\n" |
| 8533 | "Connection: Upgrade\r\n" |
| 8534 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n"), |
| 8535 | }; |
| 8536 | SequencedSocketData data0(reads0, writes0); |
| 8537 | session_deps_.socket_factory->AddSocketDataProvider(&data0); |
| 8538 | SSLSocketDataProvider ssl0(ASYNC, OK); |
| 8539 | ssl0.next_proto = kProtoHTTP2; |
| 8540 | ssl0.next_protos_expected_in_ssl_config = |
| 8541 | NextProtoVector{kProtoHTTP2, kProtoHTTP11}; |
| 8542 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl0); |
| 8543 | |
| 8544 | StaticSocketDataProvider data1(reads1, writes1); |
| 8545 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 8546 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 8547 | // When creating the second connection, only HTTP/1.1 should be allowed. |
| 8548 | ssl1.next_protos_expected_in_ssl_config = NextProtoVector{}; |
| 8549 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 8550 | |
| 8551 | session_deps_.enable_websocket_over_http2 = true; |
| 8552 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8553 | |
| 8554 | HttpRequestInfo initial_request_info; |
| 8555 | initial_request_info.method = "GET"; |
| 8556 | initial_request_info.url = kInitialUrl; |
| 8557 | initial_request_info.traffic_annotation = |
| 8558 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8559 | HttpNetworkTransaction initial_trans(DEFAULT_PRIORITY, session.get()); |
| 8560 | TestCompletionCallback initial_callback; |
| 8561 | int rv = initial_trans.Start(&initial_request_info, |
| 8562 | initial_callback.callback(), NetLogWithSource()); |
| 8563 | EXPECT_THAT(initial_callback.GetResult(rv), IsOk()); |
| 8564 | |
| 8565 | EXPECT_FALSE(session->http_server_properties()->RequiresHTTP11( |
| 8566 | url::SchemeHostPort(kInitialUrl), NetworkIsolationKey())); |
| 8567 | |
| 8568 | HttpRequestInfo websocket_request_info; |
| 8569 | websocket_request_info.method = "GET"; |
| 8570 | websocket_request_info.url = kWebSocketUrl; |
| 8571 | websocket_request_info.traffic_annotation = |
| 8572 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8573 | EXPECT_TRUE(HostPortPair::FromURL(initial_request_info.url) |
| 8574 | .Equals(HostPortPair::FromURL(websocket_request_info.url))); |
| 8575 | websocket_request_info.extra_headers.SetHeader("Origin", "http://server"); |
| 8576 | websocket_request_info.extra_headers.SetHeader("Sec-WebSocket-Version", "13"); |
| 8577 | // The following two headers must be removed by WebSocketHttp2HandshakeStream. |
| 8578 | websocket_request_info.extra_headers.SetHeader("Connection", "Upgrade"); |
| 8579 | websocket_request_info.extra_headers.SetHeader("Upgrade", "websocket"); |
| 8580 | |
| 8581 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 8582 | |
| 8583 | HttpNetworkTransaction websocket_trans(MEDIUM, session.get()); |
| 8584 | websocket_trans.SetWebSocketHandshakeStreamCreateHelper( |
| 8585 | &websocket_stream_create_helper); |
| 8586 | |
| 8587 | TestCompletionCallback websocket_callback; |
| 8588 | rv = websocket_trans.Start(&websocket_request_info, |
| 8589 | websocket_callback.callback(), NetLogWithSource()); |
| 8590 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8591 | |
| 8592 | EXPECT_FALSE(websocket_trans.IsReadyToRestartForAuth()); |
| 8593 | |
| 8594 | const HttpResponseInfo* response = websocket_trans.GetResponseInfo(); |
| 8595 | ASSERT_TRUE(response); |
| 8596 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge)); |
| 8597 | |
| 8598 | rv = websocket_trans.RestartWithAuth( |
| 8599 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8600 | websocket_callback.callback()); |
| 8601 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8602 | |
| 8603 | EXPECT_TRUE(websocket_trans.IsReadyToRestartForAuth()); |
| 8604 | |
| 8605 | response = websocket_trans.GetResponseInfo(); |
| 8606 | ASSERT_TRUE(response); |
| 8607 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 8608 | |
| 8609 | rv = websocket_trans.RestartWithAuth(AuthCredentials(), |
| 8610 | websocket_callback.callback()); |
| 8611 | EXPECT_THAT(websocket_callback.GetResult(rv), IsOk()); |
| 8612 | |
| 8613 | // The server should have been marked as requiring HTTP/1.1. The important |
| 8614 | // part here is that the scheme that requires HTTP/1.1 should be HTTPS, not |
| 8615 | // WSS. |
| 8616 | EXPECT_TRUE(session->http_server_properties()->RequiresHTTP11( |
| 8617 | url::SchemeHostPort(kInitialUrl), NetworkIsolationKey())); |
| 8618 | } |
| 8619 | |
| 8620 | #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
| 8621 | |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8622 | // Test that, if we have an NTLM proxy and the origin resets the connection, we |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 8623 | // do no retry forever as a result of TLS retries. This is a regression test for |
| 8624 | // https://crbug.com/823387. The version interference probe has since been |
| 8625 | // removed, but we now have a legacy crypto fallback. (If that fallback is |
| 8626 | // removed, this test should be kept but with the expectations tweaked, in case |
| 8627 | // future fallbacks are added.) |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8628 | TEST_F(HttpNetworkTransactionTest, NTLMProxyTLSHandshakeReset) { |
| 8629 | // The NTLM test data expects the proxy to be named 'server'. The origin is |
| 8630 | // https://origin/. |
| 8631 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 8632 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8633 | "PROXY server", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8634 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 8635 | SSLContextConfig config; |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8636 | session_deps_.ssl_config_service = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8637 | std::make_unique<TestSSLConfigService>(config); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8638 | |
| 8639 | HttpRequestInfo request; |
| 8640 | request.method = "GET"; |
| 8641 | request.url = GURL("https://origin/"); |
| 8642 | request.traffic_annotation = |
| 8643 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 8644 | |
| 8645 | // Ensure load is not disrupted by flags which suppress behaviour specific |
| 8646 | // to other auth schemes. |
| 8647 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
| 8648 | |
Asanka Herath | bf0b55d | 2019-12-07 03:27:09 | [diff] [blame] | 8649 | HttpAuthNtlmMechanism::ScopedProcSetter proc_setter( |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8650 | MockGetMSTime, MockGenerateRandom, MockGetHostName); |
| 8651 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 8652 | |
| 8653 | // Generate the NTLM messages based on known test data. |
| 8654 | std::string negotiate_msg; |
| 8655 | std::string challenge_msg; |
| 8656 | std::string authenticate_msg; |
| 8657 | base::Base64Encode( |
| 8658 | base::StringPiece( |
| 8659 | reinterpret_cast<const char*>(ntlm::test::kExpectedNegotiateMsg), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8660 | base::size(ntlm::test::kExpectedNegotiateMsg)), |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8661 | &negotiate_msg); |
| 8662 | base::Base64Encode( |
| 8663 | base::StringPiece( |
| 8664 | reinterpret_cast<const char*>(ntlm::test::kChallengeMsgFromSpecV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8665 | base::size(ntlm::test::kChallengeMsgFromSpecV2)), |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8666 | &challenge_msg); |
| 8667 | base::Base64Encode( |
| 8668 | base::StringPiece( |
| 8669 | reinterpret_cast<const char*>( |
| 8670 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8671 | base::size( |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8672 | ntlm::test::kExpectedAuthenticateMsgEmptyChannelBindingsV2)), |
| 8673 | &authenticate_msg); |
| 8674 | |
| 8675 | MockWrite data_writes[] = { |
| 8676 | // The initial CONNECT request. |
| 8677 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8678 | "Host: origin:443\r\n" |
| 8679 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8680 | |
| 8681 | // After restarting with an identity. |
| 8682 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8683 | "Host: origin:443\r\n" |
| 8684 | "Proxy-Connection: keep-alive\r\n" |
| 8685 | "Proxy-Authorization: NTLM "), |
| 8686 | MockWrite(negotiate_msg.c_str()), |
| 8687 | // End headers. |
| 8688 | MockWrite("\r\n\r\n"), |
| 8689 | |
| 8690 | // The second restart. |
| 8691 | MockWrite("CONNECT origin:443 HTTP/1.1\r\n" |
| 8692 | "Host: origin:443\r\n" |
| 8693 | "Proxy-Connection: keep-alive\r\n" |
| 8694 | "Proxy-Authorization: NTLM "), |
| 8695 | MockWrite(authenticate_msg.c_str()), |
| 8696 | // End headers. |
| 8697 | MockWrite("\r\n\r\n"), |
| 8698 | }; |
| 8699 | |
| 8700 | MockRead data_reads[] = { |
| 8701 | // The initial NTLM response. |
| 8702 | MockRead("HTTP/1.1 407 Access Denied\r\n" |
| 8703 | "Content-Length: 0\r\n" |
| 8704 | "Proxy-Authenticate: NTLM\r\n\r\n"), |
| 8705 | |
| 8706 | // The NTLM challenge message. |
| 8707 | MockRead("HTTP/1.1 407 Access Denied\r\n" |
| 8708 | "Content-Length: 0\r\n" |
| 8709 | "Proxy-Authenticate: NTLM "), |
| 8710 | MockRead(challenge_msg.c_str()), |
| 8711 | // End headers. |
| 8712 | MockRead("\r\n\r\n"), |
| 8713 | |
| 8714 | // Finally the tunnel is established. |
| 8715 | MockRead("HTTP/1.1 200 Connected\r\n\r\n"), |
| 8716 | }; |
| 8717 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8718 | StaticSocketDataProvider data(data_reads, data_writes); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8719 | SSLSocketDataProvider data_ssl(ASYNC, ERR_CONNECTION_RESET); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8720 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 8721 | session_deps_.socket_factory->AddSSLSocketDataProvider(&data_ssl); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8722 | |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 8723 | StaticSocketDataProvider data2(data_reads, data_writes); |
| 8724 | SSLSocketDataProvider data2_ssl(ASYNC, ERR_CONNECTION_RESET); |
| 8725 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 8726 | session_deps_.socket_factory->AddSSLSocketDataProvider(&data2_ssl); |
| 8727 | |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8728 | // Start the transaction. The proxy responds with an NTLM authentication |
| 8729 | // request. |
| 8730 | TestCompletionCallback callback; |
| 8731 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 8732 | int rv = callback.GetResult( |
| 8733 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
| 8734 | |
| 8735 | EXPECT_THAT(rv, IsOk()); |
| 8736 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
| 8737 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 8738 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8739 | EXPECT_TRUE(CheckNTLMProxyAuth(response->auth_challenge)); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8740 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 8741 | // Configure credentials and restart. The proxy responds with the challenge |
| 8742 | // message. |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8743 | rv = callback.GetResult(trans.RestartWithAuth( |
| 8744 | AuthCredentials(ntlm::test::kDomainUserCombined, ntlm::test::kPassword), |
| 8745 | callback.callback())); |
| 8746 | EXPECT_THAT(rv, IsOk()); |
| 8747 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8748 | response = trans.GetResponseInfo(); |
| 8749 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 8750 | EXPECT_FALSE(response->auth_challenge.has_value()); |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8751 | |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 8752 | // Restart once more. The tunnel will be established and the the SSL handshake |
| 8753 | // will reset. The fallback will then kick in and restart the process. The |
| 8754 | // proxy responds with another NTLM authentiation request, but we don't need |
| 8755 | // to provide credentials as the cached ones work. |
| 8756 | rv = callback.GetResult( |
| 8757 | trans.RestartWithAuth(AuthCredentials(), callback.callback())); |
| 8758 | EXPECT_THAT(rv, IsOk()); |
| 8759 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8760 | response = trans.GetResponseInfo(); |
| 8761 | ASSERT_TRUE(response); |
| 8762 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 8763 | |
| 8764 | // The proxy responds with the NTLM challenge message. |
| 8765 | rv = callback.GetResult( |
| 8766 | trans.RestartWithAuth(AuthCredentials(), callback.callback())); |
| 8767 | EXPECT_THAT(rv, IsOk()); |
| 8768 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
| 8769 | response = trans.GetResponseInfo(); |
| 8770 | ASSERT_TRUE(response); |
| 8771 | EXPECT_FALSE(response->auth_challenge.has_value()); |
| 8772 | |
| 8773 | // Send the NTLM authenticate message. The tunnel is established and the |
| 8774 | // handshake resets again. We should not retry again. |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 8775 | rv = callback.GetResult( |
| 8776 | trans.RestartWithAuth(AuthCredentials(), callback.callback())); |
| 8777 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
| 8778 | } |
| 8779 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 8780 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 8781 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8782 | // Test reading a server response which has only headers, and no body. |
| 8783 | // After some maximum number of bytes is consumed, the transaction should |
| 8784 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8785 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 8786 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8787 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8788 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8789 | request.traffic_annotation = |
| 8790 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8791 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8792 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8793 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8794 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 8795 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 8796 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 8797 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8798 | |
| 8799 | MockRead data_reads[] = { |
| 8800 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8801 | MockRead(ASYNC, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8802 | MockRead("\r\nBODY"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8803 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8804 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8805 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8806 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8807 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8808 | TestCompletionCallback callback; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8809 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8810 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8811 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8812 | |
| 8813 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8814 | EXPECT_THAT(rv, IsError(ERR_RESPONSE_HEADERS_TOO_BIG)); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 8815 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8816 | |
| 8817 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 8818 | // establish tunnel. |
| 8819 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8820 | TEST_F(HttpNetworkTransactionTest, DontRecycleTransportSocketForSSLTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8821 | HttpRequestInfo request; |
| 8822 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8823 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8824 | request.traffic_annotation = |
| 8825 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8826 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8827 | // Configure against proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 8828 | session_deps_.proxy_resolution_service = |
| 8829 | ConfiguredProxyResolutionService::CreateFixed( |
| 8830 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 8831 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8832 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8833 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 8834 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 8835 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8836 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8837 | // Since we have proxy, should try to establish tunnel. |
| 8838 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 8839 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 8840 | "Host: www.example.org:443\r\n" |
| 8841 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8842 | }; |
| 8843 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 8844 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8845 | // connection. Usually a proxy would return 501 (not implemented), |
| 8846 | // or 200 (tunnel established). |
| 8847 | MockRead data_reads1[] = { |
mmenke | d39192ee | 2015-12-09 00:57:23 | [diff] [blame] | 8848 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 8849 | MockRead("Content-Length: 10\r\n\r\n"), |
| 8850 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8851 | }; |
| 8852 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8853 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8854 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8855 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8856 | TestCompletionCallback callback1; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8857 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8858 | int rv = trans->Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8859 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8860 | |
| 8861 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8862 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8863 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 8864 | // Empty the current queue. This is necessary because idle sockets are |
| 8865 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8866 | base::RunLoop().RunUntilIdle(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 8867 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8868 | // We now check to make sure the TCPClientSocket was not added back to |
| 8869 | // the pool. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8870 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8871 | trans.reset(); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8872 | base::RunLoop().RunUntilIdle(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8873 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8874 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 8875 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 8876 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8877 | // Make sure that we recycle a socket after reading all of the response body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8878 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 8879 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8880 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8881 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8882 | request.traffic_annotation = |
| 8883 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8884 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8885 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8886 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8887 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8888 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8889 | MockRead data_reads[] = { |
| 8890 | // A part of the response body is received with the response headers. |
| 8891 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 8892 | // The rest of the response body is received in two parts. |
| 8893 | MockRead("lo"), |
| 8894 | MockRead(" world"), |
| 8895 | MockRead("junk"), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8896 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8897 | }; |
| 8898 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8899 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8900 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8901 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8902 | TestCompletionCallback callback; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8903 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8904 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8905 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8906 | |
| 8907 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8908 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8909 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8910 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8911 | ASSERT_TRUE(response); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8912 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8913 | EXPECT_TRUE(response->headers); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8914 | std::string status_line = response->headers->GetStatusLine(); |
| 8915 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 8916 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8917 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8918 | |
| 8919 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8920 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8921 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8922 | EXPECT_EQ("hello world", response_data); |
| 8923 | |
| 8924 | // Empty the current queue. This is necessary because idle sockets are |
| 8925 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8926 | base::RunLoop().RunUntilIdle(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8927 | |
| 8928 | // We now check to make sure the socket was added back to the pool. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8929 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 8930 | } |
| 8931 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8932 | // Make sure that we recycle a SSL socket after reading all of the response |
| 8933 | // body. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8934 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8935 | HttpRequestInfo request; |
| 8936 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8937 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8938 | request.traffic_annotation = |
| 8939 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8940 | |
| 8941 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8942 | MockWrite( |
| 8943 | "GET / HTTP/1.1\r\n" |
| 8944 | "Host: www.example.org\r\n" |
| 8945 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8946 | }; |
| 8947 | |
| 8948 | MockRead data_reads[] = { |
| 8949 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8950 | MockRead("Content-Length: 11\r\n\r\n"), |
| 8951 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8952 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8953 | }; |
| 8954 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 8955 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8956 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8957 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 8958 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 8959 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8960 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8961 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8962 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 8963 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8964 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8965 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 8966 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8967 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8968 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 8969 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8970 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8971 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 8972 | ASSERT_TRUE(response); |
| 8973 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8974 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8975 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8976 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8977 | |
| 8978 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 8979 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 8980 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8981 | EXPECT_EQ("hello world", response_data); |
| 8982 | |
| 8983 | // Empty the current queue. This is necessary because idle sockets are |
| 8984 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 8985 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8986 | |
| 8987 | // We now check to make sure the socket was added back to the pool. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 8988 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8989 | } |
| 8990 | |
| 8991 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 8992 | // from the pool and make sure that we recover okay. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 8993 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8994 | HttpRequestInfo request; |
| 8995 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 8996 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 8997 | request.traffic_annotation = |
| 8998 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8999 | |
| 9000 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9001 | MockWrite( |
| 9002 | "GET / HTTP/1.1\r\n" |
| 9003 | "Host: www.example.org\r\n" |
| 9004 | "Connection: keep-alive\r\n\r\n"), |
| 9005 | MockWrite( |
| 9006 | "GET / HTTP/1.1\r\n" |
| 9007 | "Host: www.example.org\r\n" |
| 9008 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9009 | }; |
| 9010 | |
| 9011 | MockRead data_reads[] = { |
mmenke | 911ee022 | 2015-12-04 03:58:42 | [diff] [blame] | 9012 | MockRead("HTTP/1.1 200 OK\r\n"), MockRead("Content-Length: 11\r\n\r\n"), |
| 9013 | MockRead("hello world"), MockRead(ASYNC, ERR_CONNECTION_CLOSED)}; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9014 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9015 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 9016 | SSLSocketDataProvider ssl2(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9017 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 9018 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9019 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9020 | StaticSocketDataProvider data(data_reads, data_writes); |
| 9021 | StaticSocketDataProvider data2(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9022 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9023 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9024 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9025 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9026 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9027 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 9028 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 9029 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9030 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9031 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9032 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9033 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9034 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9035 | |
| 9036 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9037 | ASSERT_TRUE(response); |
| 9038 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9039 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9040 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9041 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9042 | |
| 9043 | std::string response_data; |
| 9044 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9045 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9046 | EXPECT_EQ("hello world", response_data); |
| 9047 | |
| 9048 | // Empty the current queue. This is necessary because idle sockets are |
| 9049 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9050 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9051 | |
| 9052 | // We now check to make sure the socket was added back to the pool. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9053 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9054 | |
| 9055 | // Now start the second transaction, which should reuse the previous socket. |
| 9056 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 9057 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 9058 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9059 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9060 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9061 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9062 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9063 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9064 | |
| 9065 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9066 | ASSERT_TRUE(response); |
| 9067 | ASSERT_TRUE(response->headers); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9068 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9069 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9070 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9071 | |
| 9072 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9073 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9074 | EXPECT_EQ("hello world", response_data); |
| 9075 | |
| 9076 | // Empty the current queue. This is necessary because idle sockets are |
| 9077 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9078 | base::RunLoop().RunUntilIdle(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9079 | |
| 9080 | // We now check to make sure the socket was added back to the pool. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9081 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 9082 | } |
| 9083 | |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9084 | // Grab a socket, use it, and put it back into the pool. Then, make |
| 9085 | // low memory notification and ensure the socket pool is flushed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9086 | TEST_F(HttpNetworkTransactionTest, FlushSocketPoolOnLowMemoryNotifications) { |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9087 | HttpRequestInfo request; |
| 9088 | request.method = "GET"; |
| 9089 | request.url = GURL("http://www.example.org/"); |
| 9090 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9091 | request.traffic_annotation = |
| 9092 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9093 | |
| 9094 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 9095 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9096 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9097 | |
| 9098 | MockRead data_reads[] = { |
| 9099 | // A part of the response body is received with the response headers. |
| 9100 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 9101 | // The rest of the response body is received in two parts. |
| 9102 | MockRead("lo"), MockRead(" world"), |
| 9103 | MockRead("junk"), // Should not be read!! |
| 9104 | MockRead(SYNCHRONOUS, OK), |
| 9105 | }; |
| 9106 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9107 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9108 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9109 | |
| 9110 | TestCompletionCallback callback; |
| 9111 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9112 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9113 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9114 | |
| 9115 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9116 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9117 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9118 | ASSERT_TRUE(response); |
| 9119 | EXPECT_TRUE(response->headers); |
| 9120 | std::string status_line = response->headers->GetStatusLine(); |
| 9121 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 9122 | |
| 9123 | // Make memory critical notification and ensure the transaction still has been |
| 9124 | // operating right. |
| 9125 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9126 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9127 | base::RunLoop().RunUntilIdle(); |
| 9128 | |
| 9129 | // Socket should not be flushed as long as it is not idle. |
| 9130 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9131 | |
| 9132 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9133 | rv = ReadTransaction(&trans, &response_data); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9134 | EXPECT_THAT(rv, IsOk()); |
| 9135 | EXPECT_EQ("hello world", response_data); |
| 9136 | |
| 9137 | // Empty the current queue. This is necessary because idle sockets are |
| 9138 | // added to the connection pool asynchronously with a PostTask. |
| 9139 | base::RunLoop().RunUntilIdle(); |
| 9140 | |
| 9141 | // We now check to make sure the socket was added back to the pool. |
| 9142 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9143 | |
| 9144 | // Idle sockets should be flushed now. |
| 9145 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9146 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9147 | base::RunLoop().RunUntilIdle(); |
| 9148 | |
| 9149 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9150 | } |
| 9151 | |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9152 | // Disable idle socket closing on memory pressure. |
| 9153 | // Grab a socket, use it, and put it back into the pool. Then, make |
| 9154 | // low memory notification and ensure the socket pool is NOT flushed. |
| 9155 | TEST_F(HttpNetworkTransactionTest, NoFlushSocketPoolOnLowMemoryNotifications) { |
| 9156 | HttpRequestInfo request; |
| 9157 | request.method = "GET"; |
| 9158 | request.url = GURL("http://www.example.org/"); |
| 9159 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9160 | request.traffic_annotation = |
| 9161 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9162 | |
| 9163 | // Disable idle socket closing on memory pressure. |
| 9164 | session_deps_.disable_idle_sockets_close_on_memory_pressure = true; |
| 9165 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 9166 | |
| 9167 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 9168 | |
| 9169 | MockRead data_reads[] = { |
| 9170 | // A part of the response body is received with the response headers. |
| 9171 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 9172 | // The rest of the response body is received in two parts. |
| 9173 | MockRead("lo"), MockRead(" world"), |
| 9174 | MockRead("junk"), // Should not be read!! |
| 9175 | MockRead(SYNCHRONOUS, OK), |
| 9176 | }; |
| 9177 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9178 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
yucliu | 48f235d | 2018-01-11 00:59:55 | [diff] [blame] | 9179 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9180 | |
| 9181 | TestCompletionCallback callback; |
| 9182 | |
| 9183 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 9184 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 9185 | |
| 9186 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9187 | |
| 9188 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 9189 | ASSERT_TRUE(response); |
| 9190 | EXPECT_TRUE(response->headers); |
| 9191 | std::string status_line = response->headers->GetStatusLine(); |
| 9192 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 9193 | |
| 9194 | // Make memory critical notification and ensure the transaction still has been |
| 9195 | // operating right. |
| 9196 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9197 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9198 | base::RunLoop().RunUntilIdle(); |
| 9199 | |
| 9200 | // Socket should not be flushed as long as it is not idle. |
| 9201 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9202 | |
| 9203 | std::string response_data; |
| 9204 | rv = ReadTransaction(&trans, &response_data); |
| 9205 | EXPECT_THAT(rv, IsOk()); |
| 9206 | EXPECT_EQ("hello world", response_data); |
| 9207 | |
| 9208 | // Empty the current queue. This is necessary because idle sockets are |
| 9209 | // added to the connection pool asynchronously with a PostTask. |
| 9210 | base::RunLoop().RunUntilIdle(); |
| 9211 | |
| 9212 | // We now check to make sure the socket was added back to the pool. |
| 9213 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9214 | |
| 9215 | // Idle sockets should NOT be flushed on moderate memory pressure. |
| 9216 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9217 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 9218 | base::RunLoop().RunUntilIdle(); |
| 9219 | |
| 9220 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9221 | |
| 9222 | // Idle sockets should NOT be flushed on critical memory pressure. |
| 9223 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9224 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9225 | base::RunLoop().RunUntilIdle(); |
| 9226 | |
| 9227 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 9228 | } |
| 9229 | |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9230 | // Grab an SSL socket, use it, and put it back into the pool. Then, make |
| 9231 | // low memory notification and ensure the socket pool is flushed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9232 | TEST_F(HttpNetworkTransactionTest, FlushSSLSocketPoolOnLowMemoryNotifications) { |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9233 | HttpRequestInfo request; |
| 9234 | request.method = "GET"; |
| 9235 | request.url = GURL("https://www.example.org/"); |
| 9236 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9237 | request.traffic_annotation = |
| 9238 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9239 | |
| 9240 | MockWrite data_writes[] = { |
| 9241 | MockWrite("GET / HTTP/1.1\r\n" |
| 9242 | "Host: www.example.org\r\n" |
| 9243 | "Connection: keep-alive\r\n\r\n"), |
| 9244 | }; |
| 9245 | |
| 9246 | MockRead data_reads[] = { |
| 9247 | MockRead("HTTP/1.1 200 OK\r\n"), MockRead("Content-Length: 11\r\n\r\n"), |
| 9248 | MockRead("hello world"), MockRead(ASYNC, ERR_CONNECTION_CLOSED)}; |
| 9249 | |
| 9250 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 9251 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 9252 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9253 | StaticSocketDataProvider data(data_reads, data_writes); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9254 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 9255 | |
| 9256 | TestCompletionCallback callback; |
| 9257 | |
| 9258 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9259 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9260 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9261 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9262 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9263 | |
| 9264 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 9265 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9266 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9267 | ASSERT_TRUE(response); |
| 9268 | ASSERT_TRUE(response->headers); |
| 9269 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9270 | |
| 9271 | // Make memory critical notification and ensure the transaction still has been |
| 9272 | // operating right. |
| 9273 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9274 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9275 | base::RunLoop().RunUntilIdle(); |
| 9276 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9277 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9278 | |
| 9279 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9280 | rv = ReadTransaction(&trans, &response_data); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9281 | EXPECT_THAT(rv, IsOk()); |
| 9282 | EXPECT_EQ("hello world", response_data); |
| 9283 | |
| 9284 | // Empty the current queue. This is necessary because idle sockets are |
| 9285 | // added to the connection pool asynchronously with a PostTask. |
| 9286 | base::RunLoop().RunUntilIdle(); |
| 9287 | |
| 9288 | // We now check to make sure the socket was added back to the pool. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9289 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9290 | |
| 9291 | // Make memory notification once again and ensure idle socket is closed. |
| 9292 | base::MemoryPressureListener::NotifyMemoryPressure( |
| 9293 | base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 9294 | base::RunLoop().RunUntilIdle(); |
| 9295 | |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 9296 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
maksim.sisov | 0adf859 | 2016-07-15 06:25:56 | [diff] [blame] | 9297 | } |
| 9298 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9299 | // Make sure that we recycle a socket after a zero-length response. |
| 9300 | // http://crbug.com/9880 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9301 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9302 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9303 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9304 | request.url = GURL( |
| 9305 | "http://www.example.org/csi?v=3&s=web&action=&" |
| 9306 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 9307 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 9308 | "rt=prt.2642,ol.2649,xjs.2951"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9309 | request.traffic_annotation = |
| 9310 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9311 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9312 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9313 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9314 | MockRead data_reads[] = { |
| 9315 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 9316 | "Content-Length: 0\r\n" |
| 9317 | "Content-Type: text/html\r\n\r\n"), |
| 9318 | MockRead("junk"), // Should not be read!! |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9319 | MockRead(SYNCHRONOUS, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9320 | }; |
| 9321 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9322 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9323 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9324 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 9325 | // Transaction must be created after the MockReads, so it's destroyed before |
| 9326 | // them. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9327 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 9328 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9329 | TestCompletionCallback callback; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9330 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9331 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9332 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9333 | |
| 9334 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9335 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9336 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9337 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9338 | ASSERT_TRUE(response); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9339 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9340 | EXPECT_TRUE(response->headers); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9341 | std::string status_line = response->headers->GetStatusLine(); |
| 9342 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 9343 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9344 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9345 | |
| 9346 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9347 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9348 | EXPECT_THAT(rv, IsOk()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9349 | EXPECT_EQ("", response_data); |
| 9350 | |
| 9351 | // Empty the current queue. This is necessary because idle sockets are |
| 9352 | // added to the connection pool asynchronously with a PostTask. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9353 | base::RunLoop().RunUntilIdle(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9354 | |
| 9355 | // We now check to make sure the socket was added back to the pool. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 9356 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 9357 | } |
| 9358 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9359 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9360 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 9361 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 9362 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 9363 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 9364 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9365 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9366 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 9367 | // after use. |
| 9368 | request[0].method = "GET"; |
| 9369 | request[0].url = GURL("http://www.google.com/"); |
| 9370 | request[0].load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9371 | request[0].traffic_annotation = |
| 9372 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9373 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 9374 | // transaction 1. The first attempts fails when writing the POST data. |
| 9375 | // This causes the transaction to retry with a new socket. The second |
| 9376 | // attempt succeeds. |
| 9377 | request[1].method = "POST"; |
| 9378 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 9379 | request[1].upload_data_stream = &upload_data_stream; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9380 | request[1].load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9381 | request[1].traffic_annotation = |
| 9382 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9383 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9384 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9385 | |
| 9386 | // The first socket is used for transaction 1 and the first attempt of |
| 9387 | // transaction 2. |
| 9388 | |
| 9389 | // The response of transaction 1. |
| 9390 | MockRead data_reads1[] = { |
| 9391 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 9392 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9393 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9394 | }; |
| 9395 | // The mock write results of transaction 1 and the first attempt of |
| 9396 | // transaction 2. |
| 9397 | MockWrite data_writes1[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9398 | MockWrite(SYNCHRONOUS, 64), // GET |
| 9399 | MockWrite(SYNCHRONOUS, 93), // POST |
| 9400 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9401 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9402 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9403 | |
| 9404 | // The second socket is used for the second attempt of transaction 2. |
| 9405 | |
| 9406 | // The response of transaction 2. |
| 9407 | MockRead data_reads2[] = { |
| 9408 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 9409 | MockRead("welcome"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9410 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9411 | }; |
| 9412 | // The mock write results of the second attempt of transaction 2. |
| 9413 | MockWrite data_writes2[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9414 | MockWrite(SYNCHRONOUS, 93), // POST |
| 9415 | MockWrite(SYNCHRONOUS, 3), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9416 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9417 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9418 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9419 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9420 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9421 | |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 9422 | const char* const kExpectedResponseData[] = { |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9423 | "hello world", "welcome" |
| 9424 | }; |
| 9425 | |
| 9426 | for (int i = 0; i < 2; ++i) { |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9427 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9428 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9429 | TestCompletionCallback callback; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9430 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9431 | int rv = trans.Start(&request[i], callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9432 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9433 | |
| 9434 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9435 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9436 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9437 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9438 | ASSERT_TRUE(response); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9439 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9440 | EXPECT_TRUE(response->headers); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9441 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9442 | |
| 9443 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9444 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9445 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 9446 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 9447 | } |
| 9448 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9449 | |
| 9450 | // Test the request-challenge-retry sequence for basic auth when there is |
| 9451 | // an identity in the URL. The request should be sent as normal, but when |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9452 | // it fails the identity from the URL is used to answer the challenge. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9453 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9454 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9455 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9456 | request.url = GURL("http://foo:b@[email protected]/"); |
[email protected] | 7b08ba6 | 2012-02-10 20:19:41 | [diff] [blame] | 9457 | request.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9458 | request.traffic_annotation = |
| 9459 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 9460 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9461 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9462 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9463 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 9464 | // The password contains an escaped character -- for this test to pass it |
| 9465 | // will need to be unescaped by HttpNetworkTransaction. |
| 9466 | EXPECT_EQ("b%40r", request.url.password()); |
| 9467 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9468 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9469 | MockWrite( |
| 9470 | "GET / HTTP/1.1\r\n" |
| 9471 | "Host: www.example.org\r\n" |
| 9472 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9473 | }; |
| 9474 | |
| 9475 | MockRead data_reads1[] = { |
| 9476 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9477 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9478 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9479 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9480 | }; |
| 9481 | |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9482 | // After the challenge above, the transaction will be restarted using the |
| 9483 | // identity from the url (foo, b@r) to answer the challenge. |
| 9484 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9485 | MockWrite( |
| 9486 | "GET / HTTP/1.1\r\n" |
| 9487 | "Host: www.example.org\r\n" |
| 9488 | "Connection: keep-alive\r\n" |
| 9489 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9490 | }; |
| 9491 | |
| 9492 | MockRead data_reads2[] = { |
| 9493 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9494 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9495 | MockRead(SYNCHRONOUS, OK), |
| 9496 | }; |
| 9497 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9498 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9499 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9500 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9501 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9502 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9503 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9504 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9505 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9506 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9507 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9508 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9509 | |
| 9510 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9511 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9512 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9513 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9514 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9515 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9516 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9517 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9518 | ASSERT_TRUE(response); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9519 | |
| 9520 | // There is no challenge info, since the identity in URL worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9521 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9522 | |
| 9523 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9524 | |
| 9525 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9526 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9527 | } |
| 9528 | |
| 9529 | // Test the request-challenge-retry sequence for basic auth when there is an |
| 9530 | // incorrect identity in the URL. The identity from the URL should be used only |
| 9531 | // once. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9532 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9533 | HttpRequestInfo request; |
| 9534 | request.method = "GET"; |
| 9535 | // Note: the URL has a username:password in it. The password "baz" is |
| 9536 | // wrong (should be "bar"). |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9537 | request.url = GURL("http://foo:[email protected]/"); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9538 | |
| 9539 | request.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9540 | request.traffic_annotation = |
| 9541 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9542 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9543 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9544 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9545 | |
| 9546 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9547 | MockWrite( |
| 9548 | "GET / HTTP/1.1\r\n" |
| 9549 | "Host: www.example.org\r\n" |
| 9550 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9551 | }; |
| 9552 | |
| 9553 | MockRead data_reads1[] = { |
| 9554 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9555 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9556 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9557 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9558 | }; |
| 9559 | |
| 9560 | // After the challenge above, the transaction will be restarted using the |
| 9561 | // identity from the url (foo, baz) to answer the challenge. |
| 9562 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9563 | MockWrite( |
| 9564 | "GET / HTTP/1.1\r\n" |
| 9565 | "Host: www.example.org\r\n" |
| 9566 | "Connection: keep-alive\r\n" |
| 9567 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9568 | }; |
| 9569 | |
| 9570 | MockRead data_reads2[] = { |
| 9571 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9572 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9573 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9574 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9575 | }; |
| 9576 | |
| 9577 | // After the challenge above, the transaction will be restarted using the |
| 9578 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 9579 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9580 | MockWrite( |
| 9581 | "GET / HTTP/1.1\r\n" |
| 9582 | "Host: www.example.org\r\n" |
| 9583 | "Connection: keep-alive\r\n" |
| 9584 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9585 | }; |
| 9586 | |
| 9587 | MockRead data_reads3[] = { |
| 9588 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9589 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9590 | MockRead(SYNCHRONOUS, OK), |
| 9591 | }; |
| 9592 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9593 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9594 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 9595 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9596 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9597 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 9598 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9599 | |
| 9600 | TestCompletionCallback callback1; |
| 9601 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9602 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9603 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9604 | |
| 9605 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9606 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9607 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9608 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9609 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9610 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9611 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9612 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9613 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9614 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9615 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9616 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9617 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9618 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9619 | |
| 9620 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9621 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9622 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9623 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9624 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9625 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9626 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9627 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9628 | ASSERT_TRUE(response); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9629 | |
| 9630 | // There is no challenge info, since the identity worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9631 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2262e3a | 2012-05-22 16:08:16 | [diff] [blame] | 9632 | |
| 9633 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9634 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 9635 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9636 | base::RunLoop().RunUntilIdle(); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 9637 | } |
| 9638 | |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9639 | |
| 9640 | // Test the request-challenge-retry sequence for basic auth when there is a |
| 9641 | // correct identity in the URL, but its use is being suppressed. The identity |
| 9642 | // from the URL should never be used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9643 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURLSuppressed) { |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9644 | HttpRequestInfo request; |
| 9645 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9646 | request.url = GURL("http://foo:[email protected]/"); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9647 | request.load_flags = LOAD_DO_NOT_USE_EMBEDDED_IDENTITY; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9648 | request.traffic_annotation = |
| 9649 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9650 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9651 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9652 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9653 | |
| 9654 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9655 | MockWrite( |
| 9656 | "GET / HTTP/1.1\r\n" |
| 9657 | "Host: www.example.org\r\n" |
| 9658 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9659 | }; |
| 9660 | |
| 9661 | MockRead data_reads1[] = { |
| 9662 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9663 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9664 | MockRead("Content-Length: 10\r\n\r\n"), |
| 9665 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 9666 | }; |
| 9667 | |
| 9668 | // After the challenge above, the transaction will be restarted using the |
| 9669 | // identity supplied by the user, not the one in the URL, to answer the |
| 9670 | // challenge. |
| 9671 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9672 | MockWrite( |
| 9673 | "GET / HTTP/1.1\r\n" |
| 9674 | "Host: www.example.org\r\n" |
| 9675 | "Connection: keep-alive\r\n" |
| 9676 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9677 | }; |
| 9678 | |
| 9679 | MockRead data_reads3[] = { |
| 9680 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9681 | MockRead("Content-Length: 100\r\n\r\n"), |
| 9682 | MockRead(SYNCHRONOUS, OK), |
| 9683 | }; |
| 9684 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9685 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9686 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9687 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9688 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 9689 | |
| 9690 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9691 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9692 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9693 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9694 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9695 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9696 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9697 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9698 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9699 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9700 | |
| 9701 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9702 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9703 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9704 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9705 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9706 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9707 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9708 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9709 | ASSERT_TRUE(response); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9710 | |
| 9711 | // There is no challenge info, since the identity worked. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9712 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9713 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9714 | |
| 9715 | // Empty the current queue. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 9716 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 9717 | } |
| 9718 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9719 | // Test that previously tried username/passwords for a realm get re-used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 9720 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 9721 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9722 | |
| 9723 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 9724 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9725 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9726 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9727 | request.url = GURL("http://www.example.org/x/y/z"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9728 | request.traffic_annotation = |
| 9729 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9730 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9731 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9732 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9733 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9734 | MockWrite( |
| 9735 | "GET /x/y/z HTTP/1.1\r\n" |
| 9736 | "Host: www.example.org\r\n" |
| 9737 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9738 | }; |
| 9739 | |
| 9740 | MockRead data_reads1[] = { |
| 9741 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9742 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9743 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9744 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9745 | }; |
| 9746 | |
| 9747 | // Resend with authorization (username=foo, password=bar) |
| 9748 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9749 | MockWrite( |
| 9750 | "GET /x/y/z HTTP/1.1\r\n" |
| 9751 | "Host: www.example.org\r\n" |
| 9752 | "Connection: keep-alive\r\n" |
| 9753 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9754 | }; |
| 9755 | |
| 9756 | // Sever accepts the authorization. |
| 9757 | MockRead data_reads2[] = { |
| 9758 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9759 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9760 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9761 | }; |
| 9762 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9763 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9764 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9765 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9766 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9767 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9768 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9769 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9770 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9771 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9772 | |
| 9773 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9774 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9775 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9776 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9777 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9778 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9779 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9780 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9781 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9782 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 9783 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9784 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9785 | |
| 9786 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9787 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9788 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9789 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9790 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9791 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9792 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9793 | } |
| 9794 | |
| 9795 | // ------------------------------------------------------------------------ |
| 9796 | |
| 9797 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 9798 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9799 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9800 | request.method = "GET"; |
| 9801 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 9802 | // protection space as MyRealm1. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9803 | request.url = GURL("http://www.example.org/x/y/a/b"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9804 | request.traffic_annotation = |
| 9805 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9806 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9807 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9808 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9809 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9810 | MockWrite( |
| 9811 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 9812 | "Host: www.example.org\r\n" |
| 9813 | "Connection: keep-alive\r\n" |
| 9814 | // Send preemptive authorization for MyRealm1 |
| 9815 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9816 | }; |
| 9817 | |
| 9818 | // The server didn't like the preemptive authorization, and |
| 9819 | // challenges us for a different realm (MyRealm2). |
| 9820 | MockRead data_reads1[] = { |
| 9821 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9822 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 9823 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9824 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9825 | }; |
| 9826 | |
| 9827 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 9828 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9829 | MockWrite( |
| 9830 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 9831 | "Host: www.example.org\r\n" |
| 9832 | "Connection: keep-alive\r\n" |
| 9833 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9834 | }; |
| 9835 | |
| 9836 | // Sever accepts the authorization. |
| 9837 | MockRead data_reads2[] = { |
| 9838 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9839 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9840 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9841 | }; |
| 9842 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9843 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9844 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9845 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9846 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9847 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9848 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9849 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9850 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9851 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9852 | |
| 9853 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9854 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9855 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9856 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9857 | ASSERT_TRUE(response); |
| 9858 | ASSERT_TRUE(response->auth_challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 9859 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 9860 | EXPECT_EQ("http://www.example.org", |
| 9861 | response->auth_challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 9862 | EXPECT_EQ("MyRealm2", response->auth_challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 9863 | EXPECT_EQ(kBasicAuthScheme, response->auth_challenge->scheme); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9864 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9865 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9866 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9867 | rv = trans.RestartWithAuth(AuthCredentials(kFoo2, kBar2), |
| 9868 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9869 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9870 | |
| 9871 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9872 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9873 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9874 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9875 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9876 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9877 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9878 | } |
| 9879 | |
| 9880 | // ------------------------------------------------------------------------ |
| 9881 | |
| 9882 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 9883 | // succeed with preemptive authorization. |
| 9884 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9885 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9886 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9887 | request.url = GURL("http://www.example.org/x/y/z2"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9888 | request.traffic_annotation = |
| 9889 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9890 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9891 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9892 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9893 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9894 | MockWrite( |
| 9895 | "GET /x/y/z2 HTTP/1.1\r\n" |
| 9896 | "Host: www.example.org\r\n" |
| 9897 | "Connection: keep-alive\r\n" |
| 9898 | // The authorization for MyRealm1 gets sent preemptively |
| 9899 | // (since the url is in the same protection space) |
| 9900 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9901 | }; |
| 9902 | |
| 9903 | // Sever accepts the preemptive authorization |
| 9904 | MockRead data_reads1[] = { |
| 9905 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9906 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9907 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9908 | }; |
| 9909 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9910 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9911 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9912 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9913 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9914 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9915 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9916 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9917 | |
| 9918 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9919 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9920 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9921 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9922 | ASSERT_TRUE(response); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9923 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9924 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9925 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9926 | } |
| 9927 | |
| 9928 | // ------------------------------------------------------------------------ |
| 9929 | |
| 9930 | // Transaction 4: request another URL in MyRealm (however the |
| 9931 | // url is not known to belong to the protection space, so no pre-auth). |
| 9932 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 9933 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9934 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9935 | request.url = GURL("http://www.example.org/x/1"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 9936 | request.traffic_annotation = |
| 9937 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9938 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9939 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 9940 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9941 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9942 | MockWrite( |
| 9943 | "GET /x/1 HTTP/1.1\r\n" |
| 9944 | "Host: www.example.org\r\n" |
| 9945 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9946 | }; |
| 9947 | |
| 9948 | MockRead data_reads1[] = { |
| 9949 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 9950 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 9951 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9952 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9953 | }; |
| 9954 | |
| 9955 | // Resend with authorization from MyRealm's cache. |
| 9956 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 9957 | MockWrite( |
| 9958 | "GET /x/1 HTTP/1.1\r\n" |
| 9959 | "Host: www.example.org\r\n" |
| 9960 | "Connection: keep-alive\r\n" |
| 9961 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9962 | }; |
| 9963 | |
| 9964 | // Sever accepts the authorization. |
| 9965 | MockRead data_reads2[] = { |
| 9966 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 9967 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 9968 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9969 | }; |
| 9970 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 9971 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 9972 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 9973 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 9974 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9975 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9976 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9977 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 9978 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9979 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9980 | |
| 9981 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9982 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9983 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9984 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9985 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9986 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9987 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9988 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 9989 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9990 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 9991 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 9992 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 9993 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 9994 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 9995 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 9996 | } |
| 9997 | |
| 9998 | // ------------------------------------------------------------------------ |
| 9999 | |
| 10000 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 10001 | // cached identity. Should invalidate and re-prompt. |
| 10002 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 10003 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10004 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10005 | request.url = GURL("http://www.example.org/p/q/t"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10006 | request.traffic_annotation = |
| 10007 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10008 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10009 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10010 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10011 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10012 | MockWrite( |
| 10013 | "GET /p/q/t HTTP/1.1\r\n" |
| 10014 | "Host: www.example.org\r\n" |
| 10015 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10016 | }; |
| 10017 | |
| 10018 | MockRead data_reads1[] = { |
| 10019 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 10020 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 10021 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10022 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10023 | }; |
| 10024 | |
| 10025 | // Resend with authorization from cache for MyRealm. |
| 10026 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10027 | MockWrite( |
| 10028 | "GET /p/q/t HTTP/1.1\r\n" |
| 10029 | "Host: www.example.org\r\n" |
| 10030 | "Connection: keep-alive\r\n" |
| 10031 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10032 | }; |
| 10033 | |
| 10034 | // Sever rejects the authorization. |
| 10035 | MockRead data_reads2[] = { |
| 10036 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 10037 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 10038 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10039 | MockRead(SYNCHRONOUS, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10040 | }; |
| 10041 | |
| 10042 | // At this point we should prompt for new credentials for MyRealm. |
| 10043 | // Restart with username=foo3, password=foo4. |
| 10044 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10045 | MockWrite( |
| 10046 | "GET /p/q/t HTTP/1.1\r\n" |
| 10047 | "Host: www.example.org\r\n" |
| 10048 | "Connection: keep-alive\r\n" |
| 10049 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10050 | }; |
| 10051 | |
| 10052 | // Sever accepts the authorization. |
| 10053 | MockRead data_reads3[] = { |
| 10054 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10055 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10056 | MockRead(SYNCHRONOUS, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10057 | }; |
| 10058 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10059 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 10060 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 10061 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10062 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 10063 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 10064 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10065 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10066 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10067 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10068 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10069 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10070 | |
| 10071 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10072 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10073 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10074 | EXPECT_TRUE(trans.IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10075 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10076 | rv = trans.RestartWithAuth(AuthCredentials(), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10077 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 10078 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10079 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10080 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 10081 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10082 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10083 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10084 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10085 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10086 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10087 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10088 | rv = trans.RestartWithAuth(AuthCredentials(kFoo3, kBar3), |
| 10089 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10090 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10091 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 10092 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10093 | EXPECT_THAT(rv, IsOk()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10094 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10095 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10096 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10097 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10098 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10099 | } |
| 10100 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10101 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10102 | // Tests that nonce count increments when multiple auth attempts |
| 10103 | // are started with the same nonce. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10104 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 10105 | HttpAuthHandlerDigest::Factory* digest_factory = |
| 10106 | new HttpAuthHandlerDigest::Factory(); |
| 10107 | HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = |
| 10108 | new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); |
| 10109 | digest_factory->set_nonce_generator(nonce_generator); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10110 | session_deps_.http_auth_handler_factory.reset(digest_factory); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10111 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10112 | |
| 10113 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 10114 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10115 | HttpRequestInfo request; |
| 10116 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10117 | request.url = GURL("http://www.example.org/x/y/z"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10118 | request.traffic_annotation = |
| 10119 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10120 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10121 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10122 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10123 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10124 | MockWrite( |
| 10125 | "GET /x/y/z HTTP/1.1\r\n" |
| 10126 | "Host: www.example.org\r\n" |
| 10127 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10128 | }; |
| 10129 | |
| 10130 | MockRead data_reads1[] = { |
| 10131 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 10132 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 10133 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10134 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10135 | }; |
| 10136 | |
| 10137 | // Resend with authorization (username=foo, password=bar) |
| 10138 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10139 | MockWrite( |
| 10140 | "GET /x/y/z HTTP/1.1\r\n" |
| 10141 | "Host: www.example.org\r\n" |
| 10142 | "Connection: keep-alive\r\n" |
| 10143 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 10144 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 10145 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 10146 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10147 | }; |
| 10148 | |
| 10149 | // Sever accepts the authorization. |
| 10150 | MockRead data_reads2[] = { |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 10151 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10152 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10153 | }; |
| 10154 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10155 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 10156 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10157 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 10158 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10159 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10160 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10161 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10162 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10163 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10164 | |
| 10165 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10166 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10167 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10168 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10169 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10170 | EXPECT_TRUE(CheckDigestServerAuth(response->auth_challenge)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10171 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10172 | TestCompletionCallback callback2; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10173 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10174 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 10175 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10176 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10177 | |
| 10178 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10179 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10180 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10181 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10182 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10183 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10184 | } |
| 10185 | |
| 10186 | // ------------------------------------------------------------------------ |
| 10187 | |
| 10188 | // Transaction 2: Request another resource in digestive's protection space. |
| 10189 | // This will preemptively add an Authorization header which should have an |
| 10190 | // "nc" value of 2 (as compared to 1 in the first use. |
| 10191 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10192 | HttpRequestInfo request; |
| 10193 | request.method = "GET"; |
| 10194 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 10195 | // protection space as digest. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10196 | request.url = GURL("http://www.example.org/x/y/a/b"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10197 | request.traffic_annotation = |
| 10198 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10199 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10200 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10201 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10202 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10203 | MockWrite( |
| 10204 | "GET /x/y/a/b HTTP/1.1\r\n" |
| 10205 | "Host: www.example.org\r\n" |
| 10206 | "Connection: keep-alive\r\n" |
| 10207 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 10208 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 10209 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 10210 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10211 | }; |
| 10212 | |
| 10213 | // Sever accepts the authorization. |
| 10214 | MockRead data_reads1[] = { |
| 10215 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10216 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10217 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10218 | }; |
| 10219 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10220 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10221 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10222 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10223 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10224 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10225 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10226 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10227 | |
| 10228 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10229 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10230 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10231 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10232 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10233 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 10234 | } |
| 10235 | } |
| 10236 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10237 | // Test the ResetStateForRestart() private method. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10238 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10239 | // Create a transaction (the dependencies aren't important). |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10240 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10241 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10242 | |
| 10243 | // Setup some state (which we expect ResetStateForRestart() will clear). |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 10244 | trans.read_buf_ = base::MakeRefCounted<IOBuffer>(15); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10245 | trans.read_buf_len_ = 15; |
| 10246 | trans.request_headers_.SetHeader("Authorization", "NTLM"); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10247 | |
| 10248 | // Setup state in response_ |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10249 | HttpResponseInfo* response = &trans.response_; |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10250 | response->auth_challenge = base::nullopt; |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 10251 | response->ssl_info.cert_status = static_cast<CertStatus>(-1); // Nonsensical. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 10252 | response->response_time = base::Time::Now(); |
| 10253 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10254 | |
| 10255 | { // Setup state for response_.vary_data |
| 10256 | HttpRequestInfo request; |
| 10257 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 10258 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 10259 | scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 10260 | request.extra_headers.SetHeader("Foo", "1"); |
| 10261 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10262 | EXPECT_TRUE(response->vary_data.Init(request, *headers.get())); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10263 | } |
| 10264 | |
| 10265 | // Cause the above state to be reset. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10266 | trans.ResetStateForRestart(); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10267 | |
| 10268 | // Verify that the state that needed to be reset, has been reset. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10269 | EXPECT_FALSE(trans.read_buf_); |
| 10270 | EXPECT_EQ(0, trans.read_buf_len_); |
| 10271 | EXPECT_TRUE(trans.request_headers_.IsEmpty()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10272 | EXPECT_FALSE(response->auth_challenge.has_value()); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10273 | EXPECT_FALSE(response->headers); |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 10274 | EXPECT_FALSE(response->was_cached); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 10275 | EXPECT_EQ(0U, response->ssl_info.cert_status); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 10276 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 10277 | } |
| 10278 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10279 | // Test HTTPS connections to a site with a bad certificate |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10280 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10281 | HttpRequestInfo request; |
| 10282 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10283 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10284 | request.traffic_annotation = |
| 10285 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10286 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10287 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10288 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 10289 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10290 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10291 | MockWrite( |
| 10292 | "GET / HTTP/1.1\r\n" |
| 10293 | "Host: www.example.org\r\n" |
| 10294 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10295 | }; |
| 10296 | |
| 10297 | MockRead data_reads[] = { |
| 10298 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10299 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10300 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10301 | MockRead(SYNCHRONOUS, OK), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10302 | }; |
| 10303 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 10304 | StaticSocketDataProvider ssl_bad_certificate; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10305 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10306 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 10307 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10308 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10309 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 10310 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10311 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
| 10312 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10313 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10314 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10315 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10316 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10317 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10318 | |
| 10319 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10320 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10321 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10322 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10323 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10324 | |
| 10325 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10326 | EXPECT_THAT(rv, IsOk()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10327 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10328 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10329 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10330 | ASSERT_TRUE(response); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10331 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10332 | } |
| 10333 | |
| 10334 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 10335 | // proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10336 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10337 | session_deps_.proxy_resolution_service = |
| 10338 | ConfiguredProxyResolutionService::CreateFixed( |
| 10339 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10340 | |
| 10341 | HttpRequestInfo request; |
| 10342 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10343 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10344 | request.traffic_annotation = |
| 10345 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10346 | |
| 10347 | MockWrite proxy_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10348 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10349 | "Host: www.example.org:443\r\n" |
| 10350 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10351 | }; |
| 10352 | |
| 10353 | MockRead proxy_reads[] = { |
| 10354 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10355 | MockRead(SYNCHRONOUS, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10356 | }; |
| 10357 | |
| 10358 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10359 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10360 | "Host: www.example.org:443\r\n" |
| 10361 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10362 | MockWrite("GET / HTTP/1.1\r\n" |
| 10363 | "Host: www.example.org\r\n" |
| 10364 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10365 | }; |
| 10366 | |
| 10367 | MockRead data_reads[] = { |
| 10368 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 10369 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 10370 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10371 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10372 | MockRead(SYNCHRONOUS, OK), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10373 | }; |
| 10374 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10375 | StaticSocketDataProvider ssl_bad_certificate(proxy_reads, proxy_writes); |
| 10376 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10377 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 10378 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10379 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10380 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 10381 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10382 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
| 10383 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10384 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10385 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10386 | |
| 10387 | for (int i = 0; i < 2; i++) { |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10388 | session_deps_.socket_factory->ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10389 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10390 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10391 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10392 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10393 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10394 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10395 | |
| 10396 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10397 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10398 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10399 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10400 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10401 | |
| 10402 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10403 | EXPECT_THAT(rv, IsOk()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10404 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10405 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10406 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10407 | ASSERT_TRUE(response); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10408 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10409 | } |
| 10410 | } |
| 10411 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10412 | |
| 10413 | // Test HTTPS connections to a site, going through an HTTPS proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10414 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10415 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10416 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10417 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10418 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10419 | session_deps_.net_log = &net_log; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10420 | |
| 10421 | HttpRequestInfo request; |
| 10422 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10423 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10424 | request.traffic_annotation = |
| 10425 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10426 | |
| 10427 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10428 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10429 | "Host: www.example.org:443\r\n" |
| 10430 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10431 | MockWrite("GET / HTTP/1.1\r\n" |
| 10432 | "Host: www.example.org\r\n" |
| 10433 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10434 | }; |
| 10435 | |
| 10436 | MockRead data_reads[] = { |
| 10437 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 10438 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 10439 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 10440 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10441 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10442 | }; |
| 10443 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10444 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10445 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10446 | SSLSocketDataProvider tunnel_ssl(ASYNC, OK); // SSL through the tunnel |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10447 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10448 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10449 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10450 | session_deps_.socket_factory->AddSSLSocketDataProvider(&tunnel_ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10451 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10452 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10453 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10454 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10455 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10456 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10457 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10458 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10459 | |
| 10460 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10461 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10462 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10463 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10464 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10465 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 10466 | EXPECT_TRUE(response->proxy_server.is_https()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10467 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 10468 | EXPECT_EQ(200, response->headers->response_code()); |
| 10469 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 10470 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10471 | |
| 10472 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10473 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10474 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 10475 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 10476 | } |
| 10477 | |
Eric Roman | 695a787 | 2019-04-16 21:53:29 | [diff] [blame] | 10478 | // Test that an HTTPS Proxy cannot redirect a CONNECT request for main frames. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10479 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaHttpsProxy) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10480 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10481 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10482 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10483 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10484 | session_deps_.net_log = &net_log; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10485 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10486 | const base::TimeDelta kTimeIncrement = base::TimeDelta::FromSeconds(4); |
| 10487 | session_deps_.host_resolver->set_ondemand_mode(true); |
| 10488 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10489 | HttpRequestInfo request; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10490 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10491 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10492 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10493 | request.traffic_annotation = |
| 10494 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10495 | |
| 10496 | MockWrite data_writes[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10497 | MockWrite(ASYNC, 0, |
| 10498 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10499 | "Host: www.example.org:443\r\n" |
| 10500 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10501 | }; |
| 10502 | |
| 10503 | MockRead data_reads[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10504 | // Pause on first read. |
| 10505 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 10506 | MockRead(ASYNC, 2, "HTTP/1.1 302 Redirect\r\n"), |
| 10507 | MockRead(ASYNC, 3, "Location: http://login.example.com/\r\n"), |
| 10508 | MockRead(ASYNC, 4, "Content-Length: 0\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10509 | }; |
| 10510 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10511 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10512 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10513 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10514 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10515 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10516 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10517 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10518 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10519 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10520 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10521 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10522 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10523 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10524 | EXPECT_TRUE(session_deps_.host_resolver->has_pending_requests()); |
| 10525 | |
| 10526 | // Host resolution takes |kTimeIncrement|. |
| 10527 | FastForwardBy(kTimeIncrement); |
| 10528 | // Resolving the current request with |ResolveNow| will cause the pending |
| 10529 | // request to instantly complete, and the async connect will start as well. |
| 10530 | session_deps_.host_resolver->ResolveOnlyRequestNow(); |
| 10531 | |
| 10532 | // Connecting takes |kTimeIncrement|. |
| 10533 | FastForwardBy(kTimeIncrement); |
| 10534 | data.RunUntilPaused(); |
| 10535 | |
| 10536 | // The server takes |kTimeIncrement| to respond. |
| 10537 | FastForwardBy(kTimeIncrement); |
| 10538 | data.Resume(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10539 | |
| 10540 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10541 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10542 | } |
| 10543 | |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10544 | // Test that an HTTPS Proxy cannot redirect a CONNECT request for subresources. |
| 10545 | TEST_F(HttpNetworkTransactionTest, |
| 10546 | RedirectOfHttpsConnectSubresourceViaHttpsProxy) { |
| 10547 | base::HistogramTester histograms; |
| 10548 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10549 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10550 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10551 | RecordingTestNetLog net_log; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10552 | session_deps_.net_log = &net_log; |
| 10553 | |
| 10554 | HttpRequestInfo request; |
| 10555 | request.method = "GET"; |
| 10556 | request.url = GURL("https://www.example.org/"); |
| 10557 | request.traffic_annotation = |
| 10558 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10559 | |
| 10560 | MockWrite data_writes[] = { |
| 10561 | MockWrite(ASYNC, 0, |
| 10562 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10563 | "Host: www.example.org:443\r\n" |
| 10564 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10565 | }; |
| 10566 | |
| 10567 | MockRead data_reads[] = { |
| 10568 | MockRead(ASYNC, 1, "HTTP/1.1 302 Redirect\r\n"), |
| 10569 | MockRead(ASYNC, 2, "Location: http://login.example.com/\r\n"), |
| 10570 | MockRead(ASYNC, 3, "Content-Length: 0\r\n\r\n"), |
| 10571 | }; |
| 10572 | |
| 10573 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
| 10574 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10575 | |
| 10576 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10577 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10578 | |
| 10579 | TestCompletionCallback callback; |
| 10580 | |
| 10581 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 10582 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 10583 | |
| 10584 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 10585 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 10586 | |
| 10587 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10588 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10589 | } |
| 10590 | |
| 10591 | // Test that an HTTPS Proxy which was auto-detected cannot redirect a CONNECT |
| 10592 | // request for main frames. |
| 10593 | TEST_F(HttpNetworkTransactionTest, |
| 10594 | RedirectOfHttpsConnectViaAutoDetectedHttpsProxy) { |
| 10595 | base::HistogramTester histograms; |
| 10596 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10597 | ConfiguredProxyResolutionService::CreateFixedFromAutoDetectedPacResult( |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10598 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10599 | RecordingTestNetLog net_log; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10600 | session_deps_.net_log = &net_log; |
| 10601 | |
| 10602 | HttpRequestInfo request; |
| 10603 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
| 10604 | request.method = "GET"; |
| 10605 | request.url = GURL("https://www.example.org/"); |
| 10606 | request.traffic_annotation = |
| 10607 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 10608 | |
| 10609 | MockWrite data_writes[] = { |
| 10610 | MockWrite(ASYNC, 0, |
| 10611 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10612 | "Host: www.example.org:443\r\n" |
| 10613 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 10614 | }; |
| 10615 | |
| 10616 | MockRead data_reads[] = { |
| 10617 | MockRead(ASYNC, 1, "HTTP/1.1 302 Redirect\r\n"), |
| 10618 | MockRead(ASYNC, 2, "Location: http://login.example.com/\r\n"), |
| 10619 | MockRead(ASYNC, 3, "Content-Length: 0\r\n\r\n"), |
| 10620 | }; |
| 10621 | |
| 10622 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
| 10623 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
| 10624 | |
| 10625 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10626 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
| 10627 | |
| 10628 | TestCompletionCallback callback; |
| 10629 | |
| 10630 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 10631 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 10632 | |
| 10633 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 10634 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 10635 | |
| 10636 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10637 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10638 | } |
| 10639 | |
Eric Roman | 695a787 | 2019-04-16 21:53:29 | [diff] [blame] | 10640 | // Tests that an HTTPS (SPDY) Proxy's cannot redirect a CONNECT request for main |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10641 | // frames. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10642 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaSpdyProxy) { |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10643 | base::HistogramTester histograms; |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10644 | session_deps_.proxy_resolution_service = |
| 10645 | ConfiguredProxyResolutionService::CreateFixed( |
| 10646 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10647 | RecordingTestNetLog net_log; |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10648 | session_deps_.net_log = &net_log; |
| 10649 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10650 | const base::TimeDelta kTimeIncrement = base::TimeDelta::FromSeconds(4); |
| 10651 | session_deps_.host_resolver->set_ondemand_mode(true); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10652 | |
| 10653 | HttpRequestInfo request; |
| 10654 | request.method = "GET"; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 10655 | request.load_flags = LOAD_MAIN_FRAME_DEPRECATED; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10656 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10657 | request.traffic_annotation = |
| 10658 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10659 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10660 | spdy::SpdySerializedFrame conn(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10661 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10662 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10663 | spdy::SpdySerializedFrame goaway( |
| 10664 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10665 | MockWrite data_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10666 | CreateMockWrite(conn, 0, SYNCHRONOUS), |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10667 | CreateMockWrite(goaway, 3, SYNCHRONOUS), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10668 | }; |
| 10669 | |
| 10670 | static const char* const kExtraHeaders[] = { |
| 10671 | "location", |
| 10672 | "http://login.example.com/", |
| 10673 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10674 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10675 | "302", kExtraHeaders, base::size(kExtraHeaders) / 2, 1)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10676 | MockRead data_reads[] = { |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10677 | // Pause on first read. |
| 10678 | MockRead(ASYNC, ERR_IO_PENDING, 1), CreateMockRead(resp, 2), |
| 10679 | MockRead(ASYNC, 0, 4), // EOF |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10680 | }; |
| 10681 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10682 | SequencedSocketData data(MockConnect(ASYNC, OK), data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10683 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10684 | proxy_ssl.next_proto = kProtoHTTP2; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10685 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10686 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10687 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10688 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10689 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10690 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10691 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10692 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10693 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10694 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10695 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10696 | EXPECT_TRUE(session_deps_.host_resolver->has_pending_requests()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10697 | |
Matt Menke | ecfecfc7 | 2019-02-05 19:15:28 | [diff] [blame] | 10698 | // Host resolution takes |kTimeIncrement|. |
| 10699 | FastForwardBy(kTimeIncrement); |
| 10700 | // Resolving the current request with |ResolveNow| will cause the pending |
| 10701 | // request to instantly complete, and the async connect will start as well. |
| 10702 | session_deps_.host_resolver->ResolveOnlyRequestNow(); |
| 10703 | |
| 10704 | // Connecting takes |kTimeIncrement|. |
| 10705 | FastForwardBy(kTimeIncrement); |
| 10706 | data.RunUntilPaused(); |
| 10707 | |
| 10708 | FastForwardBy(kTimeIncrement); |
| 10709 | data.Resume(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10710 | rv = callback.WaitForResult(); |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 10711 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10712 | } |
| 10713 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 10714 | // Test that an HTTPS proxy's response to a CONNECT request is filtered. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10715 | TEST_F(HttpNetworkTransactionTest, ErrorResponseToHttpsConnectViaHttpsProxy) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10716 | session_deps_.proxy_resolution_service = |
| 10717 | ConfiguredProxyResolutionService::CreateFixed( |
| 10718 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10719 | |
| 10720 | HttpRequestInfo request; |
| 10721 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10722 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10723 | request.traffic_annotation = |
| 10724 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10725 | |
| 10726 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 10727 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 10728 | "Host: www.example.org:443\r\n" |
| 10729 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10730 | }; |
| 10731 | |
| 10732 | MockRead data_reads[] = { |
| 10733 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 10734 | MockRead("Content-Length: 23\r\n\r\n"), |
| 10735 | MockRead("The host does not exist"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10736 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10737 | }; |
| 10738 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10739 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10740 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10741 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10742 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10743 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10744 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10745 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10746 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10747 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10748 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10749 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10750 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10751 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10752 | |
| 10753 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10754 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10755 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 10756 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10757 | } |
| 10758 | |
[email protected] | 4eddbc73 | 2012-08-09 05:40:17 | [diff] [blame] | 10759 | // Test that a SPDY proxy's response to a CONNECT request is filtered. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10760 | TEST_F(HttpNetworkTransactionTest, ErrorResponseToHttpsConnectViaSpdyProxy) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10761 | session_deps_.proxy_resolution_service = |
| 10762 | ConfiguredProxyResolutionService::CreateFixed( |
| 10763 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10764 | |
| 10765 | HttpRequestInfo request; |
| 10766 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10767 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10768 | request.traffic_annotation = |
| 10769 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10770 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10771 | spdy::SpdySerializedFrame conn(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10772 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10773 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10774 | spdy::SpdySerializedFrame rst( |
| 10775 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10776 | MockWrite data_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10777 | CreateMockWrite(conn, 0), CreateMockWrite(rst, 3), |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10778 | }; |
| 10779 | |
| 10780 | static const char* const kExtraHeaders[] = { |
| 10781 | "location", |
| 10782 | "http://login.example.com/", |
| 10783 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10784 | spdy::SpdySerializedFrame resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10785 | "404", kExtraHeaders, base::size(kExtraHeaders) / 2, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10786 | spdy::SpdySerializedFrame body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10787 | spdy_util_.ConstructSpdyDataFrame(1, "The host does not exist", true)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10788 | MockRead data_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10789 | CreateMockRead(resp, 1), CreateMockRead(body, 2), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 10790 | MockRead(ASYNC, 0, 4), // EOF |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10791 | }; |
| 10792 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10793 | SequencedSocketData data(data_reads, data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 10794 | SSLSocketDataProvider proxy_ssl(ASYNC, OK); // SSL to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10795 | proxy_ssl.next_proto = kProtoHTTP2; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10796 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10797 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 10798 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy_ssl); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10799 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10800 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10801 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10802 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10803 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10804 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 10805 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10806 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10807 | |
| 10808 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10809 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10810 | |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 10811 | // TODO(juliatuttle): Anything else to check here? |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 10812 | } |
| 10813 | |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10814 | // Test the request-challenge-retry sequence for basic auth, through |
| 10815 | // a SPDY proxy over a single SPDY session. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10816 | TEST_F(HttpNetworkTransactionTest, BasicAuthSpdyProxy) { |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10817 | HttpRequestInfo request; |
| 10818 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10819 | request.url = GURL("https://www.example.org/"); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10820 | // when the no authentication data flag is set. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 10821 | request.load_flags = LOAD_DO_NOT_SEND_AUTH_DATA; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10822 | request.traffic_annotation = |
| 10823 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10824 | |
| 10825 | // Configure against https proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10826 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10827 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10828 | "HTTPS myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10829 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10830 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10831 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10832 | |
| 10833 | // Since we have proxy, should try to establish tunnel. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10834 | spdy::SpdySerializedFrame req(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10835 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 10836 | HostPortPair("www.example.org", 443))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10837 | spdy::SpdySerializedFrame rst( |
| 10838 | spdy_util_.ConstructSpdyRstStream(1, spdy::ERROR_CODE_CANCEL)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 10839 | spdy_util_.UpdateWithStreamDestruction(1); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10840 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 10841 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10842 | // be issuing -- the final header line contains the credentials. |
| 10843 | const char* const kAuthCredentials[] = { |
| 10844 | "proxy-authorization", "Basic Zm9vOmJhcg==", |
| 10845 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10846 | spdy::SpdySerializedFrame connect2(spdy_util_.ConstructSpdyConnect( |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 10847 | kAuthCredentials, base::size(kAuthCredentials) / 2, 3, |
| 10848 | HttpProxyConnectJob::kH2QuicTunnelPriority, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10849 | HostPortPair("www.example.org", 443))); |
| 10850 | // fetch https://www.example.org/ via HTTP |
| 10851 | const char get[] = |
| 10852 | "GET / HTTP/1.1\r\n" |
| 10853 | "Host: www.example.org\r\n" |
| 10854 | "Connection: keep-alive\r\n\r\n"; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10855 | spdy::SpdySerializedFrame wrapped_get( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10856 | spdy_util_.ConstructSpdyDataFrame(3, get, false)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10857 | |
| 10858 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10859 | CreateMockWrite(req, 0, ASYNC), CreateMockWrite(rst, 2, ASYNC), |
| 10860 | CreateMockWrite(connect2, 3), CreateMockWrite(wrapped_get, 5), |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10861 | }; |
| 10862 | |
| 10863 | // The proxy responds to the connect with a 407, using a persistent |
| 10864 | // connection. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 10865 | const char kAuthStatus[] = "407"; |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10866 | const char* const kAuthChallenge[] = { |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10867 | "proxy-authenticate", "Basic realm=\"MyRealm1\"", |
| 10868 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10869 | spdy::SpdySerializedFrame conn_auth_resp(spdy_util_.ConstructSpdyReplyError( |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 10870 | kAuthStatus, kAuthChallenge, base::size(kAuthChallenge) / 2, 1)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10871 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10872 | spdy::SpdySerializedFrame conn_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10873 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10874 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 10875 | "Content-Length: 5\r\n\r\n"; |
| 10876 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10877 | spdy::SpdySerializedFrame wrapped_get_resp( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10878 | spdy_util_.ConstructSpdyDataFrame(3, resp, false)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10879 | spdy::SpdySerializedFrame wrapped_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 10880 | spdy_util_.ConstructSpdyDataFrame(3, "hello", false)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10881 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10882 | CreateMockRead(conn_auth_resp, 1, ASYNC), |
| 10883 | CreateMockRead(conn_resp, 4, ASYNC), |
| 10884 | CreateMockRead(wrapped_get_resp, 6, ASYNC), |
| 10885 | CreateMockRead(wrapped_body, 7, ASYNC), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 10886 | MockRead(ASYNC, OK, 8), // EOF. May or may not be read. |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10887 | }; |
| 10888 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 10889 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10890 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10891 | // Negotiate SPDY to the proxy |
| 10892 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 10893 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10894 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10895 | // Vanilla SSL to the server |
| 10896 | SSLSocketDataProvider server(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10897 | session_deps_.socket_factory->AddSSLSocketDataProvider(&server); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10898 | |
| 10899 | TestCompletionCallback callback1; |
| 10900 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 10901 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10902 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10903 | |
| 10904 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10905 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10906 | |
| 10907 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10908 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 10909 | auto entries = log.GetEntries(); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10910 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 10911 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 10912 | NetLogEventPhase::NONE); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10913 | ExpectLogContainsSomewhere( |
| 10914 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 10915 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 10916 | NetLogEventPhase::NONE); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10917 | |
| 10918 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10919 | ASSERT_TRUE(response); |
| 10920 | ASSERT_TRUE(response->headers); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10921 | EXPECT_EQ(407, response->headers->response_code()); |
| 10922 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10923 | EXPECT_TRUE(response->auth_challenge.has_value()); |
| 10924 | EXPECT_TRUE(CheckBasicSecureProxyAuth(response->auth_challenge)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10925 | |
| 10926 | TestCompletionCallback callback2; |
| 10927 | |
| 10928 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 10929 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10930 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10931 | |
| 10932 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 10933 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10934 | |
| 10935 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 10936 | ASSERT_TRUE(response); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10937 | |
| 10938 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 10939 | EXPECT_EQ(200, response->headers->response_code()); |
| 10940 | EXPECT_EQ(5, response->headers->GetContentLength()); |
| 10941 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 10942 | |
| 10943 | // The password prompt info should not be set. |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 10944 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10945 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 10946 | LoadTimingInfo load_timing_info; |
| 10947 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 10948 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 10949 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 10950 | |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10951 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 10952 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 0c5fb72 | 2012-02-28 11:50:35 | [diff] [blame] | 10953 | } |
| 10954 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10955 | // Test that an explicitly trusted SPDY proxy can push a resource from an |
| 10956 | // origin that is different from that of its associated resource. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 10957 | TEST_F(HttpNetworkTransactionTest, CrossOriginSPDYProxyPush) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10958 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 10959 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10960 | proxy_delegate->set_trusted_spdy_proxy(net::ProxyServer::FromURI( |
| 10961 | "https://myproxy:443", net::ProxyServer::SCHEME_HTTP)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10962 | HttpRequestInfo request; |
| 10963 | HttpRequestInfo push_request; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10964 | request.traffic_annotation = |
| 10965 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10966 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10967 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 10968 | request.url = GURL("http://www.example.org/"); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10969 | push_request.method = "GET"; |
| 10970 | push_request.url = GURL("http://www.another-origin.com/foo.dat"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 10971 | push_request.traffic_annotation = |
| 10972 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10973 | |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 10974 | // Configure against https proxy server "myproxy:443". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10975 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 10976 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 10977 | "HTTPS myproxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 10978 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 10979 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10980 | |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 10981 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 10982 | proxy_delegate.get()); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 10983 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10984 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10985 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10986 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 10987 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10988 | spdy::SpdySerializedFrame stream2_priority( |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 10989 | spdy_util_.ConstructSpdyPriority(2, 1, IDLE, true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10990 | |
| 10991 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 10992 | CreateMockWrite(stream1_syn, 0, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 10993 | CreateMockWrite(stream2_priority, 3, ASYNC), |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 10994 | }; |
| 10995 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10996 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10997 | nullptr, 0, 2, 1, "http://www.another-origin.com/foo.dat")); |
Bence Béky | 7bf9436 | 2018-01-10 13:19:36 | [diff] [blame] | 10998 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 10999 | spdy::SpdySerializedFrame stream1_reply( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 11000 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11001 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11002 | spdy::SpdySerializedFrame stream1_body( |
| 11003 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11004 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11005 | spdy::SpdySerializedFrame stream2_body( |
Bence Béky | d74f438 | 2018-02-20 18:26:19 | [diff] [blame] | 11006 | spdy_util_.ConstructSpdyDataFrame(2, "pushed", true)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11007 | |
| 11008 | MockRead spdy_reads[] = { |
Bence Béky | 7bf9436 | 2018-01-10 13:19:36 | [diff] [blame] | 11009 | CreateMockRead(stream2_syn, 1, ASYNC), |
| 11010 | CreateMockRead(stream1_reply, 2, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11011 | CreateMockRead(stream1_body, 4, ASYNC), |
| 11012 | CreateMockRead(stream2_body, 5, ASYNC), |
| 11013 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), // Force a hang |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11014 | }; |
| 11015 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11016 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11017 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11018 | // Negotiate SPDY to the proxy |
| 11019 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 11020 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11021 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11022 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11023 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11024 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11025 | TestCompletionCallback callback; |
| 11026 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11027 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11028 | |
| 11029 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11030 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11031 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 11032 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11033 | auto push_trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11034 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 11035 | rv = push_trans->Start(&push_request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11036 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11037 | |
| 11038 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11039 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11040 | const HttpResponseInfo* push_response = push_trans->GetResponseInfo(); |
| 11041 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11042 | ASSERT_TRUE(response); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11043 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 11044 | |
| 11045 | EXPECT_EQ(200, response->headers->response_code()); |
| 11046 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 11047 | |
| 11048 | std::string response_data; |
| 11049 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11050 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11051 | EXPECT_EQ("hello!", response_data); |
| 11052 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11053 | LoadTimingInfo load_timing_info; |
| 11054 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 11055 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11056 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11057 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11058 | // Verify the pushed stream. |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11059 | EXPECT_TRUE(push_response->headers); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11060 | EXPECT_EQ(200, push_response->headers->response_code()); |
| 11061 | |
| 11062 | rv = ReadTransaction(push_trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11063 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11064 | EXPECT_EQ("pushed", response_data); |
| 11065 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11066 | LoadTimingInfo push_load_timing_info; |
| 11067 | EXPECT_TRUE(push_trans->GetLoadTimingInfo(&push_load_timing_info)); |
| 11068 | TestLoadTimingReusedWithPac(push_load_timing_info); |
| 11069 | // The transactions should share a socket ID, despite being for different |
| 11070 | // origins. |
| 11071 | EXPECT_EQ(load_timing_info.socket_log_id, |
| 11072 | push_load_timing_info.socket_log_id); |
| 11073 | |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11074 | trans.reset(); |
| 11075 | push_trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 11076 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 7c6f7ba | 2012-04-03 04:09:29 | [diff] [blame] | 11077 | } |
| 11078 | |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11079 | // Test that an explicitly trusted SPDY proxy cannot push HTTPS content. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11080 | TEST_F(HttpNetworkTransactionTest, CrossOriginProxyPushCorrectness) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11081 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11082 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11083 | proxy_delegate->set_trusted_spdy_proxy(net::ProxyServer::FromURI( |
| 11084 | "https://myproxy:443", net::ProxyServer::SCHEME_HTTP)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11085 | HttpRequestInfo request; |
| 11086 | |
| 11087 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11088 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11089 | request.traffic_annotation = |
| 11090 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11091 | |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11092 | session_deps_.proxy_resolution_service = |
| 11093 | ConfiguredProxyResolutionService::CreateFixed( |
| 11094 | "https://myproxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11095 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11096 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 11097 | |
| 11098 | // Enable cross-origin push. |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 11099 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 11100 | proxy_delegate.get()); |
[email protected] | 61b4efc | 2012-04-27 18:12:50 | [diff] [blame] | 11101 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11102 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11103 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11104 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 11105 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11106 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11107 | spdy::SpdySerializedFrame push_rst( |
| 11108 | spdy_util_.ConstructSpdyRstStream(2, spdy::ERROR_CODE_REFUSED_STREAM)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11109 | |
| 11110 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11111 | CreateMockWrite(stream1_syn, 0, ASYNC), CreateMockWrite(push_rst, 3), |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11112 | }; |
| 11113 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11114 | spdy::SpdySerializedFrame stream1_reply( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 11115 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11116 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11117 | spdy::SpdySerializedFrame stream1_body( |
| 11118 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11119 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11120 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 11121 | nullptr, 0, 2, 1, "https://www.another-origin.com/foo.dat")); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11122 | |
| 11123 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11124 | CreateMockRead(stream1_reply, 1, ASYNC), |
| 11125 | CreateMockRead(stream2_syn, 2, ASYNC), |
| 11126 | CreateMockRead(stream1_body, 4, ASYNC), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 11127 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 5), // Force a hang |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11128 | }; |
| 11129 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11130 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11131 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11132 | // Negotiate SPDY to the proxy |
| 11133 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 11134 | proxy.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11135 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11136 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11137 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11138 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11139 | TestCompletionCallback callback; |
| 11140 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11141 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11142 | |
| 11143 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11144 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11145 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 11146 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11147 | ASSERT_TRUE(response); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11148 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 11149 | |
| 11150 | EXPECT_EQ(200, response->headers->response_code()); |
| 11151 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 11152 | |
| 11153 | std::string response_data; |
| 11154 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11155 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11156 | EXPECT_EQ("hello!", response_data); |
| 11157 | |
| 11158 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 11159 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
[email protected] | 8c84319 | 2012-04-05 07:15:00 | [diff] [blame] | 11160 | } |
| 11161 | |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11162 | // Test that an explicitly trusted SPDY proxy can push same-origin HTTPS |
| 11163 | // resources. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11164 | TEST_F(HttpNetworkTransactionTest, SameOriginProxyPushCorrectness) { |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11165 | // Configure the proxy delegate to allow cross-origin SPDY pushes. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11166 | auto proxy_delegate = std::make_unique<TestProxyDelegate>(); |
tbansal | 28e68f8 | 2016-02-04 02:56:15 | [diff] [blame] | 11167 | proxy_delegate->set_trusted_spdy_proxy( |
| 11168 | net::ProxyServer::FromURI("myproxy:70", net::ProxyServer::SCHEME_HTTP)); |
| 11169 | |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11170 | HttpRequestInfo request; |
| 11171 | |
| 11172 | request.method = "GET"; |
| 11173 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11174 | request.traffic_annotation = |
| 11175 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11176 | |
| 11177 | // Configure against https proxy server "myproxy:70". |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11178 | session_deps_.proxy_resolution_service = |
| 11179 | ConfiguredProxyResolutionService::CreateFixed( |
| 11180 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11181 | RecordingBoundTestNetLog log; |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11182 | session_deps_.net_log = log.bound().net_log(); |
| 11183 | |
| 11184 | // Enable cross-origin push. |
Eric Roman | 3d8546a | 2018-09-10 17:00:52 | [diff] [blame] | 11185 | session_deps_.proxy_resolution_service->SetProxyDelegate( |
| 11186 | proxy_delegate.get()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11187 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11188 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11189 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11190 | spdy::SpdySerializedFrame stream1_syn( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 11191 | spdy_util_.ConstructSpdyGet("http://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11192 | spdy::SpdySerializedFrame stream2_priority( |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11193 | spdy_util_.ConstructSpdyPriority(2, 1, IDLE, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11194 | |
| 11195 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11196 | CreateMockWrite(stream1_syn, 0, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11197 | CreateMockWrite(stream2_priority, 3, ASYNC), |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11198 | }; |
| 11199 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11200 | spdy::SpdySerializedFrame stream1_reply( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 11201 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11202 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11203 | spdy::SpdySerializedFrame stream2_syn(spdy_util_.ConstructSpdyPush( |
Bence Béky | 096cf05 | 2017-08-08 23:55:33 | [diff] [blame] | 11204 | nullptr, 0, 2, 1, "http://www.example.org/foo.dat")); |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 11205 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11206 | spdy::SpdySerializedFrame stream1_body( |
| 11207 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11208 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11209 | spdy::SpdySerializedFrame stream2_reply( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 11210 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11211 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 11212 | spdy::SpdySerializedFrame stream2_body( |
| 11213 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11214 | |
| 11215 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 11216 | CreateMockRead(stream1_reply, 1, ASYNC), |
| 11217 | CreateMockRead(stream2_syn, 2, ASYNC), |
tombergan | 5d22c18 | 2017-01-11 02:05:35 | [diff] [blame] | 11218 | CreateMockRead(stream1_body, 4, ASYNC), |
| 11219 | CreateMockRead(stream2_body, 5, ASYNC), |
| 11220 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), // Force a hang |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11221 | }; |
| 11222 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11223 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11224 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 11225 | // Negotiate SPDY to the proxy |
| 11226 | SSLSocketDataProvider proxy(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 11227 | proxy.next_proto = kProtoHTTP2; |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11228 | session_deps_.socket_factory->AddSSLSocketDataProvider(&proxy); |
| 11229 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 11230 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 11231 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11232 | TestCompletionCallback callback; |
| 11233 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11234 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11235 | |
| 11236 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11237 | EXPECT_THAT(rv, IsOk()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11238 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 11239 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11240 | ASSERT_TRUE(response); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11241 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 11242 | |
| 11243 | EXPECT_EQ(200, response->headers->response_code()); |
| 11244 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 11245 | |
| 11246 | std::string response_data; |
| 11247 | rv = ReadTransaction(trans.get(), &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11248 | EXPECT_THAT(rv, IsOk()); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11249 | EXPECT_EQ("hello!", response_data); |
| 11250 | |
| 11251 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 11252 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
tbansal | 8ef1d3e | 2016-02-03 04:05:42 | [diff] [blame] | 11253 | } |
| 11254 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11255 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 11256 | // HTTPS proxy |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11257 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11258 | session_deps_.proxy_resolution_service = |
| 11259 | ConfiguredProxyResolutionService::CreateFixed( |
| 11260 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11261 | |
| 11262 | HttpRequestInfo request; |
| 11263 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11264 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11265 | request.traffic_annotation = |
| 11266 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11267 | |
| 11268 | // Attempt to fetch the URL from a server with a bad cert |
| 11269 | MockWrite bad_cert_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 11270 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11271 | "Host: www.example.org:443\r\n" |
| 11272 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11273 | }; |
| 11274 | |
| 11275 | MockRead bad_cert_reads[] = { |
| 11276 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11277 | MockRead(SYNCHRONOUS, OK) |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11278 | }; |
| 11279 | |
| 11280 | // Attempt to fetch the URL with a good cert |
| 11281 | MockWrite good_data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 11282 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11283 | "Host: www.example.org:443\r\n" |
| 11284 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 11285 | MockWrite("GET / HTTP/1.1\r\n" |
| 11286 | "Host: www.example.org\r\n" |
| 11287 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11288 | }; |
| 11289 | |
| 11290 | MockRead good_cert_reads[] = { |
| 11291 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 11292 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11293 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11294 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11295 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11296 | }; |
| 11297 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11298 | StaticSocketDataProvider ssl_bad_certificate(bad_cert_reads, bad_cert_writes); |
| 11299 | StaticSocketDataProvider data(good_cert_reads, good_data_writes); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11300 | SSLSocketDataProvider ssl_bad(ASYNC, ERR_CERT_AUTHORITY_INVALID); |
| 11301 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11302 | |
| 11303 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11304 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11305 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_bad_certificate); |
| 11306 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bad); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11307 | |
| 11308 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11309 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 11310 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 11311 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11312 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11313 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11314 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11315 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11316 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11317 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11318 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11319 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11320 | |
| 11321 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11322 | EXPECT_THAT(rv, IsError(ERR_CERT_AUTHORITY_INVALID)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11323 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11324 | rv = trans.RestartIgnoringLastError(callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11325 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11326 | |
| 11327 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11328 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11329 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11330 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11331 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11332 | ASSERT_TRUE(response); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 11333 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 11334 | } |
| 11335 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11336 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11337 | HttpRequestInfo request; |
| 11338 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11339 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11340 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 11341 | "Chromium Ultra Awesome X Edition"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11342 | request.traffic_annotation = |
| 11343 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11344 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11345 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11346 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11347 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11348 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11349 | MockWrite( |
| 11350 | "GET / HTTP/1.1\r\n" |
| 11351 | "Host: www.example.org\r\n" |
| 11352 | "Connection: keep-alive\r\n" |
| 11353 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11354 | }; |
| 11355 | |
| 11356 | // Lastly, the server responds with the actual content. |
| 11357 | MockRead data_reads[] = { |
| 11358 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11359 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11360 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11361 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11362 | }; |
| 11363 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11364 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11365 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11366 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11367 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11368 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11369 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11370 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11371 | |
| 11372 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11373 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11374 | } |
| 11375 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11376 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11377 | // Test user agent values, used both for the request header of the original |
| 11378 | // request, and the value returned by the HttpUserAgentSettings. nullptr means |
| 11379 | // no request header / no HttpUserAgentSettings object. |
| 11380 | const char* kTestUserAgents[] = {nullptr, "", "Foopy"}; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11381 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11382 | for (const char* setting_user_agent : kTestUserAgents) { |
| 11383 | if (!setting_user_agent) { |
| 11384 | session_deps_.http_user_agent_settings.reset(); |
| 11385 | } else { |
| 11386 | session_deps_.http_user_agent_settings = |
| 11387 | std::make_unique<StaticHttpUserAgentSettings>( |
| 11388 | std::string() /* accept-language */, setting_user_agent); |
| 11389 | } |
| 11390 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11391 | ConfiguredProxyResolutionService::CreateFixed( |
| 11392 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11393 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 11394 | for (const char* request_user_agent : kTestUserAgents) { |
| 11395 | HttpRequestInfo request; |
| 11396 | request.method = "GET"; |
| 11397 | request.url = GURL("https://www.example.org/"); |
| 11398 | if (request_user_agent) { |
| 11399 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 11400 | request_user_agent); |
| 11401 | } |
| 11402 | request.traffic_annotation = |
| 11403 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11404 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11405 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11406 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11407 | std::string expected_request; |
| 11408 | if (!setting_user_agent || strlen(setting_user_agent) == 0) { |
| 11409 | expected_request = |
| 11410 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11411 | "Host: www.example.org:443\r\n" |
| 11412 | "Proxy-Connection: keep-alive\r\n\r\n"; |
| 11413 | } else { |
| 11414 | expected_request = base::StringPrintf( |
| 11415 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 11416 | "Host: www.example.org:443\r\n" |
| 11417 | "Proxy-Connection: keep-alive\r\n" |
| 11418 | "User-Agent: %s\r\n\r\n", |
| 11419 | setting_user_agent); |
| 11420 | } |
| 11421 | MockWrite data_writes[] = { |
| 11422 | MockWrite(expected_request.c_str()), |
| 11423 | }; |
| 11424 | MockRead data_reads[] = { |
| 11425 | // Return an error, so the transaction stops here (this test isn't |
| 11426 | // interested in the rest). |
| 11427 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 11428 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 11429 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 11430 | }; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11431 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11432 | StaticSocketDataProvider data(data_reads, data_writes); |
| 11433 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11434 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11435 | TestCompletionCallback callback; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11436 | |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 11437 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 11438 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 11439 | |
| 11440 | rv = callback.WaitForResult(); |
| 11441 | EXPECT_THAT(rv, IsOk()); |
| 11442 | } |
| 11443 | } |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 11444 | } |
| 11445 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11446 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11447 | HttpRequestInfo request; |
| 11448 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11449 | request.url = GURL("http://www.example.org/"); |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 11450 | request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
| 11451 | "http://the.previous.site.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11452 | request.traffic_annotation = |
| 11453 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11454 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11455 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11456 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11457 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11458 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11459 | MockWrite( |
| 11460 | "GET / HTTP/1.1\r\n" |
| 11461 | "Host: www.example.org\r\n" |
| 11462 | "Connection: keep-alive\r\n" |
| 11463 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11464 | }; |
| 11465 | |
| 11466 | // Lastly, the server responds with the actual content. |
| 11467 | MockRead data_reads[] = { |
| 11468 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11469 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11470 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11471 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11472 | }; |
| 11473 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11474 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11475 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11476 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11477 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11478 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11479 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11480 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11481 | |
| 11482 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11483 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11484 | } |
| 11485 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11486 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11487 | HttpRequestInfo request; |
| 11488 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11489 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11490 | request.traffic_annotation = |
| 11491 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11492 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11493 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11494 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11495 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11496 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11497 | MockWrite( |
| 11498 | "POST / HTTP/1.1\r\n" |
| 11499 | "Host: www.example.org\r\n" |
| 11500 | "Connection: keep-alive\r\n" |
| 11501 | "Content-Length: 0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11502 | }; |
| 11503 | |
| 11504 | // Lastly, the server responds with the actual content. |
| 11505 | MockRead data_reads[] = { |
| 11506 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11507 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11508 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11509 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11510 | }; |
| 11511 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11512 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11513 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11514 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11515 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11516 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11517 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11518 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11519 | |
| 11520 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11521 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11522 | } |
| 11523 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11524 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11525 | HttpRequestInfo request; |
| 11526 | request.method = "PUT"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11527 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11528 | request.traffic_annotation = |
| 11529 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11530 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11531 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11532 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11533 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11534 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11535 | MockWrite( |
| 11536 | "PUT / HTTP/1.1\r\n" |
| 11537 | "Host: www.example.org\r\n" |
| 11538 | "Connection: keep-alive\r\n" |
| 11539 | "Content-Length: 0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11540 | }; |
| 11541 | |
| 11542 | // Lastly, the server responds with the actual content. |
| 11543 | MockRead data_reads[] = { |
| 11544 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11545 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11546 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11547 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11548 | }; |
| 11549 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11550 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11551 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11552 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11553 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11554 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11555 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11556 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11557 | |
| 11558 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11559 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11560 | } |
| 11561 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11562 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11563 | HttpRequestInfo request; |
| 11564 | request.method = "HEAD"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11565 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11566 | request.traffic_annotation = |
| 11567 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11568 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11569 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11570 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11571 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11572 | MockWrite data_writes[] = { |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 11573 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 11574 | "Host: www.example.org\r\n" |
| 11575 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11576 | }; |
| 11577 | |
| 11578 | // Lastly, the server responds with the actual content. |
| 11579 | MockRead data_reads[] = { |
| 11580 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11581 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11582 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11583 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11584 | }; |
| 11585 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11586 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11587 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11588 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11589 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11590 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11591 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11592 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11593 | |
| 11594 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11595 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11596 | } |
| 11597 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11598 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11599 | HttpRequestInfo request; |
| 11600 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11601 | request.url = GURL("http://www.example.org/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11602 | request.load_flags = LOAD_BYPASS_CACHE; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11603 | request.traffic_annotation = |
| 11604 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11605 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11606 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11607 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11608 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11609 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11610 | MockWrite( |
| 11611 | "GET / HTTP/1.1\r\n" |
| 11612 | "Host: www.example.org\r\n" |
| 11613 | "Connection: keep-alive\r\n" |
| 11614 | "Pragma: no-cache\r\n" |
| 11615 | "Cache-Control: no-cache\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11616 | }; |
| 11617 | |
| 11618 | // Lastly, the server responds with the actual content. |
| 11619 | MockRead data_reads[] = { |
| 11620 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11621 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11622 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11623 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11624 | }; |
| 11625 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11626 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11627 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11628 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11629 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11630 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11631 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11632 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11633 | |
| 11634 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11635 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11636 | } |
| 11637 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11638 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlValidateCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11639 | HttpRequestInfo request; |
| 11640 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11641 | request.url = GURL("http://www.example.org/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11642 | request.load_flags = LOAD_VALIDATE_CACHE; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11643 | request.traffic_annotation = |
| 11644 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11645 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11646 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11647 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11648 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11649 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11650 | MockWrite( |
| 11651 | "GET / HTTP/1.1\r\n" |
| 11652 | "Host: www.example.org\r\n" |
| 11653 | "Connection: keep-alive\r\n" |
| 11654 | "Cache-Control: max-age=0\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11655 | }; |
| 11656 | |
| 11657 | // Lastly, the server responds with the actual content. |
| 11658 | MockRead data_reads[] = { |
| 11659 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11660 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11661 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11662 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11663 | }; |
| 11664 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11665 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11666 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11667 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11668 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11669 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11670 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11671 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11672 | |
| 11673 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11674 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11675 | } |
| 11676 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11677 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11678 | HttpRequestInfo request; |
| 11679 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11680 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11681 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11682 | request.traffic_annotation = |
| 11683 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11684 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11685 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11686 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11687 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11688 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11689 | MockWrite( |
| 11690 | "GET / HTTP/1.1\r\n" |
| 11691 | "Host: www.example.org\r\n" |
| 11692 | "Connection: keep-alive\r\n" |
| 11693 | "FooHeader: Bar\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11694 | }; |
| 11695 | |
| 11696 | // Lastly, the server responds with the actual content. |
| 11697 | MockRead data_reads[] = { |
| 11698 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11699 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11700 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11701 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11702 | }; |
| 11703 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11704 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11705 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11706 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11707 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11708 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11709 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11710 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11711 | |
| 11712 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11713 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 11714 | } |
| 11715 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11716 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11717 | HttpRequestInfo request; |
| 11718 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11719 | request.url = GURL("http://www.example.org/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 11720 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 11721 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 11722 | request.extra_headers.SetHeader("FoO", "bar"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11723 | request.traffic_annotation = |
| 11724 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11725 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11726 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11727 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11728 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11729 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11730 | MockWrite( |
| 11731 | "GET / HTTP/1.1\r\n" |
| 11732 | "Host: www.example.org\r\n" |
| 11733 | "Connection: keep-alive\r\n" |
| 11734 | "referer: www.foo.com\r\n" |
| 11735 | "hEllo: Kitty\r\n" |
| 11736 | "FoO: bar\r\n\r\n"), |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11737 | }; |
| 11738 | |
| 11739 | // Lastly, the server responds with the actual content. |
| 11740 | MockRead data_reads[] = { |
| 11741 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11742 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 11743 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11744 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11745 | }; |
| 11746 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11747 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11748 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11749 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11750 | TestCompletionCallback callback; |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11751 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11752 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11753 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11754 | |
| 11755 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11756 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 11757 | } |
| 11758 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11759 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11760 | HttpRequestInfo request; |
| 11761 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11762 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11763 | request.traffic_annotation = |
| 11764 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11765 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11766 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11767 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11768 | "SOCKS myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11769 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11770 | session_deps_.net_log = &net_log; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11771 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11772 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11773 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11774 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11775 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 11776 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11777 | |
| 11778 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11779 | MockWrite(ASYNC, write_buffer, base::size(write_buffer)), |
| 11780 | MockWrite("GET / HTTP/1.1\r\n" |
| 11781 | "Host: www.example.org\r\n" |
| 11782 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11783 | |
| 11784 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11785 | MockRead(ASYNC, read_buffer, base::size(read_buffer)), |
| 11786 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11787 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11788 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11789 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11790 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11791 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11792 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11793 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11794 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11795 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11796 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11797 | |
| 11798 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11799 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11800 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11801 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11802 | ASSERT_TRUE(response); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11803 | |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11804 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, response->proxy_server.scheme()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11805 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11806 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11807 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11808 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11809 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11810 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11811 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11812 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11813 | EXPECT_EQ("Payload", response_text); |
| 11814 | } |
| 11815 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11816 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11817 | HttpRequestInfo request; |
| 11818 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11819 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11820 | request.traffic_annotation = |
| 11821 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11822 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11823 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11824 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11825 | "SOCKS myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11826 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11827 | session_deps_.net_log = &net_log; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11828 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11829 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11830 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11831 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11832 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 11833 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11834 | |
| 11835 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11836 | MockWrite(ASYNC, reinterpret_cast<char*>(write_buffer), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11837 | base::size(write_buffer)), |
| 11838 | MockWrite("GET / HTTP/1.1\r\n" |
| 11839 | "Host: www.example.org\r\n" |
| 11840 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 11841 | |
| 11842 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11843 | MockRead(ASYNC, reinterpret_cast<char*>(read_buffer), |
| 11844 | base::size(read_buffer)), |
| 11845 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11846 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11847 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11848 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11849 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11850 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11851 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 11852 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11853 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11854 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11855 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11856 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11857 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11858 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11859 | |
| 11860 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11861 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11862 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11863 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11864 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11865 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11866 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 11867 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11868 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11869 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11870 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, response->proxy_server.scheme()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11871 | |
| 11872 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11873 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11874 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11875 | EXPECT_EQ("Payload", response_text); |
| 11876 | } |
| 11877 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11878 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET_no_PAC) { |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11879 | HttpRequestInfo request; |
| 11880 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11881 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11882 | request.traffic_annotation = |
| 11883 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11884 | |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11885 | session_deps_.proxy_resolution_service = |
| 11886 | ConfiguredProxyResolutionService::CreateFixed( |
| 11887 | "socks4://myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11888 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11889 | session_deps_.net_log = &net_log; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11890 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11891 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11892 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11893 | |
| 11894 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 11895 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 11896 | |
| 11897 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11898 | MockWrite(ASYNC, write_buffer, base::size(write_buffer)), |
| 11899 | MockWrite("GET / HTTP/1.1\r\n" |
| 11900 | "Host: www.example.org\r\n" |
| 11901 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11902 | |
| 11903 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11904 | MockRead(ASYNC, read_buffer, base::size(read_buffer)), |
| 11905 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11906 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11907 | MockRead("Payload"), MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11908 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11909 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11910 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11911 | |
| 11912 | TestCompletionCallback callback; |
| 11913 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11914 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11915 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11916 | |
| 11917 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11918 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11919 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11920 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11921 | ASSERT_TRUE(response); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11922 | |
| 11923 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11924 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11925 | TestLoadTimingNotReused(load_timing_info, |
| 11926 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11927 | |
| 11928 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11929 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11930 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11931 | EXPECT_EQ("Payload", response_text); |
| 11932 | } |
| 11933 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 11934 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11935 | HttpRequestInfo request; |
| 11936 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11937 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 11938 | request.traffic_annotation = |
| 11939 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 11940 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11941 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 11942 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 11943 | "SOCKS5 myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 11944 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11945 | session_deps_.net_log = &net_log; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11946 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 11947 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11948 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11949 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11950 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 11951 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11952 | const char kSOCKS5OkRequest[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 11953 | 0x05, // Version |
| 11954 | 0x01, // Command (CONNECT) |
| 11955 | 0x00, // Reserved. |
| 11956 | 0x03, // Address type (DOMAINNAME). |
| 11957 | 0x0F, // Length of domain (15) |
| 11958 | 'w', 'w', 'w', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', // Domain string |
| 11959 | '.', 'o', 'r', 'g', 0x00, 0x50, // 16-bit port (80) |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 11960 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11961 | const char kSOCKS5OkResponse[] = |
| 11962 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 11963 | |
| 11964 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11965 | MockWrite(ASYNC, kSOCKS5GreetRequest, base::size(kSOCKS5GreetRequest)), |
| 11966 | MockWrite(ASYNC, kSOCKS5OkRequest, base::size(kSOCKS5OkRequest)), |
| 11967 | MockWrite("GET / HTTP/1.1\r\n" |
| 11968 | "Host: www.example.org\r\n" |
| 11969 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11970 | |
| 11971 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 11972 | MockRead(ASYNC, kSOCKS5GreetResponse, base::size(kSOCKS5GreetResponse)), |
| 11973 | MockRead(ASYNC, kSOCKS5OkResponse, base::size(kSOCKS5OkResponse)), |
| 11974 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 11975 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 11976 | MockRead("Payload"), |
| 11977 | MockRead(SYNCHRONOUS, OK)}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11978 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 11979 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 11980 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11981 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 11982 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11983 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 11984 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11985 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11986 | |
| 11987 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 11988 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11989 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11990 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 11991 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 11992 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, response->proxy_server.scheme()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11993 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11994 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 11995 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 11996 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 11997 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 11998 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 11999 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12000 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12001 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12002 | EXPECT_EQ("Payload", response_text); |
| 12003 | } |
| 12004 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12005 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12006 | HttpRequestInfo request; |
| 12007 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12008 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12009 | request.traffic_annotation = |
| 12010 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12011 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12012 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12013 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 12014 | "SOCKS5 myproxy:1080", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 12015 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12016 | session_deps_.net_log = &net_log; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12017 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12018 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12019 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12020 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12021 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 12022 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 12023 | const unsigned char kSOCKS5OkRequest[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12024 | 0x05, // Version |
| 12025 | 0x01, // Command (CONNECT) |
| 12026 | 0x00, // Reserved. |
| 12027 | 0x03, // Address type (DOMAINNAME). |
| 12028 | 0x0F, // Length of domain (15) |
| 12029 | 'w', 'w', 'w', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', // Domain string |
| 12030 | '.', 'o', 'r', 'g', 0x01, 0xBB, // 16-bit port (443) |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 12031 | }; |
| 12032 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12033 | const char kSOCKS5OkResponse[] = |
| 12034 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 12035 | |
| 12036 | MockWrite data_writes[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12037 | MockWrite(ASYNC, kSOCKS5GreetRequest, base::size(kSOCKS5GreetRequest)), |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12038 | MockWrite(ASYNC, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12039 | base::size(kSOCKS5OkRequest)), |
| 12040 | MockWrite("GET / HTTP/1.1\r\n" |
| 12041 | "Host: www.example.org\r\n" |
| 12042 | "Connection: keep-alive\r\n\r\n")}; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 12043 | |
| 12044 | MockRead data_reads[] = { |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12045 | MockRead(ASYNC, kSOCKS5GreetResponse, base::size(kSOCKS5GreetResponse)), |
| 12046 | MockRead(ASYNC, kSOCKS5OkResponse, base::size(kSOCKS5OkResponse)), |
| 12047 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 12048 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 12049 | MockRead("Payload"), |
| 12050 | MockRead(SYNCHRONOUS, OK)}; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12051 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12052 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12053 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12054 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12055 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12056 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12057 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12058 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12059 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12060 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12061 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12062 | |
| 12063 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12064 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12065 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12066 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12067 | ASSERT_TRUE(response); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 12068 | EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, response->proxy_server.scheme()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12069 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 12070 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12071 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 12072 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 12073 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 12074 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12075 | std::string response_text; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12076 | rv = ReadTransaction(&trans, &response_text); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12077 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 12078 | EXPECT_EQ("Payload", response_text); |
| 12079 | } |
| 12080 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 12081 | namespace { |
| 12082 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12083 | // Tests that for connection endpoints the group ids are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12084 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12085 | struct GroupIdTest { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12086 | std::string proxy_server; |
| 12087 | std::string url; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12088 | ClientSocketPool::GroupId expected_group_id; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 12089 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12090 | }; |
| 12091 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12092 | std::unique_ptr<HttpNetworkSession> SetupSessionForGroupIdTests( |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12093 | SpdySessionDependencies* session_deps_) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12094 | std::unique_ptr<HttpNetworkSession> session(CreateSession(session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12095 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 12096 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 12097 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 12098 | AlternativeService alternative_service(kProtoHTTP2, "", 444); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 12099 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 12100 | http_server_properties->SetHttp2AlternativeService( |
bnc | aa60ff40 | 2016-06-22 19:12:42 | [diff] [blame] | 12101 | url::SchemeHostPort("https", "host.with.alternate", 443), |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 12102 | NetworkIsolationKey(), alternative_service, expiration); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12103 | |
| 12104 | return session; |
| 12105 | } |
| 12106 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12107 | int GroupIdTransactionHelper(const std::string& url, |
| 12108 | HttpNetworkSession* session) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12109 | HttpRequestInfo request; |
| 12110 | request.method = "GET"; |
| 12111 | request.url = GURL(url); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12112 | request.traffic_annotation = |
| 12113 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12114 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12115 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12116 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12117 | TestCompletionCallback callback; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12118 | |
| 12119 | // We do not complete this request, the dtor will clean the transaction up. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12120 | return trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12121 | } |
| 12122 | |
[email protected] | 448d4ca5 | 2012-03-04 04:12:23 | [diff] [blame] | 12123 | } // namespace |
| 12124 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12125 | TEST_F(HttpNetworkTransactionTest, GroupIdForDirectConnections) { |
| 12126 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12127 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12128 | "", // unused |
| 12129 | "http://www.example.org/direct", |
| 12130 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12131 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12132 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12133 | NetworkIsolationKey(), |
| 12134 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12135 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12136 | }, |
| 12137 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12138 | "", // unused |
| 12139 | "http://[2001:1418:13:1::25]/direct", |
| 12140 | ClientSocketPool::GroupId(HostPortPair("2001:1418:13:1::25", 80), |
| 12141 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12142 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12143 | NetworkIsolationKey(), |
| 12144 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12145 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12146 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12147 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12148 | // SSL Tests |
| 12149 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12150 | "", // unused |
| 12151 | "https://www.example.org/direct_ssl", |
| 12152 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12153 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12154 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12155 | NetworkIsolationKey(), |
| 12156 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12157 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12158 | }, |
| 12159 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12160 | "", // unused |
| 12161 | "https://[2001:1418:13:1::25]/direct", |
| 12162 | ClientSocketPool::GroupId(HostPortPair("2001:1418:13:1::25", 443), |
| 12163 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12164 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12165 | NetworkIsolationKey(), |
| 12166 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12167 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12168 | }, |
| 12169 | { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12170 | "", // unused |
| 12171 | "https://host.with.alternate/direct", |
| 12172 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12173 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12174 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12175 | NetworkIsolationKey(), |
| 12176 | false /* disable_secure_dns */), |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12177 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12178 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12179 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 12180 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12181 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12182 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12183 | ConfiguredProxyResolutionService::CreateFixed( |
| 12184 | tests[i].proxy_server, TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12185 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12186 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12187 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12188 | HttpNetworkSessionPeer peer(session.get()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12189 | CaptureGroupIdTransportSocketPool* transport_conn_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12190 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12191 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12192 | mock_pool_manager->SetSocketPool(ProxyServer::Direct(), |
| 12193 | base::WrapUnique(transport_conn_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12194 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12195 | |
| 12196 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12197 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12198 | EXPECT_EQ(tests[i].expected_group_id, |
| 12199 | transport_conn_pool->last_group_id_received()); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 12200 | EXPECT_TRUE(transport_conn_pool->socket_requested()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12201 | } |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12202 | } |
| 12203 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12204 | TEST_F(HttpNetworkTransactionTest, GroupIdForHTTPProxyConnections) { |
| 12205 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12206 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12207 | "http_proxy", |
| 12208 | "http://www.example.org/http_proxy_normal", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12209 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12210 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12211 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12212 | NetworkIsolationKey(), |
| 12213 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12214 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12215 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12216 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12217 | // SSL Tests |
| 12218 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12219 | "http_proxy", |
| 12220 | "https://www.example.org/http_connect_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12221 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12222 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12223 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12224 | NetworkIsolationKey(), |
| 12225 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12226 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12227 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 12228 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12229 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12230 | "http_proxy", |
| 12231 | "https://host.with.alternate/direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12232 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12233 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12234 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12235 | NetworkIsolationKey(), |
| 12236 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12237 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12238 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12239 | }; |
| 12240 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12241 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12242 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12243 | ConfiguredProxyResolutionService::CreateFixed( |
| 12244 | tests[i].proxy_server, TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12245 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12246 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12247 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12248 | HttpNetworkSessionPeer peer(session.get()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12249 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 12250 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 12251 | HostPortPair("http_proxy", 80)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12252 | CaptureGroupIdTransportSocketPool* http_proxy_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12253 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12254 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12255 | mock_pool_manager->SetSocketPool(proxy_server, |
| 12256 | base::WrapUnique(http_proxy_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12257 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12258 | |
| 12259 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12260 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12261 | EXPECT_EQ(tests[i].expected_group_id, |
| 12262 | http_proxy_pool->last_group_id_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12263 | } |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12264 | } |
| 12265 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12266 | TEST_F(HttpNetworkTransactionTest, GroupIdForSOCKSConnections) { |
| 12267 | const GroupIdTest tests[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12268 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12269 | "socks4://socks_proxy:1080", |
| 12270 | "http://www.example.org/socks4_direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12271 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12272 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12273 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12274 | NetworkIsolationKey(), |
| 12275 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12276 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12277 | }, |
| 12278 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12279 | "socks5://socks_proxy:1080", |
| 12280 | "http://www.example.org/socks5_direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12281 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 80), |
| 12282 | ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12283 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12284 | NetworkIsolationKey(), |
| 12285 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12286 | false, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12287 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12288 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12289 | // SSL Tests |
| 12290 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12291 | "socks4://socks_proxy:1080", |
| 12292 | "https://www.example.org/socks4_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12293 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12294 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12295 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12296 | NetworkIsolationKey(), |
| 12297 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12298 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12299 | }, |
| 12300 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12301 | "socks5://socks_proxy:1080", |
| 12302 | "https://www.example.org/socks5_ssl", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12303 | ClientSocketPool::GroupId(HostPortPair("www.example.org", 443), |
| 12304 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12305 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12306 | NetworkIsolationKey(), |
| 12307 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12308 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12309 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 12310 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12311 | { |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12312 | "socks4://socks_proxy:1080", |
| 12313 | "https://host.with.alternate/direct", |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12314 | ClientSocketPool::GroupId(HostPortPair("host.with.alternate", 443), |
| 12315 | ClientSocketPool::SocketType::kSsl, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 12316 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 12317 | NetworkIsolationKey(), |
| 12318 | false /* disable_secure_dns */), |
Matt Menke | 4802de6 | 2019-03-08 22:47:50 | [diff] [blame] | 12319 | true, |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12320 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12321 | }; |
| 12322 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 12323 | for (size_t i = 0; i < base::size(tests); ++i) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 12324 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12325 | ConfiguredProxyResolutionService::CreateFixed( |
| 12326 | tests[i].proxy_server, TRAFFIC_ANNOTATION_FOR_TESTS); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12327 | std::unique_ptr<HttpNetworkSession> session( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12328 | SetupSessionForGroupIdTests(&session_deps_)); |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 12329 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 12330 | HttpNetworkSessionPeer peer(session.get()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12331 | |
Matt Menke | e8648fa | 2019-01-17 16:47:07 | [diff] [blame] | 12332 | ProxyServer proxy_server( |
| 12333 | ProxyServer::FromURI(tests[i].proxy_server, ProxyServer::SCHEME_HTTP)); |
| 12334 | ASSERT_TRUE(proxy_server.is_valid()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12335 | CaptureGroupIdTransportSocketPool* socks_conn_pool = |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 12336 | new CaptureGroupIdTransportSocketPool(&dummy_connect_job_params_); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12337 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 12338 | mock_pool_manager->SetSocketPool(proxy_server, |
| 12339 | base::WrapUnique(socks_conn_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 12340 | peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12341 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12342 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12343 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12344 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 12345 | GroupIdTransactionHelper(tests[i].url, session.get())); |
| 12346 | EXPECT_EQ(tests[i].expected_group_id, |
| 12347 | socks_conn_pool->last_group_id_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 12348 | } |
| 12349 | } |
| 12350 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12351 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12352 | HttpRequestInfo request; |
| 12353 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12354 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12355 | request.traffic_annotation = |
| 12356 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12357 | |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12358 | session_deps_.proxy_resolution_service = |
| 12359 | ConfiguredProxyResolutionService::CreateFixed( |
| 12360 | "myproxy:70;foobar:80", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 12361 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 12362 | // This simulates failure resolving all hostnames; that means we will fail |
| 12363 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12364 | session_deps_.host_resolver->rules()->AddSimulatedFailure("*"); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 12365 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12366 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12367 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12368 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12369 | TestCompletionCallback callback; |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12370 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12371 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12372 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12373 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12374 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12375 | EXPECT_THAT(rv, IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 12376 | } |
| 12377 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12378 | // Make sure we can handle an error when writing the request. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12379 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12380 | HttpRequestInfo request; |
| 12381 | request.method = "GET"; |
| 12382 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12383 | request.traffic_annotation = |
| 12384 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12385 | |
| 12386 | MockWrite write_failure[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12387 | MockWrite(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12388 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12389 | StaticSocketDataProvider data(base::span<MockRead>(), write_failure); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12390 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12391 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12392 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12393 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12394 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12395 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12396 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12397 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12398 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12399 | |
| 12400 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12401 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12402 | |
| 12403 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12404 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12405 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12406 | } |
| 12407 | |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12408 | // Check that a connection closed after the start of the headers finishes ok. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12409 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12410 | HttpRequestInfo request; |
| 12411 | request.method = "GET"; |
| 12412 | request.url = GURL("http://www.foo.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12413 | request.traffic_annotation = |
| 12414 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12415 | |
| 12416 | MockRead data_reads[] = { |
| 12417 | MockRead("HTTP/1."), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12418 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12419 | }; |
| 12420 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12421 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12422 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12423 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12424 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12425 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12426 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12427 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12428 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12429 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12430 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12431 | |
| 12432 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12433 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12434 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12435 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12436 | ASSERT_TRUE(response); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12437 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12438 | EXPECT_TRUE(response->headers); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12439 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 12440 | |
| 12441 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12442 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12443 | EXPECT_THAT(rv, IsOk()); |
zmo | 9528c9f4 | 2015-08-04 22:12:08 | [diff] [blame] | 12444 | EXPECT_EQ("", response_data); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12445 | |
| 12446 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12447 | EXPECT_TRUE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 12448 | EXPECT_LT(0u, endpoint.address().size()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12449 | } |
| 12450 | |
| 12451 | // Make sure that a dropped connection while draining the body for auth |
| 12452 | // restart does the right thing. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12453 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12454 | HttpRequestInfo request; |
| 12455 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12456 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12457 | request.traffic_annotation = |
| 12458 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12459 | |
| 12460 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12461 | MockWrite( |
| 12462 | "GET / HTTP/1.1\r\n" |
| 12463 | "Host: www.example.org\r\n" |
| 12464 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12465 | }; |
| 12466 | |
| 12467 | MockRead data_reads1[] = { |
| 12468 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 12469 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 12470 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 12471 | MockRead("Content-Length: 14\r\n\r\n"), |
| 12472 | MockRead("Unauth"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12473 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12474 | }; |
| 12475 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12476 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12477 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12478 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12479 | // After calling trans.RestartWithAuth(), this is the request we should |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12480 | // be issuing -- the final header line contains the credentials. |
| 12481 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12482 | MockWrite( |
| 12483 | "GET / HTTP/1.1\r\n" |
| 12484 | "Host: www.example.org\r\n" |
| 12485 | "Connection: keep-alive\r\n" |
| 12486 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12487 | }; |
| 12488 | |
| 12489 | // Lastly, the server responds with the actual content. |
| 12490 | MockRead data_reads2[] = { |
| 12491 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 12492 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 12493 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12494 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12495 | }; |
| 12496 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12497 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12498 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12499 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12500 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12501 | TestCompletionCallback callback1; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12502 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12503 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12504 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12505 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12506 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12507 | |
| 12508 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12509 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12510 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12511 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12512 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12513 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12514 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12515 | TestCompletionCallback callback2; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12516 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12517 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12518 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12519 | |
| 12520 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12521 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12522 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12523 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12524 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12525 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12526 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 12527 | } |
| 12528 | |
| 12529 | // Test HTTPS connections going through a proxy that sends extra data. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12530 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 12531 | session_deps_.proxy_resolution_service = |
| 12532 | ConfiguredProxyResolutionService::CreateFixed( |
| 12533 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12534 | |
| 12535 | HttpRequestInfo request; |
| 12536 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12537 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12538 | request.traffic_annotation = |
| 12539 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12540 | |
| 12541 | MockRead proxy_reads[] = { |
| 12542 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12543 | MockRead(SYNCHRONOUS, OK) |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12544 | }; |
| 12545 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12546 | StaticSocketDataProvider data(proxy_reads, base::span<MockWrite>()); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12547 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12548 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12549 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12550 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12551 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12552 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12553 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12554 | session_deps_.socket_factory->ResetNextMockIndexes(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12555 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12556 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12557 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12558 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12559 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12560 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12561 | |
| 12562 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12563 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 12564 | } |
| 12565 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12566 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12567 | HttpRequestInfo request; |
| 12568 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12569 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12570 | request.traffic_annotation = |
| 12571 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12572 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12573 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12574 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 12575 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12576 | MockRead data_reads[] = { |
| 12577 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12578 | MockRead(SYNCHRONOUS, OK), |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12579 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12580 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12581 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12582 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12583 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12584 | TestCompletionCallback callback; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12585 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12586 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12587 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12588 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12589 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12590 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12591 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12592 | ASSERT_TRUE(response); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12593 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12594 | EXPECT_TRUE(response->headers); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 12595 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 12596 | |
| 12597 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12598 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12599 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 12600 | } |
| 12601 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12602 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 12603 | base::FilePath temp_file_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 12604 | ASSERT_TRUE(base::CreateTemporaryFile(&temp_file_path)); |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12605 | const uint64_t kFakeSize = 100000; // file is actually blank |
[email protected] | d9896165 | 2012-09-11 20:27:21 | [diff] [blame] | 12606 | UploadFileElementReader::ScopedOverridingContentLengthForTests |
| 12607 | overriding_content_length(kFakeSize); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12608 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12609 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12610 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12611 | base::ThreadTaskRunnerHandle::Get().get(), temp_file_path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 12612 | std::numeric_limits<uint64_t>::max(), base::Time())); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12613 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12614 | |
| 12615 | HttpRequestInfo request; |
| 12616 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12617 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12618 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12619 | request.traffic_annotation = |
| 12620 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12621 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12622 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12623 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12624 | |
| 12625 | MockRead data_reads[] = { |
| 12626 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 12627 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 12628 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12629 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12630 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12631 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12632 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12633 | TestCompletionCallback callback; |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12634 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12635 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12636 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12637 | |
| 12638 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12639 | EXPECT_THAT(rv, IsError(ERR_UPLOAD_FILE_CHANGED)); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12640 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12641 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12642 | ASSERT_TRUE(response); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12643 | |
maksim.sisov | e869bf5 | 2016-06-23 17:11:52 | [diff] [blame] | 12644 | EXPECT_FALSE(response->headers); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12645 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 12646 | base::DeleteFile(temp_file_path, false); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12647 | } |
| 12648 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12649 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 12650 | base::FilePath temp_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 12651 | ASSERT_TRUE(base::CreateTemporaryFile(&temp_file)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12652 | std::string temp_file_content("Unreadable file."); |
lazyboy | 8df84fc | 2017-04-12 02:12:48 | [diff] [blame] | 12653 | ASSERT_EQ(static_cast<int>(temp_file_content.length()), |
| 12654 | base::WriteFile(temp_file, temp_file_content.c_str(), |
| 12655 | temp_file_content.length())); |
[email protected] | 92be8eb | 2014-08-07 22:57:11 | [diff] [blame] | 12656 | ASSERT_TRUE(base::MakeFileUnreadable(temp_file)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12657 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12658 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12659 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12660 | base::ThreadTaskRunnerHandle::Get().get(), temp_file, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 12661 | std::numeric_limits<uint64_t>::max(), base::Time())); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12662 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12663 | |
| 12664 | HttpRequestInfo request; |
| 12665 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12666 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12667 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12668 | request.traffic_annotation = |
| 12669 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 12670 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 12671 | // If we try to upload an unreadable file, the transaction should fail. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12672 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12673 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12674 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12675 | StaticSocketDataProvider data; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12676 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12677 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12678 | TestCompletionCallback callback; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12679 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12680 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12681 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12682 | |
| 12683 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12684 | EXPECT_THAT(rv, IsError(ERR_ACCESS_DENIED)); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12685 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 12686 | base::DeleteFile(temp_file, false); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 12687 | } |
| 12688 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12689 | TEST_F(HttpNetworkTransactionTest, CancelDuringInitRequestBody) { |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12690 | class FakeUploadElementReader : public UploadElementReader { |
| 12691 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 12692 | FakeUploadElementReader() = default; |
| 12693 | ~FakeUploadElementReader() override = default; |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12694 | |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12695 | CompletionOnceCallback TakeCallback() { return std::move(callback_); } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12696 | |
| 12697 | // UploadElementReader overrides: |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12698 | int Init(CompletionOnceCallback callback) override { |
| 12699 | callback_ = std::move(callback); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12700 | return ERR_IO_PENDING; |
| 12701 | } |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 12702 | uint64_t GetContentLength() const override { return 0; } |
| 12703 | uint64_t BytesRemaining() const override { return 0; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 12704 | int Read(IOBuffer* buf, |
| 12705 | int buf_length, |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12706 | CompletionOnceCallback callback) override { |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12707 | return ERR_FAILED; |
| 12708 | } |
| 12709 | |
| 12710 | private: |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12711 | CompletionOnceCallback callback_; |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12712 | }; |
| 12713 | |
| 12714 | FakeUploadElementReader* fake_reader = new FakeUploadElementReader; |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12715 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 12716 | element_readers.push_back(base::WrapUnique(fake_reader)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 12717 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12718 | |
| 12719 | HttpRequestInfo request; |
| 12720 | request.method = "POST"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12721 | request.url = GURL("http://www.example.org/upload"); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12722 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12723 | request.traffic_annotation = |
| 12724 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12725 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12726 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 12727 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 12728 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12729 | |
| 12730 | StaticSocketDataProvider data; |
| 12731 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12732 | |
| 12733 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12734 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12735 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 12736 | base::RunLoop().RunUntilIdle(); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12737 | |
| 12738 | // Transaction is pending on request body initialization. |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12739 | CompletionOnceCallback init_callback = fake_reader->TakeCallback(); |
| 12740 | ASSERT_FALSE(init_callback.is_null()); |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12741 | |
| 12742 | // Return Init()'s result after the transaction gets destroyed. |
| 12743 | trans.reset(); |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 12744 | std::move(init_callback).Run(OK); // Should not crash. |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 12745 | } |
| 12746 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12747 | // Tests that changes to Auth realms are treated like auth rejections. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12748 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12749 | HttpRequestInfo request; |
| 12750 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12751 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12752 | request.traffic_annotation = |
| 12753 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12754 | |
| 12755 | // First transaction will request a resource and receive a Basic challenge |
| 12756 | // with realm="first_realm". |
| 12757 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12758 | MockWrite( |
| 12759 | "GET / HTTP/1.1\r\n" |
| 12760 | "Host: www.example.org\r\n" |
| 12761 | "Connection: keep-alive\r\n" |
| 12762 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12763 | }; |
| 12764 | MockRead data_reads1[] = { |
| 12765 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12766 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 12767 | "\r\n"), |
| 12768 | }; |
| 12769 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12770 | // After calling trans.RestartWithAuth(), provide an Authentication header |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12771 | // for first_realm. The server will reject and provide a challenge with |
| 12772 | // second_realm. |
| 12773 | MockWrite data_writes2[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12774 | MockWrite( |
| 12775 | "GET / HTTP/1.1\r\n" |
| 12776 | "Host: www.example.org\r\n" |
| 12777 | "Connection: keep-alive\r\n" |
| 12778 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 12779 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12780 | }; |
| 12781 | MockRead data_reads2[] = { |
| 12782 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12783 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 12784 | "\r\n"), |
| 12785 | }; |
| 12786 | |
| 12787 | // This again fails, and goes back to first_realm. Make sure that the |
| 12788 | // entry is removed from cache. |
| 12789 | MockWrite data_writes3[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12790 | MockWrite( |
| 12791 | "GET / HTTP/1.1\r\n" |
| 12792 | "Host: www.example.org\r\n" |
| 12793 | "Connection: keep-alive\r\n" |
| 12794 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 12795 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12796 | }; |
| 12797 | MockRead data_reads3[] = { |
| 12798 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 12799 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 12800 | "\r\n"), |
| 12801 | }; |
| 12802 | |
| 12803 | // Try one last time (with the correct password) and get the resource. |
| 12804 | MockWrite data_writes4[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 12805 | MockWrite( |
| 12806 | "GET / HTTP/1.1\r\n" |
| 12807 | "Host: www.example.org\r\n" |
| 12808 | "Connection: keep-alive\r\n" |
| 12809 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 12810 | "\r\n"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12811 | }; |
| 12812 | MockRead data_reads4[] = { |
| 12813 | MockRead("HTTP/1.1 200 OK\r\n" |
| 12814 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12815 | "Content-Length: 5\r\n" |
| 12816 | "\r\n" |
| 12817 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12818 | }; |
| 12819 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12820 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 12821 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 12822 | StaticSocketDataProvider data3(data_reads3, data_writes3); |
| 12823 | StaticSocketDataProvider data4(data_reads4, data_writes4); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 12824 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 12825 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 12826 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 12827 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12828 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12829 | TestCompletionCallback callback1; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12830 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12831 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12832 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 12833 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12834 | // Issue the first request with Authorize headers. There should be a |
| 12835 | // password prompt for first_realm waiting to be filled in after the |
| 12836 | // transaction completes. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12837 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12838 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12839 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12840 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12841 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12842 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12843 | base::Optional<AuthChallengeInfo> challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12844 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12845 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12846 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12847 | EXPECT_EQ("first_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12848 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12849 | |
| 12850 | // Issue the second request with an incorrect password. There should be a |
| 12851 | // password prompt for second_realm waiting to be filled in after the |
| 12852 | // transaction completes. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12853 | TestCompletionCallback callback2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12854 | rv = trans.RestartWithAuth(AuthCredentials(kFirst, kBaz), |
| 12855 | callback2.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12856 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12857 | rv = callback2.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12858 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12859 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12860 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12861 | challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12862 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12863 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12864 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12865 | EXPECT_EQ("second_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12866 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12867 | |
| 12868 | // Issue the third request with another incorrect password. There should be |
| 12869 | // a password prompt for first_realm waiting to be filled in. If the password |
| 12870 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 12871 | // first_realm was not correctly removed. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12872 | TestCompletionCallback callback3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12873 | rv = trans.RestartWithAuth(AuthCredentials(kSecond, kFou), |
| 12874 | callback3.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12875 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12876 | rv = callback3.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12877 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12878 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12879 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12880 | challenge = response->auth_challenge; |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12881 | ASSERT_TRUE(challenge); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12882 | EXPECT_FALSE(challenge->is_proxy); |
asanka | 098c009 | 2016-06-16 20:18:43 | [diff] [blame] | 12883 | EXPECT_EQ("http://www.example.org", challenge->challenger.Serialize()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 12884 | EXPECT_EQ("first_realm", challenge->realm); |
aberent | bba302d | 2015-12-03 10:20:19 | [diff] [blame] | 12885 | EXPECT_EQ(kBasicAuthScheme, challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12886 | |
| 12887 | // Issue the fourth request with the correct password and username. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12888 | TestCompletionCallback callback4; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12889 | rv = trans.RestartWithAuth(AuthCredentials(kFirst, kBar), |
| 12890 | callback4.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12891 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12892 | rv = callback4.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12893 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12894 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 12895 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 12896 | EXPECT_FALSE(response->auth_challenge.has_value()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 12897 | } |
| 12898 | |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12899 | // Regression test for https://crbug.com/754395. |
| 12900 | TEST_F(HttpNetworkTransactionTest, IgnoreAltSvcWithInvalidCert) { |
| 12901 | MockRead data_reads[] = { |
| 12902 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 12903 | MockRead(kAlternativeServiceHttpHeader), |
| 12904 | MockRead("\r\n"), |
| 12905 | MockRead("hello world"), |
| 12906 | MockRead(SYNCHRONOUS, OK), |
| 12907 | }; |
| 12908 | |
| 12909 | HttpRequestInfo request; |
| 12910 | request.method = "GET"; |
| 12911 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12912 | request.traffic_annotation = |
| 12913 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12914 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12915 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12916 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12917 | |
| 12918 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 12919 | ssl.ssl_info.cert = |
| 12920 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 12921 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 12922 | ssl.ssl_info.cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12923 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12924 | |
| 12925 | TestCompletionCallback callback; |
| 12926 | |
| 12927 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 12928 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 12929 | |
| 12930 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 12931 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 12932 | |
| 12933 | url::SchemeHostPort test_server(request.url); |
| 12934 | HttpServerProperties* http_server_properties = |
| 12935 | session->http_server_properties(); |
| 12936 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12937 | http_server_properties |
| 12938 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12939 | .empty()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12940 | |
| 12941 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 12942 | |
| 12943 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 12944 | ASSERT_TRUE(response); |
| 12945 | ASSERT_TRUE(response->headers); |
| 12946 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 12947 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 12948 | EXPECT_FALSE(response->was_alpn_negotiated); |
| 12949 | |
| 12950 | std::string response_data; |
| 12951 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 12952 | EXPECT_EQ("hello world", response_data); |
| 12953 | |
| 12954 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12955 | http_server_properties |
| 12956 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12957 | .empty()); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 12958 | } |
| 12959 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 12960 | TEST_F(HttpNetworkTransactionTest, HonorAlternativeServiceHeader) { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12961 | MockRead data_reads[] = { |
| 12962 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 12963 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12964 | MockRead("\r\n"), |
| 12965 | MockRead("hello world"), |
| 12966 | MockRead(SYNCHRONOUS, OK), |
| 12967 | }; |
| 12968 | |
| 12969 | HttpRequestInfo request; |
| 12970 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12971 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 12972 | request.traffic_annotation = |
| 12973 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12974 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 12975 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12976 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 12977 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12978 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 12979 | ssl.ssl_info.cert = |
| 12980 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 12981 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12982 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 12983 | |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12984 | TestCompletionCallback callback; |
| 12985 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12986 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 12987 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12988 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 12989 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 12990 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12991 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 12992 | url::SchemeHostPort test_server(request.url); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 12993 | HttpServerProperties* http_server_properties = |
| 12994 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 12995 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 12996 | http_server_properties |
| 12997 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 12998 | .empty()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 12999 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13000 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13001 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13002 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13003 | ASSERT_TRUE(response); |
| 13004 | ASSERT_TRUE(response->headers); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13005 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13006 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13007 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13008 | |
| 13009 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13010 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13011 | EXPECT_EQ("hello world", response_data); |
| 13012 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13013 | AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13014 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 13015 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13016 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 13017 | AlternativeService alternative_service(kProtoHTTP2, "mail.example.org", 443); |
| 13018 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13019 | alternative_service_info_vector[0].alternative_service()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13020 | } |
| 13021 | |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13022 | TEST_F(HttpNetworkTransactionTest, |
| 13023 | HonorAlternativeServiceHeaderWithNetworkIsolationKey) { |
| 13024 | base::test::ScopedFeatureList feature_list; |
| 13025 | feature_list.InitWithFeatures( |
| 13026 | // enabled_features |
| 13027 | {features::kPartitionHttpServerPropertiesByNetworkIsolationKey, |
| 13028 | // Need to partition connections by NetworkIsolationKey for |
| 13029 | // SpdySessionKeys to include NetworkIsolationKeys. |
| 13030 | features::kPartitionConnectionsByNetworkIsolationKey}, |
| 13031 | // disabled_features |
| 13032 | {}); |
| 13033 | // Since HttpServerProperties caches the feature value, have to create a new |
| 13034 | // one. |
| 13035 | session_deps_.http_server_properties = |
| 13036 | std::make_unique<HttpServerProperties>(); |
| 13037 | |
| 13038 | const url::Origin kOrigin1 = url::Origin::Create(GURL("https://foo.test/")); |
| 13039 | const net::NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 13040 | const url::Origin kOrigin2 = url::Origin::Create(GURL("https://bar.test/")); |
| 13041 | const net::NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
| 13042 | |
| 13043 | MockRead data_reads[] = { |
| 13044 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 13045 | MockRead(kAlternativeServiceHttpHeader), |
| 13046 | MockRead("\r\n"), |
| 13047 | MockRead("hello world"), |
| 13048 | MockRead(SYNCHRONOUS, OK), |
| 13049 | }; |
| 13050 | |
| 13051 | HttpRequestInfo request; |
| 13052 | request.method = "GET"; |
| 13053 | request.url = GURL("https://www.example.org/"); |
| 13054 | request.traffic_annotation = |
| 13055 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 13056 | request.network_isolation_key = kNetworkIsolationKey1; |
| 13057 | |
| 13058 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
| 13059 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13060 | |
| 13061 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 13062 | ssl.ssl_info.cert = |
| 13063 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13064 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 13065 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13066 | |
| 13067 | TestCompletionCallback callback; |
| 13068 | |
| 13069 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 13070 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 13071 | |
| 13072 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 13073 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13074 | |
| 13075 | url::SchemeHostPort test_server(request.url); |
| 13076 | HttpServerProperties* http_server_properties = |
| 13077 | session->http_server_properties(); |
| 13078 | EXPECT_TRUE( |
| 13079 | http_server_properties |
| 13080 | ->GetAlternativeServiceInfos(test_server, kNetworkIsolationKey1) |
| 13081 | .empty()); |
| 13082 | |
| 13083 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 13084 | |
| 13085 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 13086 | ASSERT_TRUE(response); |
| 13087 | ASSERT_TRUE(response->headers); |
| 13088 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13089 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 13090 | EXPECT_FALSE(response->was_alpn_negotiated); |
| 13091 | |
| 13092 | std::string response_data; |
| 13093 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 13094 | EXPECT_EQ("hello world", response_data); |
| 13095 | |
| 13096 | AlternativeServiceInfoVector alternative_service_info_vector = |
| 13097 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 13098 | kNetworkIsolationKey1); |
| 13099 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 13100 | AlternativeService alternative_service(kProtoHTTP2, "mail.example.org", 443); |
| 13101 | EXPECT_EQ(alternative_service, |
| 13102 | alternative_service_info_vector[0].alternative_service()); |
| 13103 | |
| 13104 | // Make sure the alternative service information is only associated with |
| 13105 | // kNetworkIsolationKey1. |
| 13106 | EXPECT_TRUE( |
| 13107 | http_server_properties |
| 13108 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13109 | .empty()); |
| 13110 | EXPECT_TRUE( |
| 13111 | http_server_properties |
| 13112 | ->GetAlternativeServiceInfos(test_server, kNetworkIsolationKey2) |
| 13113 | .empty()); |
| 13114 | } |
| 13115 | |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13116 | // Regression test for https://crbug.com/615497. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13117 | TEST_F(HttpNetworkTransactionTest, |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13118 | DoNotParseAlternativeServiceHeaderOnInsecureRequest) { |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13119 | MockRead data_reads[] = { |
| 13120 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13121 | MockRead(kAlternativeServiceHttpHeader), |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13122 | MockRead("\r\n"), |
| 13123 | MockRead("hello world"), |
| 13124 | MockRead(SYNCHRONOUS, OK), |
| 13125 | }; |
| 13126 | |
| 13127 | HttpRequestInfo request; |
| 13128 | request.method = "GET"; |
| 13129 | request.url = GURL("http://www.example.org/"); |
| 13130 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13131 | request.traffic_annotation = |
| 13132 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13133 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13134 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13135 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13136 | |
| 13137 | TestCompletionCallback callback; |
| 13138 | |
| 13139 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13140 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13141 | |
| 13142 | url::SchemeHostPort test_server(request.url); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13143 | HttpServerProperties* http_server_properties = |
| 13144 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13145 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13146 | http_server_properties |
| 13147 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13148 | .empty()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13149 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13150 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13151 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13152 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13153 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13154 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13155 | ASSERT_TRUE(response); |
| 13156 | ASSERT_TRUE(response->headers); |
| 13157 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13158 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13159 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13160 | |
| 13161 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13162 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13163 | EXPECT_EQ("hello world", response_data); |
| 13164 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13165 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13166 | http_server_properties |
| 13167 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13168 | .empty()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13169 | } |
| 13170 | |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 13171 | // HTTP/2 Alternative Services should be disabled by default. |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13172 | // TODO(bnc): Remove when https://crbug.com/615413 is fixed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13173 | TEST_F(HttpNetworkTransactionTest, |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13174 | DisableHTTP2AlternativeServicesWithDifferentHost) { |
bnc | a86731e | 2017-04-17 12:31:28 | [diff] [blame] | 13175 | session_deps_.enable_http2_alternative_service = false; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13176 | |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13177 | HttpRequestInfo request; |
| 13178 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13179 | request.url = GURL("https://www.example.org/"); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13180 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13181 | request.traffic_annotation = |
| 13182 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13183 | |
| 13184 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13185 | StaticSocketDataProvider first_data; |
| 13186 | first_data.set_connect_data(mock_connect); |
| 13187 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13188 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13189 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13190 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13191 | |
| 13192 | MockRead data_reads[] = { |
| 13193 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 13194 | MockRead(ASYNC, OK), |
| 13195 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13196 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13197 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
| 13198 | |
| 13199 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 13200 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13201 | HttpServerProperties* http_server_properties = |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13202 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13203 | AlternativeService alternative_service(kProtoHTTP2, "different.example.org", |
| 13204 | 444); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13205 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13206 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13207 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13208 | alternative_service, expiration); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13209 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13210 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13211 | TestCompletionCallback callback; |
| 13212 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13213 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13214 | // Alternative service is not used, request fails. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13215 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 13216 | } |
| 13217 | |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13218 | // Regression test for https://crbug.com/615497: |
| 13219 | // Alternative Services should be disabled for http origin. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13220 | TEST_F(HttpNetworkTransactionTest, |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13221 | DisableAlternativeServicesForInsecureOrigin) { |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13222 | HttpRequestInfo request; |
| 13223 | request.method = "GET"; |
| 13224 | request.url = GURL("http://www.example.org/"); |
| 13225 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13226 | request.traffic_annotation = |
| 13227 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13228 | |
| 13229 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13230 | StaticSocketDataProvider first_data; |
| 13231 | first_data.set_connect_data(mock_connect); |
| 13232 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
| 13233 | |
| 13234 | MockRead data_reads[] = { |
| 13235 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 13236 | MockRead(ASYNC, OK), |
| 13237 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13238 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13239 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
| 13240 | |
| 13241 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 13242 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13243 | HttpServerProperties* http_server_properties = |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13244 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13245 | AlternativeService alternative_service(kProtoHTTP2, "", 444); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13246 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13247 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13248 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13249 | alternative_service, expiration); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13250 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13251 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13252 | TestCompletionCallback callback; |
| 13253 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13254 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13255 | // Alternative service is not used, request fails. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13256 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | e3dd56f | 2016-06-01 10:37:11 | [diff] [blame] | 13257 | } |
| 13258 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13259 | TEST_F(HttpNetworkTransactionTest, ClearAlternativeServices) { |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13260 | // Set an alternative service for origin. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13261 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13262 | HttpServerProperties* http_server_properties = |
| 13263 | session->http_server_properties(); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13264 | url::SchemeHostPort test_server("https", "www.example.org", 443); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13265 | AlternativeService alternative_service(kProtoQUIC, "", 80); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13266 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13267 | http_server_properties->SetQuicAlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13268 | test_server, NetworkIsolationKey(), alternative_service, expiration, |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 13269 | session->context().quic_context->params()->supported_versions); |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13270 | EXPECT_EQ(1u, |
| 13271 | http_server_properties |
| 13272 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13273 | .size()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13274 | |
| 13275 | // Send a clear header. |
| 13276 | MockRead data_reads[] = { |
| 13277 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 13278 | MockRead("Alt-Svc: clear\r\n"), |
| 13279 | MockRead("\r\n"), |
| 13280 | MockRead("hello world"), |
| 13281 | MockRead(SYNCHRONOUS, OK), |
| 13282 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13283 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13284 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13285 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13286 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13287 | ssl.ssl_info.cert = |
| 13288 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13289 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13290 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13291 | |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13292 | HttpRequestInfo request; |
| 13293 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13294 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13295 | request.traffic_annotation = |
| 13296 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13297 | |
| 13298 | TestCompletionCallback callback; |
| 13299 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13300 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13301 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13302 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13303 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13304 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13305 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13306 | ASSERT_TRUE(response); |
| 13307 | ASSERT_TRUE(response->headers); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13308 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13309 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13310 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13311 | |
| 13312 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13313 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13314 | EXPECT_EQ("hello world", response_data); |
| 13315 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13316 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13317 | http_server_properties |
| 13318 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13319 | .empty()); |
bnc | 4f57585 | 2015-10-14 18:35:08 | [diff] [blame] | 13320 | } |
| 13321 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13322 | TEST_F(HttpNetworkTransactionTest, HonorMultipleAlternativeServiceHeaders) { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13323 | MockRead data_reads[] = { |
| 13324 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13325 | MockRead("Alt-Svc: h2=\"www.example.com:443\","), |
| 13326 | MockRead("h2=\":1234\"\r\n\r\n"), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13327 | MockRead("hello world"), |
| 13328 | MockRead(SYNCHRONOUS, OK), |
| 13329 | }; |
| 13330 | |
| 13331 | HttpRequestInfo request; |
| 13332 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13333 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13334 | request.traffic_annotation = |
| 13335 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13336 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13337 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13338 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 13339 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13340 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13341 | ssl.ssl_info.cert = |
| 13342 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13343 | ASSERT_TRUE(ssl.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13344 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13345 | |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13346 | TestCompletionCallback callback; |
| 13347 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13348 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13349 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13350 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13351 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13352 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13353 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13354 | url::SchemeHostPort test_server("https", "www.example.org", 443); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13355 | HttpServerProperties* http_server_properties = |
| 13356 | session->http_server_properties(); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13357 | EXPECT_TRUE( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13358 | http_server_properties |
| 13359 | ->GetAlternativeServiceInfos(test_server, NetworkIsolationKey()) |
| 13360 | .empty()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13361 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13362 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13363 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13364 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13365 | ASSERT_TRUE(response); |
| 13366 | ASSERT_TRUE(response->headers); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13367 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13368 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13369 | EXPECT_FALSE(response->was_alpn_negotiated); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13370 | |
| 13371 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13372 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13373 | EXPECT_EQ("hello world", response_data); |
| 13374 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13375 | AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13376 | http_server_properties->GetAlternativeServiceInfos(test_server, |
| 13377 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13378 | ASSERT_EQ(2u, alternative_service_info_vector.size()); |
| 13379 | |
| 13380 | AlternativeService alternative_service(kProtoHTTP2, "www.example.com", 443); |
| 13381 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13382 | alternative_service_info_vector[0].alternative_service()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13383 | AlternativeService alternative_service_2(kProtoHTTP2, "www.example.org", |
| 13384 | 1234); |
| 13385 | EXPECT_EQ(alternative_service_2, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13386 | alternative_service_info_vector[1].alternative_service()); |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13387 | } |
| 13388 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13389 | TEST_F(HttpNetworkTransactionTest, IdentifyQuicBroken) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13390 | url::SchemeHostPort server("https", "origin.example.org", 443); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13391 | HostPortPair alternative("alternative.example.org", 443); |
| 13392 | std::string origin_url = "https://origin.example.org:443"; |
| 13393 | std::string alternative_url = "https://alternative.example.org:443"; |
| 13394 | |
| 13395 | // Negotiate HTTP/1.1 with alternative.example.org. |
| 13396 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13397 | ssl.next_proto = kProtoHTTP11; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13398 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13399 | |
| 13400 | // HTTP/1.1 data for request. |
| 13401 | MockWrite http_writes[] = { |
| 13402 | MockWrite("GET / HTTP/1.1\r\n" |
| 13403 | "Host: alternative.example.org\r\n" |
| 13404 | "Connection: keep-alive\r\n\r\n"), |
| 13405 | }; |
| 13406 | |
| 13407 | MockRead http_reads[] = { |
| 13408 | MockRead("HTTP/1.1 200 OK\r\n" |
| 13409 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 13410 | "Content-Length: 40\r\n\r\n" |
| 13411 | "first HTTP/1.1 response from alternative"), |
| 13412 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13413 | StaticSocketDataProvider http_data(http_reads, http_writes); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13414 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 13415 | |
| 13416 | StaticSocketDataProvider data_refused; |
| 13417 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 13418 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 13419 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13420 | // Set up a QUIC alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13421 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13422 | HttpServerProperties* http_server_properties = |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13423 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13424 | AlternativeService alternative_service(kProtoQUIC, alternative); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13425 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13426 | http_server_properties->SetQuicAlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13427 | server, NetworkIsolationKey(), alternative_service, expiration, |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 13428 | DefaultSupportedQuicVersions()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13429 | // Mark the QUIC alternative service as broken. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13430 | http_server_properties->MarkAlternativeServiceBroken(alternative_service, |
| 13431 | NetworkIsolationKey()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13432 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13433 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 13434 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13435 | request.method = "GET"; |
| 13436 | request.url = GURL(origin_url); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13437 | request.traffic_annotation = |
| 13438 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 13439 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13440 | TestCompletionCallback callback; |
| 13441 | NetErrorDetails details; |
| 13442 | EXPECT_FALSE(details.quic_broken); |
| 13443 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13444 | trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13445 | trans.PopulateNetErrorDetails(&details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13446 | EXPECT_TRUE(details.quic_broken); |
| 13447 | } |
| 13448 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13449 | TEST_F(HttpNetworkTransactionTest, IdentifyQuicNotBroken) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13450 | url::SchemeHostPort server("https", "origin.example.org", 443); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13451 | HostPortPair alternative1("alternative1.example.org", 443); |
| 13452 | HostPortPair alternative2("alternative2.example.org", 443); |
| 13453 | std::string origin_url = "https://origin.example.org:443"; |
| 13454 | std::string alternative_url1 = "https://alternative1.example.org:443"; |
| 13455 | std::string alternative_url2 = "https://alternative2.example.org:443"; |
| 13456 | |
| 13457 | // Negotiate HTTP/1.1 with alternative1.example.org. |
| 13458 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13459 | ssl.next_proto = kProtoHTTP11; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13460 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13461 | |
| 13462 | // HTTP/1.1 data for request. |
| 13463 | MockWrite http_writes[] = { |
| 13464 | MockWrite("GET / HTTP/1.1\r\n" |
| 13465 | "Host: alternative1.example.org\r\n" |
| 13466 | "Connection: keep-alive\r\n\r\n"), |
| 13467 | }; |
| 13468 | |
| 13469 | MockRead http_reads[] = { |
| 13470 | MockRead("HTTP/1.1 200 OK\r\n" |
| 13471 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 13472 | "Content-Length: 40\r\n\r\n" |
| 13473 | "first HTTP/1.1 response from alternative1"), |
| 13474 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13475 | StaticSocketDataProvider http_data(http_reads, http_writes); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13476 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 13477 | |
| 13478 | StaticSocketDataProvider data_refused; |
| 13479 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 13480 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 13481 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13482 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13483 | HttpServerProperties* http_server_properties = |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13484 | session->http_server_properties(); |
| 13485 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13486 | // Set up two QUIC alternative services for server. |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13487 | AlternativeServiceInfoVector alternative_service_info_vector; |
| 13488 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 13489 | |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13490 | AlternativeService alternative_service1(kProtoQUIC, alternative1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13491 | alternative_service_info_vector.push_back( |
| 13492 | AlternativeServiceInfo::CreateQuicAlternativeServiceInfo( |
| 13493 | alternative_service1, expiration, |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 13494 | session->context().quic_context->params()->supported_versions)); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13495 | AlternativeService alternative_service2(kProtoQUIC, alternative2); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13496 | alternative_service_info_vector.push_back( |
| 13497 | AlternativeServiceInfo::CreateQuicAlternativeServiceInfo( |
| 13498 | alternative_service2, expiration, |
Victor Vasiliev | a1e66d7 | 2019-12-05 17:55:38 | [diff] [blame] | 13499 | session->context().quic_context->params()->supported_versions)); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13500 | |
| 13501 | http_server_properties->SetAlternativeServices( |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13502 | server, NetworkIsolationKey(), alternative_service_info_vector); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13503 | |
| 13504 | // Mark one of the QUIC alternative service as broken. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13505 | http_server_properties->MarkAlternativeServiceBroken(alternative_service1, |
| 13506 | NetworkIsolationKey()); |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13507 | EXPECT_EQ(2u, http_server_properties |
| 13508 | ->GetAlternativeServiceInfos(server, NetworkIsolationKey()) |
| 13509 | .size()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13510 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13511 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 13512 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13513 | request.method = "GET"; |
| 13514 | request.url = GURL(origin_url); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13515 | request.traffic_annotation = |
| 13516 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 13517 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13518 | TestCompletionCallback callback; |
| 13519 | NetErrorDetails details; |
| 13520 | EXPECT_FALSE(details.quic_broken); |
| 13521 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13522 | trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13523 | trans.PopulateNetErrorDetails(&details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 13524 | EXPECT_FALSE(details.quic_broken); |
| 13525 | } |
| 13526 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13527 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocolAndFallback) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13528 | HttpRequestInfo request; |
| 13529 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13530 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13531 | request.traffic_annotation = |
| 13532 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13533 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13534 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13535 | StaticSocketDataProvider first_data; |
| 13536 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13537 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13538 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13539 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13540 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13541 | |
| 13542 | MockRead data_reads[] = { |
| 13543 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13544 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13545 | MockRead(ASYNC, OK), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13546 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13547 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13548 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13549 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13550 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13551 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13552 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13553 | session->http_server_properties(); |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 13554 | const url::SchemeHostPort server(request.url); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13555 | // Port must be < 1024, or the header will be ignored (since initial port was |
| 13556 | // port 80 (another restricted port). |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13557 | // Port is ignored by MockConnect anyway. |
| 13558 | const AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13559 | 666); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13560 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13561 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13562 | server, NetworkIsolationKey(), alternative_service, expiration); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13563 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13564 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13565 | TestCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13566 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13567 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13568 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13569 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13570 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13571 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13572 | ASSERT_TRUE(response); |
| 13573 | ASSERT_TRUE(response->headers); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13574 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13575 | |
| 13576 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13577 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13578 | EXPECT_EQ("hello world", response_data); |
| 13579 | |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13580 | const AlternativeServiceInfoVector alternative_service_info_vector = |
Matt Menke | 3233d8f2 | 2019-08-20 21:01:49 | [diff] [blame] | 13581 | http_server_properties->GetAlternativeServiceInfos(server, |
| 13582 | NetworkIsolationKey()); |
zhongyi | c4de0303 | 2017-05-19 04:07:34 | [diff] [blame] | 13583 | ASSERT_EQ(1u, alternative_service_info_vector.size()); |
| 13584 | EXPECT_EQ(alternative_service, |
zhongyi | 422ce35 | 2017-06-09 23:28:54 | [diff] [blame] | 13585 | alternative_service_info_vector[0].alternative_service()); |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 13586 | EXPECT_TRUE(http_server_properties->IsAlternativeServiceBroken( |
| 13587 | alternative_service, NetworkIsolationKey())); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 13588 | } |
| 13589 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13590 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13591 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13592 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13593 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13594 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedBlocked) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13595 | HttpRequestInfo restricted_port_request; |
| 13596 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13597 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13598 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13599 | restricted_port_request.traffic_annotation = |
| 13600 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13601 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13602 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13603 | StaticSocketDataProvider first_data; |
| 13604 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13605 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13606 | |
| 13607 | MockRead data_reads[] = { |
| 13608 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13609 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13610 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13611 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13612 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13613 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13614 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13615 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13616 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13617 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13618 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13619 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13620 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13621 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13622 | const int kUnrestrictedAlternatePort = 1024; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13623 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13624 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13625 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13626 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13627 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13628 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13629 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13630 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13631 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13632 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13633 | int rv = trans.Start(&restricted_port_request, callback.callback(), |
| 13634 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13635 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13636 | // Invalid change to unrestricted port should fail. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13637 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_REFUSED)); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13638 | } |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13639 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13640 | // Ensure that we are allowed to redirect traffic via an alternate protocol to |
| 13641 | // an unrestricted (port >= 1024) when the original traffic was on a restricted |
| 13642 | // port (port < 1024) if we set |enable_user_alternate_protocol_ports|. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13643 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedPermitted) { |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13644 | session_deps_.enable_user_alternate_protocol_ports = true; |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13645 | |
| 13646 | HttpRequestInfo restricted_port_request; |
| 13647 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13648 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13649 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13650 | restricted_port_request.traffic_annotation = |
| 13651 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13652 | |
| 13653 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
| 13654 | StaticSocketDataProvider first_data; |
| 13655 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13656 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13657 | |
| 13658 | MockRead data_reads[] = { |
| 13659 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13660 | MockRead("hello world"), |
| 13661 | MockRead(ASYNC, OK), |
| 13662 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13663 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13664 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13665 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13666 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13667 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13668 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13669 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13670 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13671 | HttpServerProperties* http_server_properties = |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13672 | session->http_server_properties(); |
| 13673 | const int kUnrestrictedAlternatePort = 1024; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13674 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13675 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13676 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13677 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13678 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13679 | alternative_service, expiration); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13680 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13681 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13682 | TestCompletionCallback callback; |
| 13683 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13684 | EXPECT_EQ(ERR_IO_PENDING, |
| 13685 | trans.Start(&restricted_port_request, callback.callback(), |
| 13686 | NetLogWithSource())); |
[email protected] | c54c696 | 2013-02-01 04:53:19 | [diff] [blame] | 13687 | // Change to unrestricted port should succeed. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13688 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13689 | } |
| 13690 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13691 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13692 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13693 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13694 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13695 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedAllowed) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13696 | HttpRequestInfo restricted_port_request; |
| 13697 | restricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13698 | restricted_port_request.url = GURL("https://www.example.org:1023/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13699 | restricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13700 | restricted_port_request.traffic_annotation = |
| 13701 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13702 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13703 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13704 | StaticSocketDataProvider first_data; |
| 13705 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13706 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13707 | |
| 13708 | MockRead data_reads[] = { |
| 13709 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13710 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13711 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13712 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13713 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13714 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13715 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13716 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 13717 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13718 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13719 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13720 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13721 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13722 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13723 | const int kRestrictedAlternatePort = 80; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13724 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13725 | kRestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13726 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13727 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13728 | url::SchemeHostPort(restricted_port_request.url), NetworkIsolationKey(), |
| 13729 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13730 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13731 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13732 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13733 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13734 | int rv = trans.Start(&restricted_port_request, callback.callback(), |
| 13735 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13736 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13737 | // Valid change to restricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13738 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13739 | } |
| 13740 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13741 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13742 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13743 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13744 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13745 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed1) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13746 | HttpRequestInfo unrestricted_port_request; |
| 13747 | unrestricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13748 | unrestricted_port_request.url = GURL("https://www.example.org:1024/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13749 | unrestricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13750 | unrestricted_port_request.traffic_annotation = |
| 13751 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13752 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13753 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13754 | StaticSocketDataProvider first_data; |
| 13755 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13756 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13757 | |
| 13758 | MockRead data_reads[] = { |
| 13759 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13760 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13761 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13762 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13763 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13764 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13765 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13766 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13767 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13768 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13769 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13770 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13771 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13772 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13773 | const int kRestrictedAlternatePort = 80; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13774 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13775 | kRestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13776 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13777 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13778 | url::SchemeHostPort(unrestricted_port_request.url), NetworkIsolationKey(), |
| 13779 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13780 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13781 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13782 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13783 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13784 | int rv = trans.Start(&unrestricted_port_request, callback.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13785 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13786 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13787 | // Valid change to restricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13788 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13789 | } |
| 13790 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13791 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
| 13792 | // to an unrestricted (port >= 1024) when the original traffic was on a |
| 13793 | // restricted port (port < 1024). Ensure that we can redirect in all other |
| 13794 | // cases. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13795 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed2) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13796 | HttpRequestInfo unrestricted_port_request; |
| 13797 | unrestricted_port_request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13798 | unrestricted_port_request.url = GURL("https://www.example.org:1024/"); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13799 | unrestricted_port_request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13800 | unrestricted_port_request.traffic_annotation = |
| 13801 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13802 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13803 | MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13804 | StaticSocketDataProvider first_data; |
| 13805 | first_data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13806 | session_deps_.socket_factory->AddSocketDataProvider(&first_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13807 | |
| 13808 | MockRead data_reads[] = { |
| 13809 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13810 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13811 | MockRead(ASYNC, OK), |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13812 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13813 | StaticSocketDataProvider second_data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13814 | session_deps_.socket_factory->AddSocketDataProvider(&second_data); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13815 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13816 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 13817 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 13818 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13819 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13820 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13821 | HttpServerProperties* http_server_properties = |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 13822 | session->http_server_properties(); |
bnc | 0bbb0262 | 2015-07-23 10:06:22 | [diff] [blame] | 13823 | const int kUnrestrictedAlternatePort = 1025; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13824 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13825 | kUnrestrictedAlternatePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13826 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13827 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13828 | url::SchemeHostPort(unrestricted_port_request.url), NetworkIsolationKey(), |
| 13829 | alternative_service, expiration); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13830 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13831 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13832 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13833 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13834 | int rv = trans.Start(&unrestricted_port_request, callback.callback(), |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13835 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13836 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13837 | // Valid change to an unrestricted port should pass. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13838 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 13839 | } |
| 13840 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 13841 | // Ensure that we are not allowed to redirect traffic via an alternate protocol |
Xida Chen | 9bfe0b6 | 2018-04-24 19:52:21 | [diff] [blame] | 13842 | // to an unsafe port, and that we resume the second HttpStreamFactory::Job once |
| 13843 | // the alternate protocol request fails. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13844 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolUnsafeBlocked) { |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13845 | HttpRequestInfo request; |
| 13846 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 13847 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13848 | request.traffic_annotation = |
| 13849 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13850 | |
| 13851 | // The alternate protocol request will error out before we attempt to connect, |
| 13852 | // so only the standard HTTP request will try to connect. |
| 13853 | MockRead data_reads[] = { |
| 13854 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 13855 | MockRead("hello world"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 13856 | MockRead(ASYNC, OK), |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13857 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13858 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13859 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13860 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13861 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13862 | |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 13863 | HttpServerProperties* http_server_properties = |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13864 | session->http_server_properties(); |
| 13865 | const int kUnsafePort = 7; |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 13866 | AlternativeService alternative_service(kProtoHTTP2, "www.example.org", |
| 13867 | kUnsafePort); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 13868 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 13869 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 13870 | url::SchemeHostPort(request.url), NetworkIsolationKey(), |
| 13871 | alternative_service, expiration); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13872 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13873 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13874 | TestCompletionCallback callback; |
| 13875 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13876 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13877 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13878 | // The HTTP request should succeed. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13879 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13880 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13881 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13882 | ASSERT_TRUE(response); |
| 13883 | ASSERT_TRUE(response->headers); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13884 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13885 | |
| 13886 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 13887 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 13888 | EXPECT_EQ("hello world", response_data); |
| 13889 | } |
| 13890 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13891 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13892 | HttpRequestInfo request; |
| 13893 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13894 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13895 | request.traffic_annotation = |
| 13896 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13897 | |
| 13898 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13899 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13900 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13901 | MockRead("\r\n"), |
| 13902 | MockRead("hello world"), |
| 13903 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 13904 | MockRead(ASYNC, OK)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13905 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13906 | StaticSocketDataProvider first_transaction(data_reads, |
| 13907 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13908 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13909 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 13910 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13911 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13912 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 13913 | AddSSLSocketData(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13914 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13915 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13916 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13917 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13918 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 13919 | spdy::SpdySerializedFrame resp( |
| 13920 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 13921 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13922 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 13923 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13924 | }; |
| 13925 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13926 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13927 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13928 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 13929 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13930 | StaticSocketDataProvider hanging_non_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13931 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 13932 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 13933 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13934 | &hanging_non_alternate_protocol_socket); |
| 13935 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 13936 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13937 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 13938 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 13939 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 13940 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13941 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13942 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13943 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13944 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13945 | |
| 13946 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13947 | ASSERT_TRUE(response); |
| 13948 | ASSERT_TRUE(response->headers); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13949 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 13950 | |
| 13951 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13952 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13953 | EXPECT_EQ("hello world", response_data); |
| 13954 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 13955 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 13956 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13957 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 13958 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13959 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 13960 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13961 | |
| 13962 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 13963 | ASSERT_TRUE(response); |
| 13964 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 13965 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 13966 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 13967 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13968 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 13969 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13970 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 13971 | } |
| 13972 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 13973 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolWithSpdyLateBinding) { |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13974 | HttpRequestInfo request; |
| 13975 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13976 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 13977 | request.traffic_annotation = |
| 13978 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13979 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13980 | // First transaction receives Alt-Svc header over HTTP/1.1. |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13981 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13982 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 13983 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 13984 | MockRead("\r\n"), |
| 13985 | MockRead("hello world"), |
| 13986 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 13987 | MockRead(ASYNC, OK), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13988 | }; |
| 13989 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 13990 | StaticSocketDataProvider http11_data(data_reads, base::span<MockWrite>()); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13991 | session_deps_.socket_factory->AddSocketDataProvider(&http11_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13992 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13993 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 13994 | ssl_http11.ssl_info.cert = |
| 13995 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 13996 | ASSERT_TRUE(ssl_http11.ssl_info.cert); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 13997 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
| 13998 | |
| 13999 | // Second transaction starts an alternative and a non-alternative Job. |
| 14000 | // Both sockets hang. |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 14001 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14002 | StaticSocketDataProvider hanging_socket1; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14003 | hanging_socket1.set_connect_data(never_finishing_connect); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14004 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket1); |
| 14005 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14006 | StaticSocketDataProvider hanging_socket2; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14007 | hanging_socket2.set_connect_data(never_finishing_connect); |
| 14008 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket2); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14009 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14010 | // Third transaction starts an alternative and a non-alternative job. |
| 14011 | // The non-alternative job hangs, but the alternative one succeeds. |
| 14012 | // The second transaction, still pending, binds to this socket. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14013 | spdy::SpdySerializedFrame req1( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14014 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14015 | spdy::SpdySerializedFrame req2( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14016 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 3, LOWEST)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14017 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14018 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 1), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14019 | }; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14020 | spdy::SpdySerializedFrame resp1( |
| 14021 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14022 | spdy::SpdySerializedFrame data1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14023 | spdy::SpdySerializedFrame resp2( |
| 14024 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14025 | spdy::SpdySerializedFrame data2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14026 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14027 | CreateMockRead(resp1, 2), CreateMockRead(data1, 3), |
| 14028 | CreateMockRead(resp2, 4), CreateMockRead(data2, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 14029 | MockRead(ASYNC, 0, 6), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14030 | }; |
| 14031 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14032 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14033 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14034 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14035 | AddSSLSocketData(); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14036 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14037 | StaticSocketDataProvider hanging_socket3; |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14038 | hanging_socket3.set_connect_data(never_finishing_connect); |
| 14039 | session_deps_.socket_factory->AddSocketDataProvider(&hanging_socket3); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14040 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14041 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14042 | TestCompletionCallback callback1; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 14043 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14044 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14045 | int rv = trans1.Start(&request, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14046 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14047 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14048 | |
| 14049 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14050 | ASSERT_TRUE(response); |
| 14051 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14052 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14053 | |
| 14054 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14055 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14056 | EXPECT_EQ("hello world", response_data); |
| 14057 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14058 | TestCompletionCallback callback2; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 14059 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14060 | rv = trans2.Start(&request, callback2.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14061 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14062 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14063 | TestCompletionCallback callback3; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 14064 | HttpNetworkTransaction trans3(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14065 | rv = trans3.Start(&request, callback3.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14066 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14067 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14068 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 14069 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14070 | |
| 14071 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14072 | ASSERT_TRUE(response); |
| 14073 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14074 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14075 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14076 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14077 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14078 | EXPECT_EQ("hello!", response_data); |
| 14079 | |
| 14080 | response = trans3.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14081 | ASSERT_TRUE(response); |
| 14082 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14083 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14084 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14085 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14086 | ASSERT_THAT(ReadTransaction(&trans3, &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14087 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14088 | } |
| 14089 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14090 | TEST_F(HttpNetworkTransactionTest, StallAlternativeServiceForNpnSpdy) { |
Bence Béky | bcae3709 | 2018-06-12 04:18:53 | [diff] [blame] | 14091 | session_deps_.host_resolver->set_synchronous_mode(true); |
| 14092 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14093 | HttpRequestInfo request; |
| 14094 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14095 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 14096 | request.traffic_annotation = |
| 14097 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14098 | |
| 14099 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14100 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 14101 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14102 | MockRead("\r\n"), |
| 14103 | MockRead("hello world"), |
| 14104 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 14105 | MockRead(ASYNC, OK), |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14106 | }; |
| 14107 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14108 | StaticSocketDataProvider first_transaction(data_reads, |
| 14109 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14110 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14111 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 14112 | SSLSocketDataProvider ssl(ASYNC, OK); |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 14113 | ssl.ssl_info.cert = |
| 14114 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 14115 | ASSERT_TRUE(ssl.ssl_info.cert); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14116 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14117 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 14118 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14119 | StaticSocketDataProvider hanging_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14120 | hanging_alternate_protocol_socket.set_connect_data( |
| 14121 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14122 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14123 | &hanging_alternate_protocol_socket); |
| 14124 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14125 | // 2nd request is just a copy of the first one, over HTTP/1.1 again. |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14126 | StaticSocketDataProvider second_transaction(data_reads, |
| 14127 | base::span<MockWrite>()); |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 14128 | session_deps_.socket_factory->AddSocketDataProvider(&second_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14129 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14130 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14131 | TestCompletionCallback callback; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14132 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14133 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14134 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14135 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14136 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14137 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14138 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14139 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14140 | |
| 14141 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14142 | ASSERT_TRUE(response); |
| 14143 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14144 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14145 | |
| 14146 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14147 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14148 | EXPECT_EQ("hello world", response_data); |
| 14149 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14150 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14151 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14152 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14153 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14154 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14155 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14156 | |
| 14157 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14158 | ASSERT_TRUE(response); |
| 14159 | ASSERT_TRUE(response->headers); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14160 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14161 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14162 | EXPECT_FALSE(response->was_alpn_negotiated); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14163 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14164 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14165 | EXPECT_EQ("hello world", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14166 | } |
| 14167 | |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14168 | // Test that proxy is resolved using the origin url, |
| 14169 | // regardless of the alternative server. |
| 14170 | TEST_F(HttpNetworkTransactionTest, UseOriginNotAlternativeForProxy) { |
| 14171 | // Configure proxy to bypass www.example.org, which is the origin URL. |
| 14172 | ProxyConfig proxy_config; |
| 14173 | proxy_config.proxy_rules().ParseFromString("myproxy:70"); |
| 14174 | proxy_config.proxy_rules().bypass_rules.AddRuleFromString("www.example.org"); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 14175 | auto proxy_config_service = std::make_unique<ProxyConfigServiceFixed>( |
| 14176 | ProxyConfigWithAnnotation(proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14177 | |
| 14178 | CapturingProxyResolver capturing_proxy_resolver; |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14179 | auto proxy_resolver_factory = std::make_unique<CapturingProxyResolverFactory>( |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14180 | &capturing_proxy_resolver); |
| 14181 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 14182 | RecordingTestNetLog net_log; |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14183 | |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 14184 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 14185 | std::make_unique<ConfiguredProxyResolutionService>( |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 14186 | std::move(proxy_config_service), std::move(proxy_resolver_factory), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 14187 | &net_log, /*quick_check_enabled=*/true); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14188 | |
| 14189 | session_deps_.net_log = &net_log; |
| 14190 | |
| 14191 | // Configure alternative service with a hostname that is not bypassed by the |
| 14192 | // proxy. |
| 14193 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 14194 | HttpServerProperties* http_server_properties = |
| 14195 | session->http_server_properties(); |
| 14196 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 14197 | HostPortPair alternative("www.example.com", 443); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 14198 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14199 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 14200 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 14201 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14202 | |
| 14203 | // Non-alternative job should hang. |
| 14204 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14205 | StaticSocketDataProvider hanging_alternate_protocol_socket; |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14206 | hanging_alternate_protocol_socket.set_connect_data(never_finishing_connect); |
| 14207 | session_deps_.socket_factory->AddSocketDataProvider( |
| 14208 | &hanging_alternate_protocol_socket); |
| 14209 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14210 | AddSSLSocketData(); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14211 | |
| 14212 | HttpRequestInfo request; |
| 14213 | request.method = "GET"; |
| 14214 | request.url = GURL("https://www.example.org/"); |
| 14215 | request.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 14216 | request.traffic_annotation = |
| 14217 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14218 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14219 | spdy::SpdySerializedFrame req( |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14220 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
| 14221 | |
| 14222 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
| 14223 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14224 | spdy::SpdySerializedFrame resp( |
| 14225 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 14226 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14227 | MockRead spdy_reads[] = { |
| 14228 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
| 14229 | }; |
| 14230 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14231 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14232 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
| 14233 | |
| 14234 | TestCompletionCallback callback; |
| 14235 | |
| 14236 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 14237 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14238 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14239 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 14240 | |
| 14241 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 14242 | ASSERT_TRUE(response); |
| 14243 | ASSERT_TRUE(response->headers); |
| 14244 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 14245 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14246 | EXPECT_TRUE(response->was_alpn_negotiated); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14247 | |
| 14248 | std::string response_data; |
| 14249 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 14250 | EXPECT_EQ("hello!", response_data); |
| 14251 | |
| 14252 | // Origin host bypasses proxy, no resolution should have happened. |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 14253 | ASSERT_TRUE(capturing_proxy_resolver.lookup_info().empty()); |
bnc | 2e88478 | 2016-08-11 19:45:19 | [diff] [blame] | 14254 | } |
| 14255 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14256 | TEST_F(HttpNetworkTransactionTest, UseAlternativeServiceForTunneledNpnSpdy) { |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14257 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 14258 | proxy_config.set_auto_detect(true); |
| 14259 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 14260 | |
sammc | 5dd160c | 2015-04-02 02:43:13 | [diff] [blame] | 14261 | CapturingProxyResolver capturing_proxy_resolver; |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 14262 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 14263 | std::make_unique<ConfiguredProxyResolutionService>( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 14264 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 14265 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
| 14266 | std::make_unique<CapturingProxyResolverFactory>( |
| 14267 | &capturing_proxy_resolver), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 14268 | nullptr, /*quick_check_enabled=*/true); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 14269 | RecordingTestNetLog net_log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14270 | session_deps_.net_log = &net_log; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14271 | |
| 14272 | HttpRequestInfo request; |
| 14273 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14274 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 14275 | request.traffic_annotation = |
| 14276 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14277 | |
| 14278 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14279 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 14280 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14281 | MockRead("\r\n"), |
| 14282 | MockRead("hello world"), |
| 14283 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 14284 | MockRead(ASYNC, OK), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14285 | }; |
| 14286 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14287 | StaticSocketDataProvider first_transaction(data_reads, |
| 14288 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14289 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14290 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 14291 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14292 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14293 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14294 | AddSSLSocketData(); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14295 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14296 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14297 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14298 | MockWrite spdy_writes[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 14299 | MockWrite(ASYNC, 0, |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 14300 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 14301 | "Host: www.example.org:443\r\n" |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 14302 | "Proxy-Connection: keep-alive\r\n\r\n"), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14303 | CreateMockWrite(req, 2), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14304 | }; |
| 14305 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 14306 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 14307 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14308 | spdy::SpdySerializedFrame resp( |
| 14309 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14310 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14311 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14312 | MockRead(ASYNC, 1, kCONNECTResponse), CreateMockRead(resp, 3), |
| 14313 | CreateMockRead(data, 4), MockRead(SYNCHRONOUS, ERR_IO_PENDING, 5), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14314 | }; |
| 14315 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14316 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14317 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14318 | |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 14319 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14320 | StaticSocketDataProvider hanging_non_alternate_protocol_socket; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14321 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 14322 | never_finishing_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14323 | session_deps_.socket_factory->AddSocketDataProvider( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 14324 | &hanging_non_alternate_protocol_socket); |
| 14325 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 14326 | TestCompletionCallback callback; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14327 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14328 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14329 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14330 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14331 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14332 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14333 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14334 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 14335 | |
| 14336 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 14337 | ASSERT_TRUE(response); |
| 14338 | ASSERT_TRUE(response->headers); |
| 14339 | EXPECT_EQ("HTTP/0.9 200 OK", response->headers->GetStatusLine()); |
| 14340 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14341 | EXPECT_TRUE(response->was_alpn_negotiated); |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14342 | |
| 14343 | std::string response_data; |
| 14344 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 14345 | EXPECT_EQ("hello world", response_data); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14346 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14347 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14348 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14349 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14350 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14351 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14352 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14353 | |
mmenke | a2dcd3bf | 2016-08-16 21:49:41 | [diff] [blame] | 14354 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14355 | ASSERT_TRUE(response); |
| 14356 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14357 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 14358 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14359 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14360 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14361 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14362 | EXPECT_EQ("hello!", response_data); |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 14363 | ASSERT_EQ(2u, capturing_proxy_resolver.lookup_info().size()); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14364 | EXPECT_EQ("https://www.example.org/", |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 14365 | capturing_proxy_resolver.lookup_info()[0].url.spec()); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 14366 | EXPECT_EQ("https://www.example.org/", |
Matt Menke | 8045afd | 2019-11-14 20:31:19 | [diff] [blame] | 14367 | capturing_proxy_resolver.lookup_info()[1].url.spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14368 | |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 14369 | LoadTimingInfo load_timing_info; |
| 14370 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 14371 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 14372 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14373 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 14374 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14375 | TEST_F(HttpNetworkTransactionTest, |
bnc | 1c196c6e | 2016-05-28 13:51:48 | [diff] [blame] | 14376 | UseAlternativeServiceForNpnSpdyWithExistingSpdySession) { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14377 | HttpRequestInfo request; |
| 14378 | request.method = "GET"; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14379 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 14380 | request.traffic_annotation = |
| 14381 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14382 | |
| 14383 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14384 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 14385 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 14386 | MockRead("\r\n"), |
| 14387 | MockRead("hello world"), |
| 14388 | MockRead(ASYNC, OK), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14389 | }; |
| 14390 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14391 | StaticSocketDataProvider first_transaction(data_reads, |
| 14392 | base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14393 | session_deps_.socket_factory->AddSocketDataProvider(&first_transaction); |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14394 | SSLSocketDataProvider ssl_http11(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 14395 | ssl_http11.next_proto = kProtoHTTP11; |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14396 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_http11); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14397 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 14398 | AddSSLSocketData(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14399 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14400 | spdy::SpdySerializedFrame req( |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 14401 | spdy_util_.ConstructSpdyGet("https://www.example.org/", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14402 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14403 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14404 | spdy::SpdySerializedFrame resp( |
| 14405 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 14406 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14407 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 14408 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14409 | }; |
| 14410 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 14411 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 14412 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14413 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 14414 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14415 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 14416 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14417 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14418 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14419 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14420 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14421 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14422 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14423 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14424 | |
| 14425 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14426 | ASSERT_TRUE(response); |
| 14427 | ASSERT_TRUE(response->headers); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14428 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 14429 | |
| 14430 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14431 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14432 | EXPECT_EQ("hello world", response_data); |
| 14433 | |
| 14434 | // Set up an initial SpdySession in the pool to reuse. |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 14435 | HostPortPair host_port_pair("www.example.org", 443); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 14436 | SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 14437 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 14438 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 14439 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 14440 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 14441 | CreateSpdySession(session.get(), key, NetLogWithSource()); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 14442 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 14443 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 14444 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14445 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 14446 | rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14447 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 14448 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14449 | |
| 14450 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 14451 | ASSERT_TRUE(response); |
| 14452 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 14453 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 14454 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 14455 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14456 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 14457 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 14458 | EXPECT_EQ("hello!", response_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 14459 | } |
| 14460 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14461 | // GenerateAuthToken is a mighty big test. |
| 14462 | // It tests all permutation of GenerateAuthToken behavior: |
| 14463 | // - Synchronous and Asynchronous completion. |
| 14464 | // - OK or error on completion. |
| 14465 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 14466 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 14467 | // - Non-authenticating and authenticating backend. |
| 14468 | // |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 14469 | // In all, there are 44 reasonable permuations (for example, if there are |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14470 | // problems generating an auth token for an authenticating proxy, we don't |
| 14471 | // need to test all permutations of the backend server). |
| 14472 | // |
| 14473 | // The test proceeds by going over each of the configuration cases, and |
| 14474 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 14475 | // specifies both the configuration for the test as well as the expectations |
| 14476 | // for the results. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 14477 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 14478 | static const char kServer[] = "http://www.example.com"; |
| 14479 | static const char kSecureServer[] = "https://www.example.com"; |
| 14480 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14481 | |
| 14482 | enum AuthTiming { |
| 14483 | AUTH_NONE, |
| 14484 | AUTH_SYNC, |
| 14485 | AUTH_ASYNC, |
| 14486 | }; |
| 14487 | |
| 14488 | const MockWrite kGet( |
| 14489 | "GET / HTTP/1.1\r\n" |
| 14490 | "Host: www.example.com\r\n" |
| 14491 | "Connection: keep-alive\r\n\r\n"); |
| 14492 | const MockWrite kGetProxy( |
| 14493 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14494 | "Host: www.example.com\r\n" |
| 14495 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 14496 | const MockWrite kGetAuth( |
| 14497 | "GET / HTTP/1.1\r\n" |
| 14498 | "Host: www.example.com\r\n" |
| 14499 | "Connection: keep-alive\r\n" |
| 14500 | "Authorization: auth_token\r\n\r\n"); |
| 14501 | const MockWrite kGetProxyAuth( |
| 14502 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14503 | "Host: www.example.com\r\n" |
| 14504 | "Proxy-Connection: keep-alive\r\n" |
| 14505 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 14506 | const MockWrite kGetAuthThroughProxy( |
| 14507 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14508 | "Host: www.example.com\r\n" |
| 14509 | "Proxy-Connection: keep-alive\r\n" |
| 14510 | "Authorization: auth_token\r\n\r\n"); |
| 14511 | const MockWrite kGetAuthWithProxyAuth( |
| 14512 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 14513 | "Host: www.example.com\r\n" |
| 14514 | "Proxy-Connection: keep-alive\r\n" |
| 14515 | "Proxy-Authorization: auth_token\r\n" |
| 14516 | "Authorization: auth_token\r\n\r\n"); |
| 14517 | const MockWrite kConnect( |
| 14518 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 14519 | "Host: www.example.com:443\r\n" |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14520 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 14521 | const MockWrite kConnectProxyAuth( |
| 14522 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 14523 | "Host: www.example.com:443\r\n" |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14524 | "Proxy-Connection: keep-alive\r\n" |
| 14525 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 14526 | |
| 14527 | const MockRead kSuccess( |
| 14528 | "HTTP/1.1 200 OK\r\n" |
| 14529 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14530 | "Content-Length: 3\r\n\r\n" |
| 14531 | "Yes"); |
| 14532 | const MockRead kFailure( |
| 14533 | "Should not be called."); |
| 14534 | const MockRead kServerChallenge( |
| 14535 | "HTTP/1.1 401 Unauthorized\r\n" |
| 14536 | "WWW-Authenticate: Mock realm=server\r\n" |
| 14537 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14538 | "Content-Length: 14\r\n\r\n" |
| 14539 | "Unauthorized\r\n"); |
| 14540 | const MockRead kProxyChallenge( |
| 14541 | "HTTP/1.1 407 Unauthorized\r\n" |
| 14542 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 14543 | "Proxy-Connection: close\r\n" |
| 14544 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 14545 | "Content-Length: 14\r\n\r\n" |
| 14546 | "Unauthorized\r\n"); |
| 14547 | const MockRead kProxyConnected( |
| 14548 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 14549 | |
| 14550 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 14551 | // no constructors, but the C++ compiler on Windows warns about |
| 14552 | // unspecified data in compound literals. So, moved to using constructors, |
| 14553 | // and TestRound's created with the default constructor should not be used. |
| 14554 | struct TestRound { |
| 14555 | TestRound() |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14556 | : expected_rv(ERR_UNEXPECTED), |
| 14557 | extra_write(nullptr), |
| 14558 | extra_read(nullptr) {} |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 14559 | TestRound(const MockWrite& write_arg, |
| 14560 | const MockRead& read_arg, |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14561 | int expected_rv_arg) |
| 14562 | : write(write_arg), |
| 14563 | read(read_arg), |
| 14564 | expected_rv(expected_rv_arg), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 14565 | extra_write(nullptr), |
| 14566 | extra_read(nullptr) {} |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14567 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 14568 | int expected_rv_arg, const MockWrite* extra_write_arg, |
[email protected] | f871ee15 | 2012-07-27 19:02:01 | [diff] [blame] | 14569 | const MockRead* extra_read_arg) |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14570 | : write(write_arg), |
| 14571 | read(read_arg), |
| 14572 | expected_rv(expected_rv_arg), |
| 14573 | extra_write(extra_write_arg), |
| 14574 | extra_read(extra_read_arg) { |
| 14575 | } |
| 14576 | MockWrite write; |
| 14577 | MockRead read; |
| 14578 | int expected_rv; |
| 14579 | const MockWrite* extra_write; |
| 14580 | const MockRead* extra_read; |
| 14581 | }; |
| 14582 | |
| 14583 | static const int kNoSSL = 500; |
| 14584 | |
| 14585 | struct TestConfig { |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14586 | int line_number; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 14587 | const char* const proxy_url; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14588 | AuthTiming proxy_auth_timing; |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14589 | int first_generate_proxy_token_rv; |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 14590 | const char* const server_url; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14591 | AuthTiming server_auth_timing; |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14592 | int first_generate_server_token_rv; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14593 | int num_auth_rounds; |
| 14594 | int first_ssl_round; |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14595 | TestRound rounds[4]; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14596 | } test_configs[] = { |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14597 | // Non-authenticating HTTP server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14598 | {__LINE__, |
| 14599 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14600 | AUTH_NONE, |
| 14601 | OK, |
| 14602 | kServer, |
| 14603 | AUTH_NONE, |
| 14604 | OK, |
| 14605 | 1, |
| 14606 | kNoSSL, |
| 14607 | {TestRound(kGet, kSuccess, OK)}}, |
| 14608 | // Authenticating HTTP server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14609 | {__LINE__, |
| 14610 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14611 | AUTH_NONE, |
| 14612 | OK, |
| 14613 | kServer, |
| 14614 | AUTH_SYNC, |
| 14615 | OK, |
| 14616 | 2, |
| 14617 | kNoSSL, |
| 14618 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14619 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14620 | {__LINE__, |
| 14621 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14622 | AUTH_NONE, |
| 14623 | OK, |
| 14624 | kServer, |
| 14625 | AUTH_SYNC, |
| 14626 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14627 | 3, |
| 14628 | kNoSSL, |
| 14629 | {TestRound(kGet, kServerChallenge, OK), |
| 14630 | TestRound(kGet, kServerChallenge, OK), |
| 14631 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14632 | {__LINE__, |
| 14633 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14634 | AUTH_NONE, |
| 14635 | OK, |
| 14636 | kServer, |
| 14637 | AUTH_SYNC, |
| 14638 | ERR_UNSUPPORTED_AUTH_SCHEME, |
| 14639 | 2, |
| 14640 | kNoSSL, |
| 14641 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14642 | {__LINE__, |
| 14643 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14644 | AUTH_NONE, |
| 14645 | OK, |
| 14646 | kServer, |
| 14647 | AUTH_SYNC, |
| 14648 | ERR_UNDOCUMENTED_SECURITY_LIBRARY_STATUS, |
| 14649 | 2, |
| 14650 | kNoSSL, |
| 14651 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14652 | {__LINE__, |
| 14653 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14654 | AUTH_SYNC, |
| 14655 | ERR_FAILED, |
| 14656 | kServer, |
| 14657 | AUTH_NONE, |
| 14658 | OK, |
| 14659 | 2, |
| 14660 | kNoSSL, |
| 14661 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14662 | TestRound(kGetProxy, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14663 | {__LINE__, |
| 14664 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14665 | AUTH_ASYNC, |
| 14666 | ERR_FAILED, |
| 14667 | kServer, |
| 14668 | AUTH_NONE, |
| 14669 | OK, |
| 14670 | 2, |
| 14671 | kNoSSL, |
| 14672 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14673 | TestRound(kGetProxy, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14674 | {__LINE__, |
| 14675 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14676 | AUTH_NONE, |
| 14677 | OK, |
| 14678 | kServer, |
| 14679 | AUTH_SYNC, |
| 14680 | ERR_FAILED, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14681 | 2, |
| 14682 | kNoSSL, |
| 14683 | {TestRound(kGet, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14684 | TestRound(kGet, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14685 | {__LINE__, |
| 14686 | nullptr, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14687 | AUTH_NONE, |
| 14688 | OK, |
| 14689 | kServer, |
| 14690 | AUTH_ASYNC, |
| 14691 | ERR_FAILED, |
| 14692 | 2, |
| 14693 | kNoSSL, |
| 14694 | {TestRound(kGet, kServerChallenge, OK), |
| 14695 | TestRound(kGet, kFailure, ERR_FAILED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14696 | {__LINE__, |
| 14697 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14698 | AUTH_NONE, |
| 14699 | OK, |
| 14700 | kServer, |
| 14701 | AUTH_ASYNC, |
| 14702 | OK, |
| 14703 | 2, |
| 14704 | kNoSSL, |
| 14705 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14706 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14707 | {__LINE__, |
| 14708 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14709 | AUTH_NONE, |
| 14710 | OK, |
| 14711 | kServer, |
| 14712 | AUTH_ASYNC, |
| 14713 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14714 | 3, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14715 | kNoSSL, |
| 14716 | {TestRound(kGet, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14717 | // The second round uses a HttpAuthHandlerMock that always succeeds. |
| 14718 | TestRound(kGet, kServerChallenge, OK), |
| 14719 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14720 | // Non-authenticating HTTP server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14721 | {__LINE__, |
| 14722 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14723 | AUTH_NONE, |
| 14724 | OK, |
| 14725 | kServer, |
| 14726 | AUTH_NONE, |
| 14727 | OK, |
| 14728 | 1, |
| 14729 | kNoSSL, |
| 14730 | {TestRound(kGetProxy, kSuccess, OK)}}, |
| 14731 | // Authenticating HTTP server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14732 | {__LINE__, |
| 14733 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14734 | AUTH_NONE, |
| 14735 | OK, |
| 14736 | kServer, |
| 14737 | AUTH_SYNC, |
| 14738 | OK, |
| 14739 | 2, |
| 14740 | kNoSSL, |
| 14741 | {TestRound(kGetProxy, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14742 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14743 | {__LINE__, |
| 14744 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14745 | AUTH_NONE, |
| 14746 | OK, |
| 14747 | kServer, |
| 14748 | AUTH_SYNC, |
| 14749 | ERR_INVALID_AUTH_CREDENTIALS, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14750 | 3, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14751 | kNoSSL, |
| 14752 | {TestRound(kGetProxy, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14753 | TestRound(kGetProxy, kServerChallenge, OK), |
| 14754 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14755 | {__LINE__, |
| 14756 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14757 | AUTH_NONE, |
| 14758 | OK, |
| 14759 | kServer, |
| 14760 | AUTH_ASYNC, |
| 14761 | OK, |
| 14762 | 2, |
| 14763 | kNoSSL, |
| 14764 | {TestRound(kGetProxy, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14765 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14766 | {__LINE__, |
| 14767 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14768 | AUTH_NONE, |
| 14769 | OK, |
| 14770 | kServer, |
| 14771 | AUTH_ASYNC, |
| 14772 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14773 | 2, |
| 14774 | kNoSSL, |
| 14775 | {TestRound(kGetProxy, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14776 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14777 | // Non-authenticating HTTP server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14778 | {__LINE__, |
| 14779 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14780 | AUTH_SYNC, |
| 14781 | OK, |
| 14782 | kServer, |
| 14783 | AUTH_NONE, |
| 14784 | OK, |
| 14785 | 2, |
| 14786 | kNoSSL, |
| 14787 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14788 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14789 | {__LINE__, |
| 14790 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14791 | AUTH_SYNC, |
| 14792 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14793 | kServer, |
| 14794 | AUTH_NONE, |
| 14795 | OK, |
| 14796 | 2, |
| 14797 | kNoSSL, |
| 14798 | {TestRound(kGetProxy, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14799 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14800 | {__LINE__, |
| 14801 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14802 | AUTH_ASYNC, |
| 14803 | OK, |
| 14804 | kServer, |
| 14805 | AUTH_NONE, |
| 14806 | OK, |
| 14807 | 2, |
| 14808 | kNoSSL, |
| 14809 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14810 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14811 | {__LINE__, |
| 14812 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14813 | AUTH_ASYNC, |
| 14814 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14815 | kServer, |
| 14816 | AUTH_NONE, |
| 14817 | OK, |
| 14818 | 2, |
| 14819 | kNoSSL, |
| 14820 | {TestRound(kGetProxy, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14821 | TestRound(kGetProxy, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14822 | {__LINE__, |
| 14823 | kProxy, |
| 14824 | AUTH_ASYNC, |
| 14825 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14826 | kServer, |
| 14827 | AUTH_NONE, |
| 14828 | OK, |
| 14829 | 3, |
| 14830 | kNoSSL, |
| 14831 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14832 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14833 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14834 | // Authenticating HTTP server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14835 | {__LINE__, |
| 14836 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14837 | AUTH_SYNC, |
| 14838 | OK, |
| 14839 | kServer, |
| 14840 | AUTH_SYNC, |
| 14841 | OK, |
| 14842 | 3, |
| 14843 | kNoSSL, |
| 14844 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14845 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14846 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14847 | {__LINE__, |
| 14848 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14849 | AUTH_SYNC, |
| 14850 | OK, |
| 14851 | kServer, |
| 14852 | AUTH_SYNC, |
| 14853 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14854 | 3, |
| 14855 | kNoSSL, |
| 14856 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14857 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14858 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14859 | {__LINE__, |
| 14860 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14861 | AUTH_ASYNC, |
| 14862 | OK, |
| 14863 | kServer, |
| 14864 | AUTH_SYNC, |
| 14865 | OK, |
| 14866 | 3, |
| 14867 | kNoSSL, |
| 14868 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14869 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14870 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14871 | {__LINE__, |
| 14872 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14873 | AUTH_ASYNC, |
| 14874 | OK, |
| 14875 | kServer, |
| 14876 | AUTH_SYNC, |
| 14877 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14878 | 3, |
| 14879 | kNoSSL, |
| 14880 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14881 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14882 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14883 | {__LINE__, |
| 14884 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14885 | AUTH_SYNC, |
| 14886 | OK, |
| 14887 | kServer, |
| 14888 | AUTH_ASYNC, |
| 14889 | OK, |
| 14890 | 3, |
| 14891 | kNoSSL, |
| 14892 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14893 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14894 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14895 | {__LINE__, |
| 14896 | kProxy, |
| 14897 | AUTH_SYNC, |
| 14898 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14899 | kServer, |
| 14900 | AUTH_ASYNC, |
| 14901 | OK, |
| 14902 | 4, |
| 14903 | kNoSSL, |
| 14904 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14905 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14906 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14907 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 14908 | {__LINE__, |
| 14909 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14910 | AUTH_SYNC, |
| 14911 | OK, |
| 14912 | kServer, |
| 14913 | AUTH_ASYNC, |
| 14914 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14915 | 3, |
| 14916 | kNoSSL, |
| 14917 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14918 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14919 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14920 | {__LINE__, |
| 14921 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14922 | AUTH_ASYNC, |
| 14923 | OK, |
| 14924 | kServer, |
| 14925 | AUTH_ASYNC, |
| 14926 | OK, |
| 14927 | 3, |
| 14928 | kNoSSL, |
| 14929 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14930 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14931 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14932 | {__LINE__, |
| 14933 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14934 | AUTH_ASYNC, |
| 14935 | OK, |
| 14936 | kServer, |
| 14937 | AUTH_ASYNC, |
| 14938 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14939 | 3, |
| 14940 | kNoSSL, |
| 14941 | {TestRound(kGetProxy, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14942 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14943 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14944 | {__LINE__, |
| 14945 | kProxy, |
| 14946 | AUTH_ASYNC, |
| 14947 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14948 | kServer, |
| 14949 | AUTH_ASYNC, |
| 14950 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14951 | 4, |
| 14952 | kNoSSL, |
| 14953 | {TestRound(kGetProxy, kProxyChallenge, OK), |
| 14954 | TestRound(kGetProxy, kProxyChallenge, OK), |
| 14955 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 14956 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14957 | // Non-authenticating HTTPS server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14958 | {__LINE__, |
| 14959 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14960 | AUTH_NONE, |
| 14961 | OK, |
| 14962 | kSecureServer, |
| 14963 | AUTH_NONE, |
| 14964 | OK, |
| 14965 | 1, |
| 14966 | 0, |
| 14967 | {TestRound(kGet, kSuccess, OK)}}, |
| 14968 | // Authenticating HTTPS server with a direct connection. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14969 | {__LINE__, |
| 14970 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14971 | AUTH_NONE, |
| 14972 | OK, |
| 14973 | kSecureServer, |
| 14974 | AUTH_SYNC, |
| 14975 | OK, |
| 14976 | 2, |
| 14977 | 0, |
| 14978 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 14979 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14980 | {__LINE__, |
| 14981 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14982 | AUTH_NONE, |
| 14983 | OK, |
| 14984 | kSecureServer, |
| 14985 | AUTH_SYNC, |
| 14986 | ERR_INVALID_AUTH_CREDENTIALS, |
| 14987 | 2, |
| 14988 | 0, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 14989 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 14990 | {__LINE__, |
| 14991 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 14992 | AUTH_NONE, |
| 14993 | OK, |
| 14994 | kSecureServer, |
| 14995 | AUTH_ASYNC, |
| 14996 | OK, |
| 14997 | 2, |
| 14998 | 0, |
| 14999 | {TestRound(kGet, kServerChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15000 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15001 | {__LINE__, |
| 15002 | nullptr, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15003 | AUTH_NONE, |
| 15004 | OK, |
| 15005 | kSecureServer, |
| 15006 | AUTH_ASYNC, |
| 15007 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15008 | 2, |
| 15009 | 0, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15010 | {TestRound(kGet, kServerChallenge, OK), TestRound(kGet, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15011 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15012 | {__LINE__, |
| 15013 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15014 | AUTH_NONE, |
| 15015 | OK, |
| 15016 | kSecureServer, |
| 15017 | AUTH_NONE, |
| 15018 | OK, |
| 15019 | 1, |
| 15020 | 0, |
| 15021 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 15022 | // Authenticating HTTPS server through a non-authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15023 | {__LINE__, |
| 15024 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15025 | AUTH_NONE, |
| 15026 | OK, |
| 15027 | kSecureServer, |
| 15028 | AUTH_SYNC, |
| 15029 | OK, |
| 15030 | 2, |
| 15031 | 0, |
| 15032 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15033 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15034 | {__LINE__, |
| 15035 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15036 | AUTH_NONE, |
| 15037 | OK, |
| 15038 | kSecureServer, |
| 15039 | AUTH_SYNC, |
| 15040 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15041 | 2, |
| 15042 | 0, |
| 15043 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15044 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15045 | {__LINE__, |
| 15046 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15047 | AUTH_NONE, |
| 15048 | OK, |
| 15049 | kSecureServer, |
| 15050 | AUTH_ASYNC, |
| 15051 | OK, |
| 15052 | 2, |
| 15053 | 0, |
| 15054 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15055 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15056 | {__LINE__, |
| 15057 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15058 | AUTH_NONE, |
| 15059 | OK, |
| 15060 | kSecureServer, |
| 15061 | AUTH_ASYNC, |
| 15062 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15063 | 2, |
| 15064 | 0, |
| 15065 | {TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15066 | TestRound(kGet, kSuccess, OK)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15067 | // Non-Authenticating HTTPS server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15068 | {__LINE__, |
| 15069 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15070 | AUTH_SYNC, |
| 15071 | OK, |
| 15072 | kSecureServer, |
| 15073 | AUTH_NONE, |
| 15074 | OK, |
| 15075 | 2, |
| 15076 | 1, |
| 15077 | {TestRound(kConnect, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15078 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15079 | {__LINE__, |
| 15080 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15081 | AUTH_SYNC, |
| 15082 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15083 | kSecureServer, |
| 15084 | AUTH_NONE, |
| 15085 | OK, |
| 15086 | 2, |
| 15087 | kNoSSL, |
| 15088 | {TestRound(kConnect, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15089 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15090 | {__LINE__, |
| 15091 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15092 | AUTH_SYNC, |
| 15093 | ERR_UNSUPPORTED_AUTH_SCHEME, |
| 15094 | kSecureServer, |
| 15095 | AUTH_NONE, |
| 15096 | OK, |
| 15097 | 2, |
| 15098 | kNoSSL, |
| 15099 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15100 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15101 | {__LINE__, |
| 15102 | kProxy, |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15103 | AUTH_SYNC, |
| 15104 | ERR_UNEXPECTED, |
| 15105 | kSecureServer, |
| 15106 | AUTH_NONE, |
| 15107 | OK, |
| 15108 | 2, |
| 15109 | kNoSSL, |
| 15110 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15111 | TestRound(kConnect, kProxyConnected, ERR_UNEXPECTED)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15112 | {__LINE__, |
| 15113 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15114 | AUTH_ASYNC, |
| 15115 | OK, |
| 15116 | kSecureServer, |
| 15117 | AUTH_NONE, |
| 15118 | OK, |
| 15119 | 2, |
| 15120 | 1, |
| 15121 | {TestRound(kConnect, kProxyChallenge, OK), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15122 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15123 | {__LINE__, |
| 15124 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15125 | AUTH_ASYNC, |
| 15126 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15127 | kSecureServer, |
| 15128 | AUTH_NONE, |
| 15129 | OK, |
| 15130 | 2, |
| 15131 | kNoSSL, |
| 15132 | {TestRound(kConnect, kProxyChallenge, OK), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15133 | TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15134 | // Authenticating HTTPS server through an authenticating proxy. |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15135 | {__LINE__, |
| 15136 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15137 | AUTH_SYNC, |
| 15138 | OK, |
| 15139 | kSecureServer, |
| 15140 | AUTH_SYNC, |
| 15141 | OK, |
| 15142 | 3, |
| 15143 | 1, |
| 15144 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15145 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15146 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15147 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15148 | {__LINE__, |
| 15149 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15150 | AUTH_SYNC, |
| 15151 | OK, |
| 15152 | kSecureServer, |
| 15153 | AUTH_SYNC, |
| 15154 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15155 | 3, |
| 15156 | 1, |
| 15157 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15158 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15159 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15160 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15161 | {__LINE__, |
| 15162 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15163 | AUTH_ASYNC, |
| 15164 | OK, |
| 15165 | kSecureServer, |
| 15166 | AUTH_SYNC, |
| 15167 | OK, |
| 15168 | 3, |
| 15169 | 1, |
| 15170 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15171 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15172 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15173 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15174 | {__LINE__, |
| 15175 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15176 | AUTH_ASYNC, |
| 15177 | OK, |
| 15178 | kSecureServer, |
| 15179 | AUTH_SYNC, |
| 15180 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15181 | 3, |
| 15182 | 1, |
| 15183 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15184 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15185 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15186 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15187 | {__LINE__, |
| 15188 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15189 | AUTH_SYNC, |
| 15190 | OK, |
| 15191 | kSecureServer, |
| 15192 | AUTH_ASYNC, |
| 15193 | OK, |
| 15194 | 3, |
| 15195 | 1, |
| 15196 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15197 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15198 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15199 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15200 | {__LINE__, |
| 15201 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15202 | AUTH_SYNC, |
| 15203 | OK, |
| 15204 | kSecureServer, |
| 15205 | AUTH_ASYNC, |
| 15206 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15207 | 3, |
| 15208 | 1, |
| 15209 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15210 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15211 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15212 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15213 | {__LINE__, |
| 15214 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15215 | AUTH_ASYNC, |
| 15216 | OK, |
| 15217 | kSecureServer, |
| 15218 | AUTH_ASYNC, |
| 15219 | OK, |
| 15220 | 3, |
| 15221 | 1, |
| 15222 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15223 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15224 | &kServerChallenge), |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15225 | TestRound(kGetAuth, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15226 | {__LINE__, |
| 15227 | kProxy, |
asanka | c9307619 | 2016-10-03 15:46:02 | [diff] [blame] | 15228 | AUTH_ASYNC, |
| 15229 | OK, |
| 15230 | kSecureServer, |
| 15231 | AUTH_ASYNC, |
| 15232 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15233 | 3, |
| 15234 | 1, |
| 15235 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15236 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15237 | &kServerChallenge), |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15238 | TestRound(kGet, kSuccess, OK)}}, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15239 | {__LINE__, |
| 15240 | kProxy, |
| 15241 | AUTH_ASYNC, |
| 15242 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15243 | kSecureServer, |
| 15244 | AUTH_ASYNC, |
| 15245 | ERR_INVALID_AUTH_CREDENTIALS, |
| 15246 | 4, |
| 15247 | 2, |
| 15248 | {TestRound(kConnect, kProxyChallenge, OK), |
| 15249 | TestRound(kConnect, kProxyChallenge, OK), |
| 15250 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, |
| 15251 | &kServerChallenge), |
| 15252 | TestRound(kGet, kSuccess, OK)}}, |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15253 | }; |
| 15254 | |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15255 | for (const auto& test_config : test_configs) { |
| 15256 | SCOPED_TRACE(::testing::Message() << "Test config at " |
| 15257 | << test_config.line_number); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15258 | HttpAuthHandlerMock::Factory* auth_factory( |
| 15259 | new HttpAuthHandlerMock::Factory()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15260 | session_deps_.http_auth_handler_factory.reset(auth_factory); |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 15261 | SSLInfo empty_ssl_info; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 15262 | |
| 15263 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15264 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15265 | for (int n = 0; n < 3; n++) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15266 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 15267 | std::string auth_challenge = "Mock realm=proxy"; |
| 15268 | GURL origin(test_config.proxy_url); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15269 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15270 | auth_challenge.end()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15271 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
Matt Menke | 618134b | 2020-05-22 02:12:27 | [diff] [blame^] | 15272 | empty_ssl_info, NetworkIsolationKey(), |
| 15273 | origin, NetLogWithSource()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15274 | auth_handler->SetGenerateExpectation( |
| 15275 | test_config.proxy_auth_timing == AUTH_ASYNC, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15276 | n == 0 ? test_config.first_generate_proxy_token_rv : OK); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15277 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
| 15278 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15279 | } |
| 15280 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 15281 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15282 | std::string auth_challenge = "Mock realm=server"; |
| 15283 | GURL origin(test_config.server_url); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15284 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15285 | auth_challenge.end()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15286 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
Matt Menke | 618134b | 2020-05-22 02:12:27 | [diff] [blame^] | 15287 | empty_ssl_info, NetworkIsolationKey(), |
| 15288 | origin, NetLogWithSource()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15289 | auth_handler->SetGenerateExpectation( |
| 15290 | test_config.server_auth_timing == AUTH_ASYNC, |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15291 | test_config.first_generate_server_token_rv); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15292 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15293 | |
| 15294 | // The second handler always succeeds. It should only be used where there |
| 15295 | // are multiple auth sessions for server auth in the same network |
| 15296 | // transaction using the same auth scheme. |
| 15297 | std::unique_ptr<HttpAuthHandlerMock> second_handler = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15298 | std::make_unique<HttpAuthHandlerMock>(); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15299 | second_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
Matt Menke | 618134b | 2020-05-22 02:12:27 | [diff] [blame^] | 15300 | empty_ssl_info, NetworkIsolationKey(), |
| 15301 | origin, NetLogWithSource()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15302 | second_handler->SetGenerateExpectation(true, OK); |
| 15303 | auth_factory->AddMockHandler(second_handler.release(), |
| 15304 | HttpAuth::AUTH_SERVER); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15305 | } |
| 15306 | if (test_config.proxy_url) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15307 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15308 | ConfiguredProxyResolutionService::CreateFixed( |
| 15309 | test_config.proxy_url, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15310 | } else { |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 15311 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15312 | ConfiguredProxyResolutionService::CreateDirect(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15313 | } |
| 15314 | |
| 15315 | HttpRequestInfo request; |
| 15316 | request.method = "GET"; |
| 15317 | request.url = GURL(test_config.server_url); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15318 | request.traffic_annotation = |
| 15319 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15320 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15321 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15322 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15323 | SSLSocketDataProvider ssl_socket_data_provider(SYNCHRONOUS, OK); |
| 15324 | |
| 15325 | std::vector<std::vector<MockRead>> mock_reads(1); |
| 15326 | std::vector<std::vector<MockWrite>> mock_writes(1); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15327 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
davidben | 8c7089a | 2017-04-17 20:38:22 | [diff] [blame] | 15328 | SCOPED_TRACE(round); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15329 | const TestRound& read_write_round = test_config.rounds[round]; |
| 15330 | |
| 15331 | // Set up expected reads and writes. |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15332 | mock_reads.back().push_back(read_write_round.read); |
| 15333 | mock_writes.back().push_back(read_write_round.write); |
| 15334 | |
| 15335 | // kProxyChallenge uses Proxy-Connection: close which means that the |
| 15336 | // socket is closed and a new one will be created for the next request. |
mmenke | e71e1533 | 2015-10-07 16:39:54 | [diff] [blame] | 15337 | if (read_write_round.read.data == kProxyChallenge.data) { |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15338 | mock_reads.push_back(std::vector<MockRead>()); |
| 15339 | mock_writes.push_back(std::vector<MockWrite>()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15340 | } |
| 15341 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15342 | if (read_write_round.extra_read) { |
| 15343 | mock_reads.back().push_back(*read_write_round.extra_read); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15344 | } |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15345 | if (read_write_round.extra_write) { |
| 15346 | mock_writes.back().push_back(*read_write_round.extra_write); |
| 15347 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15348 | |
| 15349 | // Add an SSL sequence if necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15350 | if (round >= test_config.first_ssl_round) |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15351 | session_deps_.socket_factory->AddSSLSocketDataProvider( |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15352 | &ssl_socket_data_provider); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15353 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15354 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15355 | std::vector<std::unique_ptr<StaticSocketDataProvider>> data_providers; |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15356 | for (size_t i = 0; i < mock_reads.size(); ++i) { |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15357 | data_providers.push_back(std::make_unique<StaticSocketDataProvider>( |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15358 | mock_reads[i], mock_writes[i])); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15359 | session_deps_.socket_factory->AddSocketDataProvider( |
olli.raula | 525048c | 2015-12-10 07:38:32 | [diff] [blame] | 15360 | data_providers.back().get()); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15361 | } |
| 15362 | |
mmenke | cc2298e | 2015-12-07 18:20:18 | [diff] [blame] | 15363 | // Transaction must be created after DataProviders, so it's destroyed before |
| 15364 | // they are as well. |
| 15365 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 15366 | |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15367 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
davidben | 8c7089a | 2017-04-17 20:38:22 | [diff] [blame] | 15368 | SCOPED_TRACE(round); |
rch | cb68dc6 | 2015-05-21 04:45:36 | [diff] [blame] | 15369 | const TestRound& read_write_round = test_config.rounds[round]; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15370 | // Start or restart the transaction. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15371 | TestCompletionCallback callback; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15372 | int rv; |
| 15373 | if (round == 0) { |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15374 | rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15375 | } else { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15376 | rv = trans.RestartWithAuth( |
| 15377 | AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15378 | } |
| 15379 | if (rv == ERR_IO_PENDING) |
| 15380 | rv = callback.WaitForResult(); |
| 15381 | |
| 15382 | // Compare results with expected data. |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15383 | EXPECT_THAT(rv, IsError(read_write_round.expected_rv)); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15384 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 15385 | if (read_write_round.expected_rv != OK) { |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15386 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 15387 | continue; |
| 15388 | } |
| 15389 | if (round + 1 < test_config.num_auth_rounds) { |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15390 | EXPECT_TRUE(response->auth_challenge.has_value()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15391 | } else { |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15392 | EXPECT_FALSE(response->auth_challenge.has_value()); |
asanka | e2257db | 2016-10-11 22:03:16 | [diff] [blame] | 15393 | EXPECT_FALSE(trans.IsReadyToRestartForAuth()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 15394 | } |
| 15395 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 15396 | } |
| 15397 | } |
| 15398 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15399 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15400 | // Do multi-round authentication and make sure it works correctly. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15401 | HttpAuthHandlerMock::Factory* auth_factory( |
| 15402 | new HttpAuthHandlerMock::Factory()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15403 | session_deps_.http_auth_handler_factory.reset(auth_factory); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 15404 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15405 | ConfiguredProxyResolutionService::CreateDirect(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15406 | session_deps_.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15407 | |
| 15408 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 15409 | auth_handler->set_connection_based(true); |
| 15410 | std::string auth_challenge = "Mock realm=server"; |
| 15411 | GURL origin("http://www.example.com"); |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 15412 | HttpAuthChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 15413 | auth_challenge.end()); |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 15414 | SSLInfo empty_ssl_info; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15415 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
Matt Menke | 618134b | 2020-05-22 02:12:27 | [diff] [blame^] | 15416 | empty_ssl_info, NetworkIsolationKey(), origin, |
| 15417 | NetLogWithSource()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 15418 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15419 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15420 | int rv = OK; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 15421 | const HttpResponseInfo* response = nullptr; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15422 | HttpRequestInfo request; |
| 15423 | request.method = "GET"; |
| 15424 | request.url = origin; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15425 | request.traffic_annotation = |
| 15426 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 15427 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15428 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15429 | |
| 15430 | // Use a TCP Socket Pool with only one connection per group. This is used |
| 15431 | // to validate that the TCP socket is not released to the pool between |
| 15432 | // each round of multi-round authentication. |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 15433 | HttpNetworkSessionPeer session_peer(session.get()); |
Matt Menke | d6fd2a5 | 2019-03-20 06:14:36 | [diff] [blame] | 15434 | CommonConnectJobParams common_connect_job_params( |
| 15435 | session->CreateCommonConnectJobParams()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 15436 | TransportClientSocketPool* transport_pool = new TransportClientSocketPool( |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 15437 | 50, // Max sockets for pool |
| 15438 | 1, // Max sockets per group |
| 15439 | base::TimeDelta::FromSeconds(10), // unused_idle_socket_timeout |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 15440 | ProxyServer::Direct(), false, // is_for_websockets |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 15441 | &common_connect_job_params); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15442 | auto mock_pool_manager = std::make_unique<MockClientSocketPoolManager>(); |
Matt Menke | d23ab95 | 2019-03-06 00:24:40 | [diff] [blame] | 15443 | mock_pool_manager->SetSocketPool(ProxyServer::Direct(), |
| 15444 | base::WrapUnique(transport_pool)); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 15445 | session_peer.SetClientSocketPoolManager(std::move(mock_pool_manager)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15446 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15447 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15448 | TestCompletionCallback callback; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15449 | |
| 15450 | const MockWrite kGet( |
| 15451 | "GET / HTTP/1.1\r\n" |
| 15452 | "Host: www.example.com\r\n" |
| 15453 | "Connection: keep-alive\r\n\r\n"); |
| 15454 | const MockWrite kGetAuth( |
| 15455 | "GET / HTTP/1.1\r\n" |
| 15456 | "Host: www.example.com\r\n" |
| 15457 | "Connection: keep-alive\r\n" |
| 15458 | "Authorization: auth_token\r\n\r\n"); |
| 15459 | |
| 15460 | const MockRead kServerChallenge( |
| 15461 | "HTTP/1.1 401 Unauthorized\r\n" |
| 15462 | "WWW-Authenticate: Mock realm=server\r\n" |
| 15463 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 15464 | "Content-Length: 14\r\n\r\n" |
| 15465 | "Unauthorized\r\n"); |
| 15466 | const MockRead kSuccess( |
| 15467 | "HTTP/1.1 200 OK\r\n" |
| 15468 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 15469 | "Content-Length: 3\r\n\r\n" |
| 15470 | "Yes"); |
| 15471 | |
| 15472 | MockWrite writes[] = { |
| 15473 | // First round |
| 15474 | kGet, |
| 15475 | // Second round |
| 15476 | kGetAuth, |
| 15477 | // Third round |
| 15478 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15479 | // Fourth round |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15480 | kGetAuth, |
| 15481 | // Competing request |
| 15482 | kGet, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15483 | }; |
| 15484 | MockRead reads[] = { |
| 15485 | // First round |
| 15486 | kServerChallenge, |
| 15487 | // Second round |
| 15488 | kServerChallenge, |
| 15489 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15490 | kServerChallenge, |
| 15491 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15492 | kSuccess, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15493 | // Competing response |
| 15494 | kSuccess, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15495 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15496 | StaticSocketDataProvider data_provider(reads, writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15497 | session_deps_.socket_factory->AddSocketDataProvider(&data_provider); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15498 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 15499 | const ClientSocketPool::GroupId kSocketGroup( |
| 15500 | HostPortPair("www.example.com", 80), ClientSocketPool::SocketType::kHttp, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 15501 | PrivacyMode::PRIVACY_MODE_DISABLED, NetworkIsolationKey(), |
| 15502 | false /* disable_secure_dns */); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15503 | |
| 15504 | // First round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15505 | auth_handler->SetGenerateExpectation(false, OK); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15506 | rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15507 | if (rv == ERR_IO_PENDING) |
| 15508 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15509 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15510 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15511 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15512 | EXPECT_TRUE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15513 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15514 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15515 | auth_handler->state()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15516 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15517 | // In between rounds, another request comes in for the same domain. |
| 15518 | // It should not be able to grab the TCP socket that trans has already |
| 15519 | // claimed. |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15520 | HttpNetworkTransaction trans_compete(DEFAULT_PRIORITY, session.get()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15521 | TestCompletionCallback callback_compete; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15522 | rv = trans_compete.Start(&request, callback_compete.callback(), |
| 15523 | NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15524 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15525 | // callback_compete.WaitForResult at this point would stall forever, |
| 15526 | // since the HttpNetworkTransaction does not release the request back to |
| 15527 | // the pool until after authentication completes. |
| 15528 | |
| 15529 | // Second round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15530 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15531 | rv = trans.RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15532 | if (rv == ERR_IO_PENDING) |
| 15533 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15534 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15535 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15536 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15537 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15538 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15539 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15540 | auth_handler->state()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15541 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15542 | // Third round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15543 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15544 | rv = trans.RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15545 | if (rv == ERR_IO_PENDING) |
| 15546 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15547 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15548 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15549 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15550 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15551 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15552 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_GENERATE_AUTH_TOKEN, |
| 15553 | auth_handler->state()); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15554 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15555 | // Fourth round of authentication, which completes successfully. |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15556 | auth_handler->SetGenerateExpectation(false, OK); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15557 | rv = trans.RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 15558 | if (rv == ERR_IO_PENDING) |
| 15559 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15560 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15561 | response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15562 | ASSERT_TRUE(response); |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 15563 | EXPECT_FALSE(response->auth_challenge.has_value()); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15564 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15565 | |
asanka | 463ca426 | 2016-11-16 02:34:31 | [diff] [blame] | 15566 | // In WAIT_FOR_CHALLENGE, although in reality the auth handler is done. A real |
| 15567 | // auth handler should transition to a DONE state in concert with the remote |
| 15568 | // server. But that's not something we can test here with a mock handler. |
| 15569 | EXPECT_EQ(HttpAuthHandlerMock::State::WAIT_FOR_CHALLENGE, |
| 15570 | auth_handler->state()); |
| 15571 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15572 | // Read the body since the fourth round was successful. This will also |
| 15573 | // release the socket back to the pool. |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 15574 | scoped_refptr<IOBufferWithSize> io_buf = |
| 15575 | base::MakeRefCounted<IOBufferWithSize>(50); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15576 | rv = trans.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15577 | if (rv == ERR_IO_PENDING) |
| 15578 | rv = callback.WaitForResult(); |
| 15579 | EXPECT_EQ(3, rv); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15580 | rv = trans.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15581 | EXPECT_EQ(0, rv); |
| 15582 | // There are still 0 idle sockets, since the trans_compete transaction |
| 15583 | // will be handed it immediately after trans releases it to the group. |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15584 | EXPECT_EQ(0u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15585 | |
| 15586 | // The competing request can now finish. Wait for the headers and then |
| 15587 | // read the body. |
| 15588 | rv = callback_compete.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15589 | EXPECT_THAT(rv, IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15590 | rv = trans_compete.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15591 | if (rv == ERR_IO_PENDING) |
| 15592 | rv = callback.WaitForResult(); |
| 15593 | EXPECT_EQ(3, rv); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15594 | rv = trans_compete.Read(io_buf.get(), io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 15595 | EXPECT_EQ(0, rv); |
| 15596 | |
| 15597 | // Finally, the socket is released to the group. |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 15598 | EXPECT_EQ(1u, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 15599 | } |
| 15600 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15601 | // This tests the case that a request is issued via http instead of spdy after |
| 15602 | // npn is negotiated. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15603 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15604 | HttpRequestInfo request; |
| 15605 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15606 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15607 | request.traffic_annotation = |
| 15608 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15609 | |
| 15610 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15611 | MockWrite( |
| 15612 | "GET / HTTP/1.1\r\n" |
| 15613 | "Host: www.example.org\r\n" |
| 15614 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15615 | }; |
| 15616 | |
| 15617 | MockRead data_reads[] = { |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 15618 | MockRead("HTTP/1.1 200 OK\r\n"), |
bnc | 2df4b52 | 2016-07-08 18:17:43 | [diff] [blame] | 15619 | MockRead(kAlternativeServiceHttpHeader), |
bnc | c958faa | 2015-07-31 18:14:52 | [diff] [blame] | 15620 | MockRead("\r\n"), |
| 15621 | MockRead("hello world"), |
| 15622 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15623 | }; |
| 15624 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15625 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 15626 | ssl.next_proto = kProtoHTTP11; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15627 | |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15628 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15629 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15630 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15631 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15632 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15633 | TestCompletionCallback callback; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15634 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15635 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15636 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15637 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15638 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15639 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15640 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 15641 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15642 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15643 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15644 | ASSERT_TRUE(response); |
| 15645 | ASSERT_TRUE(response->headers); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15646 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 15647 | |
| 15648 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15649 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15650 | EXPECT_EQ("hello world", response_data); |
| 15651 | |
| 15652 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 15653 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 15654 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15655 | |
bnc | 55ff9da | 2015-08-19 18:42:35 | [diff] [blame] | 15656 | // Simulate the SSL handshake completing with an NPN negotiation followed by an |
| 15657 | // immediate server closing of the socket. |
| 15658 | // Regression test for https://crbug.com/46369. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15659 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15660 | HttpRequestInfo request; |
| 15661 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15662 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15663 | request.traffic_annotation = |
| 15664 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15665 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15666 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 15667 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15668 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15669 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 15670 | spdy::SpdySerializedFrame req( |
| 15671 | spdy_util_.ConstructSpdyGet(nullptr, 0, 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 15672 | MockWrite spdy_writes[] = {CreateMockWrite(req, 1)}; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15673 | |
| 15674 | MockRead spdy_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15675 | MockRead(SYNCHRONOUS, 0, 0) // Not async - return 0 immediately. |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15676 | }; |
| 15677 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15678 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15679 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15680 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15681 | TestCompletionCallback callback; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15682 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15683 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15684 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15685 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15686 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15687 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 15688 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_CLOSED)); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 15689 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 15690 | |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15691 | // A subclass of HttpAuthHandlerMock that records the request URL when |
| 15692 | // it gets it. This is needed since the auth handler may get destroyed |
| 15693 | // before we get a chance to query it. |
| 15694 | class UrlRecordingHttpAuthHandlerMock : public HttpAuthHandlerMock { |
| 15695 | public: |
| 15696 | explicit UrlRecordingHttpAuthHandlerMock(GURL* url) : url_(url) {} |
| 15697 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 15698 | ~UrlRecordingHttpAuthHandlerMock() override = default; |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15699 | |
| 15700 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 15701 | int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
| 15702 | const HttpRequestInfo* request, |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 15703 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 15704 | std::string* auth_token) override { |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15705 | *url_ = request->url; |
| 15706 | return HttpAuthHandlerMock::GenerateAuthTokenImpl( |
Bence Béky | 7236fb7 | 2018-08-01 14:35:09 | [diff] [blame] | 15707 | credentials, request, std::move(callback), auth_token); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 15708 | } |
| 15709 | |
| 15710 | private: |
| 15711 | GURL* url_; |
| 15712 | }; |
| 15713 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15714 | // Test that if we cancel the transaction as the connection is completing, that |
| 15715 | // everything tears down correctly. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15716 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15717 | // Setup everything about the connection to complete synchronously, so that |
| 15718 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 15719 | // for is the callback from the HttpStreamRequest. |
| 15720 | // Then cancel the transaction. |
| 15721 | // Verify that we don't crash. |
[email protected] | d973e99a | 2012-02-17 21:02:36 | [diff] [blame] | 15722 | MockConnect mock_connect(SYNCHRONOUS, OK); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15723 | MockRead data_reads[] = { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15724 | MockRead(SYNCHRONOUS, "HTTP/1.0 200 OK\r\n\r\n"), |
| 15725 | MockRead(SYNCHRONOUS, "hello world"), |
| 15726 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15727 | }; |
| 15728 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15729 | HttpRequestInfo request; |
| 15730 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15731 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15732 | request.traffic_annotation = |
| 15733 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15734 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15735 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 15736 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 15737 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 15738 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15739 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15740 | data.set_connect_data(mock_connect); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15741 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15742 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15743 | TestCompletionCallback callback; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15744 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 15745 | RecordingBoundTestNetLog log; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15746 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15747 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 15748 | trans.reset(); // Cancel the transaction here. |
| 15749 | |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 15750 | base::RunLoop().RunUntilIdle(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 15751 | } |
| 15752 | |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15753 | // Test that if a transaction is cancelled after receiving the headers, the |
| 15754 | // stream is drained properly and added back to the socket pool. The main |
| 15755 | // purpose of this test is to make sure that an HttpStreamParser can be read |
| 15756 | // from after the HttpNetworkTransaction and the objects it owns have been |
| 15757 | // deleted. |
| 15758 | // See http://crbug.com/368418 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15759 | TEST_F(HttpNetworkTransactionTest, CancelAfterHeaders) { |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15760 | MockRead data_reads[] = { |
| 15761 | MockRead(ASYNC, "HTTP/1.1 200 OK\r\n"), |
| 15762 | MockRead(ASYNC, "Content-Length: 2\r\n"), |
| 15763 | MockRead(ASYNC, "Connection: Keep-Alive\r\n\r\n"), |
| 15764 | MockRead(ASYNC, "1"), |
| 15765 | // 2 async reads are necessary to trigger a ReadResponseBody call after the |
| 15766 | // HttpNetworkTransaction has been deleted. |
| 15767 | MockRead(ASYNC, "2"), |
| 15768 | MockRead(SYNCHRONOUS, ERR_IO_PENDING), // Should never read this. |
| 15769 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15770 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15771 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 15772 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15773 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15774 | |
| 15775 | { |
| 15776 | HttpRequestInfo request; |
| 15777 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15778 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15779 | request.traffic_annotation = |
| 15780 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15781 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 15782 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15783 | TestCompletionCallback callback; |
| 15784 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 15785 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15786 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15787 | callback.WaitForResult(); |
| 15788 | |
| 15789 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15790 | ASSERT_TRUE(response); |
| 15791 | EXPECT_TRUE(response->headers); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15792 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 15793 | |
| 15794 | // The transaction and HttpRequestInfo are deleted. |
| 15795 | } |
| 15796 | |
| 15797 | // Let the HttpResponseBodyDrainer drain the socket. |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 15798 | base::RunLoop().RunUntilIdle(); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15799 | |
| 15800 | // Socket should now be idle, waiting to be reused. |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 15801 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | ecab6e05 | 2014-05-16 14:58:12 | [diff] [blame] | 15802 | } |
| 15803 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15804 | // Test a basic GET request through a proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15805 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15806 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15807 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15808 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 15809 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15810 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15811 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15812 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15813 | HttpRequestInfo request; |
| 15814 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15815 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15816 | request.traffic_annotation = |
| 15817 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15818 | |
| 15819 | MockWrite data_writes1[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15820 | MockWrite( |
| 15821 | "GET http://www.example.org/ HTTP/1.1\r\n" |
| 15822 | "Host: www.example.org\r\n" |
| 15823 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15824 | }; |
| 15825 | |
| 15826 | MockRead data_reads1[] = { |
| 15827 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15828 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15829 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15830 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15831 | }; |
| 15832 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15833 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15834 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15835 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15836 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15837 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15838 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15839 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15840 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15841 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15842 | |
| 15843 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15844 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15845 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15846 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15847 | ASSERT_TRUE(response); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15848 | |
| 15849 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 15850 | EXPECT_EQ(200, response->headers->response_code()); |
| 15851 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 15852 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 15853 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 15854 | HostPortPair::FromString("myproxy:70")), |
| 15855 | response->proxy_server); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15856 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15857 | |
| 15858 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15859 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15860 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 15861 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15862 | } |
| 15863 | |
| 15864 | // Test a basic HTTPS GET request through a proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15865 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15866 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15867 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15868 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 15869 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15870 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15871 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15872 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15873 | HttpRequestInfo request; |
| 15874 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 15875 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15876 | request.traffic_annotation = |
| 15877 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15878 | |
| 15879 | // Since we have proxy, should try to establish tunnel. |
| 15880 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15881 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 15882 | "Host: www.example.org:443\r\n" |
| 15883 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15884 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15885 | MockWrite("GET / HTTP/1.1\r\n" |
| 15886 | "Host: www.example.org\r\n" |
| 15887 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15888 | }; |
| 15889 | |
| 15890 | MockRead data_reads1[] = { |
| 15891 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 15892 | |
| 15893 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15894 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15895 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15896 | MockRead(SYNCHRONOUS, OK), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15897 | }; |
| 15898 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15899 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15900 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 15901 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 15902 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15903 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 15904 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15905 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15906 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 15907 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15908 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15909 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15910 | |
| 15911 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15912 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 15913 | auto entries = log.GetEntries(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15914 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15915 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 15916 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15917 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 15918 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15919 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 15920 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15921 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15922 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 15923 | ASSERT_TRUE(response); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15924 | |
| 15925 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 15926 | EXPECT_EQ(200, response->headers->response_code()); |
| 15927 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 15928 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 15929 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 15930 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 15931 | HostPortPair::FromString("myproxy:70")), |
| 15932 | response->proxy_server); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15933 | |
| 15934 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15935 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
[email protected] | 029c83b6 | 2013-01-24 05:28:20 | [diff] [blame] | 15936 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 15937 | CONNECT_TIMING_HAS_SSL_TIMES); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 15938 | } |
| 15939 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15940 | // Test a basic HTTPS GET request through a proxy, connecting to an IPv6 |
| 15941 | // literal host. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 15942 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetIPv6) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 15943 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 15944 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 15945 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 15946 | RecordingBoundTestNetLog log; |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15947 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 15948 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15949 | |
| 15950 | HttpRequestInfo request; |
| 15951 | request.method = "GET"; |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15952 | request.url = GURL("https://[::2]:443/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 15953 | request.traffic_annotation = |
| 15954 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15955 | |
| 15956 | // Since we have proxy, should try to establish tunnel. |
| 15957 | MockWrite data_writes1[] = { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15958 | MockWrite("CONNECT [::2]:443 HTTP/1.1\r\n" |
| 15959 | "Host: [::2]:443\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15960 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 15961 | |
| 15962 | MockWrite("GET / HTTP/1.1\r\n" |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 15963 | "Host: [::2]\r\n" |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15964 | "Connection: keep-alive\r\n\r\n"), |
| 15965 | }; |
| 15966 | |
| 15967 | MockRead data_reads1[] = { |
| 15968 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 15969 | |
| 15970 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 15971 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 15972 | MockRead("Content-Length: 100\r\n\r\n"), |
| 15973 | MockRead(SYNCHRONOUS, OK), |
| 15974 | }; |
| 15975 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 15976 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15977 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 15978 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 15979 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 15980 | |
| 15981 | TestCompletionCallback callback1; |
| 15982 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15983 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15984 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15985 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15986 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15987 | |
| 15988 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 15989 | EXPECT_THAT(rv, IsOk()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 15990 | auto entries = log.GetEntries(); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15991 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15992 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 15993 | NetLogEventPhase::NONE); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15994 | ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 15995 | entries, pos, |
| 15996 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 15997 | NetLogEventPhase::NONE); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 15998 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 15999 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16000 | ASSERT_TRUE(response); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 16001 | |
| 16002 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 16003 | EXPECT_EQ(200, response->headers->response_code()); |
| 16004 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 16005 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 16006 | EXPECT_TRUE(response->was_fetched_via_proxy); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 16007 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 16008 | HostPortPair::FromString("myproxy:70")), |
| 16009 | response->proxy_server); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 16010 | |
| 16011 | LoadTimingInfo load_timing_info; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16012 | EXPECT_TRUE(trans.GetLoadTimingInfo(&load_timing_info)); |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 16013 | TestLoadTimingNotReusedWithPac(load_timing_info, |
| 16014 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 16015 | } |
| 16016 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16017 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 16018 | // while establishing the tunnel. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16019 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 16020 | session_deps_.proxy_resolution_service = |
| 16021 | ConfiguredProxyResolutionService::CreateFixed( |
| 16022 | "myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 16023 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16024 | session_deps_.net_log = log.bound().net_log(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16025 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16026 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16027 | HttpRequestInfo request; |
| 16028 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16029 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16030 | request.traffic_annotation = |
| 16031 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16032 | |
| 16033 | // Since we have proxy, should try to establish tunnel. |
| 16034 | MockWrite data_writes1[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 16035 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 16036 | "Host: www.example.org:443\r\n" |
| 16037 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16038 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 16039 | MockWrite("GET / HTTP/1.1\r\n" |
| 16040 | "Host: www.example.org\r\n" |
| 16041 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16042 | }; |
| 16043 | |
| 16044 | MockRead data_reads1[] = { |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16045 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16046 | MockRead(ASYNC, 0, 0), // EOF |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16047 | }; |
| 16048 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16049 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16050 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16051 | SSLSocketDataProvider ssl(ASYNC, OK); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16052 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16053 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16054 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16055 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16056 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 16057 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16058 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16059 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16060 | |
| 16061 | rv = callback1.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16062 | EXPECT_THAT(rv, IsError(ERR_EMPTY_RESPONSE)); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 16063 | auto entries = log.GetEntries(); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16064 | size_t pos = ExpectLogContainsSomewhere( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 16065 | entries, 0, NetLogEventType::HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 16066 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16067 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 16068 | entries, pos, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 16069 | NetLogEventType::HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 16070 | NetLogEventPhase::NONE); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 16071 | } |
| 16072 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16073 | // Test for crbug.com/55424. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16074 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16075 | spdy::SpdySerializedFrame req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16076 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16077 | MockWrite spdy_writes[] = {CreateMockWrite(req, 0)}; |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16078 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16079 | spdy::SpdySerializedFrame resp( |
| 16080 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16081 | spdy::SpdySerializedFrame data(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16082 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16083 | CreateMockRead(resp, 1), CreateMockRead(data, 2), MockRead(ASYNC, 0, 3), |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16084 | }; |
| 16085 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16086 | SequencedSocketData spdy_data(spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16087 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16088 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16089 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 16090 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16091 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16092 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16093 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16094 | |
| 16095 | // Set up an initial SpdySession in the pool to reuse. |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16096 | HostPortPair host_port_pair("www.example.org", 443); |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 16097 | SpdySessionKey key(host_port_pair, ProxyServer::Direct(), |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 16098 | PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 16099 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 16100 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 795cbf8 | 2013-07-22 09:37:27 | [diff] [blame] | 16101 | base::WeakPtr<SpdySession> spdy_session = |
Bence Béky | 0ef1556e | 2017-06-30 19:52:52 | [diff] [blame] | 16102 | CreateSpdySession(session.get(), key, NetLogWithSource()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16103 | |
| 16104 | HttpRequestInfo request; |
| 16105 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16106 | request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16107 | request.traffic_annotation = |
| 16108 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16109 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16110 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16111 | |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 16112 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16113 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16114 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16115 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 16116 | } |
| 16117 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16118 | // Given a net error, cause that error to be returned from the first Write() |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16119 | // call and verify that the HttpNetworkTransaction fails with that error. |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 16120 | void HttpNetworkTransactionTest::CheckErrorIsPassedBack( |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16121 | int error, IoMode mode) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16122 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16123 | request_info.url = GURL("https://www.example.com/"); |
| 16124 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16125 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16126 | request_info.traffic_annotation = |
| 16127 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16128 | |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16129 | SSLSocketDataProvider ssl_data(mode, OK); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16130 | MockWrite data_writes[] = { |
| 16131 | MockWrite(mode, error), |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16132 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16133 | StaticSocketDataProvider data(base::span<MockRead>(), data_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16134 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 16135 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16136 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16137 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16138 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16139 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16140 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16141 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16142 | if (rv == ERR_IO_PENDING) |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16143 | rv = callback.WaitForResult(); |
| 16144 | ASSERT_EQ(error, rv); |
| 16145 | } |
| 16146 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16147 | TEST_F(HttpNetworkTransactionTest, SSLWriteCertError) { |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16148 | // Just check a grab bag of cert errors. |
| 16149 | static const int kErrors[] = { |
| 16150 | ERR_CERT_COMMON_NAME_INVALID, |
| 16151 | ERR_CERT_AUTHORITY_INVALID, |
| 16152 | ERR_CERT_DATE_INVALID, |
| 16153 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 16154 | for (size_t i = 0; i < base::size(kErrors); i++) { |
[email protected] | 8ddf832 | 2012-02-23 18:08:06 | [diff] [blame] | 16155 | CheckErrorIsPassedBack(kErrors[i], ASYNC); |
| 16156 | CheckErrorIsPassedBack(kErrors[i], SYNCHRONOUS); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 16157 | } |
| 16158 | } |
| 16159 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16160 | // Ensure that a client certificate is removed from the SSL client auth |
| 16161 | // cache when: |
| 16162 | // 1) No proxy is involved. |
| 16163 | // 2) TLS False Start is disabled. |
| 16164 | // 3) The initial TLS handshake requests a client certificate. |
| 16165 | // 4) The client supplies an invalid/unacceptable certificate. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16166 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_NoFalseStart) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16167 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16168 | request_info.url = GURL("https://www.example.com/"); |
| 16169 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16170 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16171 | request_info.traffic_annotation = |
| 16172 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16173 | |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 16174 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16175 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16176 | |
| 16177 | // [ssl_]data1 contains the data for the first SSL handshake. When a |
| 16178 | // CertificateRequest is received for the first time, the handshake will |
| 16179 | // be aborted to allow the caller to provide a certificate. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16180 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16181 | ssl_data1.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16182 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16183 | StaticSocketDataProvider data1; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16184 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16185 | |
| 16186 | // [ssl_]data2 contains the data for the second SSL handshake. When TLS |
| 16187 | // False Start is not being used, the result of the SSL handshake will be |
| 16188 | // returned as part of the SSLClientSocket::Connect() call. This test |
| 16189 | // matches the result of a server sending a handshake_failure alert, |
| 16190 | // rather than a Finished message, because it requires a client |
| 16191 | // certificate and none was supplied. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16192 | SSLSocketDataProvider ssl_data2(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16193 | ssl_data2.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16194 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16195 | StaticSocketDataProvider data2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16196 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16197 | |
| 16198 | // [ssl_]data3 contains the data for the third SSL handshake. When a |
| 16199 | // connection to a server fails during an SSL handshake, |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 16200 | // HttpNetworkTransaction will attempt to fallback with legacy cryptography |
| 16201 | // enabled on some errors. This is transparent to the caller |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16202 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 16203 | // requiring a client certificate, this fallback handshake should also |
| 16204 | // fail. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16205 | SSLSocketDataProvider ssl_data3(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 16206 | ssl_data3.expected_disable_legacy_crypto = false; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16207 | ssl_data3.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16208 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16209 | StaticSocketDataProvider data3; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16210 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16211 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16212 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16213 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16214 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16215 | // Begin the SSL handshake with the peer. This consumes ssl_data1. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16216 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16217 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16218 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16219 | |
| 16220 | // Complete the SSL handshake, which should abort due to requiring a |
| 16221 | // client certificate. |
| 16222 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16223 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16224 | |
| 16225 | // Indicate that no certificate should be supplied. From the perspective |
| 16226 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 16227 | // certificate, so this is the same as supply a |
| 16228 | // legitimate-but-unacceptable certificate. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16229 | rv = trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16230 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16231 | |
| 16232 | // Ensure the certificate was added to the client auth cache before |
| 16233 | // allowing the connection to continue restarting. |
| 16234 | scoped_refptr<X509Certificate> client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16235 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16236 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16237 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16238 | ASSERT_FALSE(client_cert); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16239 | |
| 16240 | // Restart the handshake. This will consume ssl_data2, which fails, and |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16241 | // then consume ssl_data3 and ssl_data4, both of which should also fail. |
| 16242 | // The result code is checked against what ssl_data4 should return. |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16243 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16244 | ASSERT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16245 | |
| 16246 | // Ensure that the client certificate is removed from the cache on a |
| 16247 | // handshake failure. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16248 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16249 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16250 | } |
| 16251 | |
| 16252 | // Ensure that a client certificate is removed from the SSL client auth |
| 16253 | // cache when: |
| 16254 | // 1) No proxy is involved. |
| 16255 | // 2) TLS False Start is enabled. |
| 16256 | // 3) The initial TLS handshake requests a client certificate. |
| 16257 | // 4) The client supplies an invalid/unacceptable certificate. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16258 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_FalseStart) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16259 | HttpRequestInfo request_info; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16260 | request_info.url = GURL("https://www.example.com/"); |
| 16261 | request_info.method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16262 | request_info.load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16263 | request_info.traffic_annotation = |
| 16264 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 16265 | |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 16266 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16267 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16268 | |
| 16269 | // When TLS False Start is used, SSLClientSocket::Connect() calls will |
| 16270 | // return successfully after reading up to the peer's Certificate message. |
| 16271 | // This is to allow the caller to call SSLClientSocket::Write(), which can |
| 16272 | // enqueue application data to be sent in the same packet as the |
| 16273 | // ChangeCipherSpec and Finished messages. |
| 16274 | // The actual handshake will be finished when SSLClientSocket::Read() is |
| 16275 | // called, which expects to process the peer's ChangeCipherSpec and |
| 16276 | // Finished messages. If there was an error negotiating with the peer, |
| 16277 | // such as due to the peer requiring a client certificate when none was |
| 16278 | // supplied, the alert sent by the peer won't be processed until Read() is |
| 16279 | // called. |
| 16280 | |
| 16281 | // Like the non-False Start case, when a client certificate is requested by |
| 16282 | // the peer, the handshake is aborted during the Connect() call. |
| 16283 | // [ssl_]data1 represents the initial SSL handshake with the peer. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16284 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16285 | ssl_data1.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16286 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16287 | StaticSocketDataProvider data1; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16288 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16289 | |
| 16290 | // When a client certificate is supplied, Connect() will not be aborted |
| 16291 | // when the peer requests the certificate. Instead, the handshake will |
| 16292 | // artificially succeed, allowing the caller to write the HTTP request to |
| 16293 | // the socket. The handshake messages are not processed until Read() is |
| 16294 | // called, which then detects that the handshake was aborted, due to the |
| 16295 | // peer sending a handshake_failure because it requires a client |
| 16296 | // certificate. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16297 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16298 | ssl_data2.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16299 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16300 | MockRead data2_reads[] = { |
| 16301 | MockRead(ASYNC /* async */, ERR_SSL_PROTOCOL_ERROR), |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16302 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16303 | StaticSocketDataProvider data2(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16304 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16305 | |
| 16306 | // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16307 | // the data for the SSL handshake once the TLSv1.1 connection falls back to |
| 16308 | // TLSv1. It has the same behaviour as [ssl_]data2. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16309 | SSLSocketDataProvider ssl_data3(ASYNC, OK); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16310 | ssl_data3.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16311 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16312 | StaticSocketDataProvider data3(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16313 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16314 | |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16315 | // [ssl_]data4 is the data for the SSL handshake once the TLSv1 connection |
| 16316 | // falls back to SSLv3. It has the same behaviour as [ssl_]data2. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16317 | SSLSocketDataProvider ssl_data4(ASYNC, OK); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16318 | ssl_data4.cert_request_info = cert_request.get(); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16319 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data4); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16320 | StaticSocketDataProvider data4(data2_reads, base::span<MockWrite>()); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16321 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16322 | |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16323 | // Need one more if TLSv1.2 is enabled. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16324 | SSLSocketDataProvider ssl_data5(ASYNC, OK); |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16325 | ssl_data5.cert_request_info = cert_request.get(); |
| 16326 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data5); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16327 | StaticSocketDataProvider data5(data2_reads, base::span<MockWrite>()); |
[email protected] | 7799de1 | 2013-05-30 05:52:51 | [diff] [blame] | 16328 | session_deps_.socket_factory->AddSocketDataProvider(&data5); |
| 16329 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16330 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 16331 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16332 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16333 | // Begin the initial SSL handshake. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 16334 | TestCompletionCallback callback; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16335 | int rv = trans.Start(&request_info, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16336 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16337 | |
| 16338 | // Complete the SSL handshake, which should abort due to requiring a |
| 16339 | // client certificate. |
| 16340 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16341 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16342 | |
| 16343 | // Indicate that no certificate should be supplied. From the perspective |
| 16344 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 16345 | // certificate, so this is the same as supply a |
| 16346 | // legitimate-but-unacceptable certificate. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16347 | rv = trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16348 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16349 | |
| 16350 | // Ensure the certificate was added to the client auth cache before |
| 16351 | // allowing the connection to continue restarting. |
| 16352 | scoped_refptr<X509Certificate> client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16353 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16354 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16355 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16356 | ASSERT_FALSE(client_cert); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16357 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16358 | // Restart the handshake. This will consume ssl_data2, which fails, and |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 16359 | // then consume ssl_data3 and ssl_data4, both of which should also fail. |
| 16360 | // The result code is checked against what ssl_data4 should return. |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16361 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16362 | ASSERT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16363 | |
| 16364 | // Ensure that the client certificate is removed from the cache on a |
| 16365 | // handshake failure. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16366 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 16367 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 16368 | } |
| 16369 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16370 | // Ensure that a client certificate is removed from the SSL client auth |
| 16371 | // cache when: |
| 16372 | // 1) An HTTPS proxy is involved. |
| 16373 | // 3) The HTTPS proxy requests a client certificate. |
| 16374 | // 4) The client supplies an invalid/unacceptable certificate for the |
| 16375 | // proxy. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16376 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Proxy_Fail) { |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 16377 | session_deps_.proxy_resolution_service = |
| 16378 | ConfiguredProxyResolutionService::CreateFixed( |
| 16379 | "https://proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 16380 | RecordingBoundTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16381 | session_deps_.net_log = log.bound().net_log(); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16382 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16383 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 16384 | cert_request->host_and_port = HostPortPair("proxy", 70); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16385 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16386 | // Repeat the test for connecting to an HTTPS endpoint, then for connecting to |
| 16387 | // an HTTP endpoint. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16388 | HttpRequestInfo requests[2]; |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16389 | requests[0].url = GURL("https://www.example.com/"); |
| 16390 | requests[0].method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16391 | requests[0].load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16392 | requests[0].traffic_annotation = |
| 16393 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16394 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16395 | // HTTPS requests are tunneled. |
| 16396 | MockWrite https_writes[] = { |
| 16397 | MockWrite("CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 16398 | "Host: www.example.com:443\r\n" |
| 16399 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 16400 | }; |
| 16401 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16402 | requests[1].url = GURL("http://www.example.com/"); |
| 16403 | requests[1].method = "GET"; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 16404 | requests[1].load_flags = LOAD_NORMAL; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16405 | requests[1].traffic_annotation = |
| 16406 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16407 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16408 | // HTTP requests are not. |
| 16409 | MockWrite http_writes[] = { |
| 16410 | MockWrite("GET http://www.example.com/ HTTP/1.1\r\n" |
| 16411 | "Host: www.example.com\r\n" |
| 16412 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 16413 | }; |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16414 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16415 | // When the server rejects the client certificate, it will close the |
| 16416 | // connection. In TLS 1.2, this is signaled out of Connect(). In TLS 1.3 (or |
| 16417 | // TLS 1.2 with False Start), the error is returned out of the first Read(). |
| 16418 | for (bool reject_in_connect : {true, false}) { |
| 16419 | SCOPED_TRACE(reject_in_connect); |
| 16420 | // Client certificate errors are typically signaled with |
| 16421 | // ERR_BAD_SSL_CLIENT_AUTH_CERT, but sometimes the server gives an arbitrary |
| 16422 | // protocol error. |
| 16423 | for (Error reject_error : |
| 16424 | {ERR_SSL_PROTOCOL_ERROR, ERR_BAD_SSL_CLIENT_AUTH_CERT}) { |
| 16425 | SCOPED_TRACE(reject_error); |
| 16426 | // Tunneled and non-tunneled requests are handled differently. Test both. |
| 16427 | for (const HttpRequestInfo& request : requests) { |
| 16428 | SCOPED_TRACE(request.url); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16429 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16430 | session_deps_.socket_factory = |
| 16431 | std::make_unique<MockClientSocketFactory>(); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16432 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16433 | // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 16434 | // [ssl_]data[1-3]. |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16435 | SSLSocketDataProvider ssl_data1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 16436 | ssl_data1.cert_request_info = cert_request.get(); |
| 16437 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 16438 | StaticSocketDataProvider data1; |
| 16439 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16440 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16441 | base::Optional<SSLSocketDataProvider> ssl_data2; |
| 16442 | base::Optional<StaticSocketDataProvider> data2; |
| 16443 | MockRead error_in_read[] = {MockRead(ASYNC, reject_error)}; |
| 16444 | if (reject_in_connect) { |
| 16445 | ssl_data2.emplace(ASYNC, reject_error); |
| 16446 | // There are no reads or writes. |
| 16447 | data2.emplace(); |
| 16448 | } else { |
| 16449 | ssl_data2.emplace(ASYNC, OK); |
| 16450 | // We will get one Write() in before observing the error in Read(). |
| 16451 | if (request.url.SchemeIsCryptographic()) { |
| 16452 | data2.emplace(error_in_read, https_writes); |
| 16453 | } else { |
| 16454 | data2.emplace(error_in_read, http_writes); |
| 16455 | } |
| 16456 | } |
| 16457 | ssl_data2->cert_request_info = cert_request.get(); |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16458 | session_deps_.socket_factory->AddSSLSocketDataProvider( |
| 16459 | &ssl_data2.value()); |
| 16460 | session_deps_.socket_factory->AddSocketDataProvider(&data2.value()); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16461 | |
David Benjamin | 07a07d65 | 2020-02-26 22:26:59 | [diff] [blame] | 16462 | // If the handshake returns ERR_SSL_PROTOCOL_ERROR, we attempt to |
| 16463 | // connect twice. |
| 16464 | base::Optional<SSLSocketDataProvider> ssl_data3; |
| 16465 | base::Optional<StaticSocketDataProvider> data3; |
| 16466 | if (reject_in_connect && reject_error == ERR_SSL_PROTOCOL_ERROR) { |
| 16467 | ssl_data3.emplace(ASYNC, reject_error); |
| 16468 | data3.emplace(); // There are no reads or writes. |
| 16469 | ssl_data3->cert_request_info = cert_request.get(); |
| 16470 | session_deps_.socket_factory->AddSSLSocketDataProvider( |
| 16471 | &ssl_data3.value()); |
| 16472 | session_deps_.socket_factory->AddSocketDataProvider(&data3.value()); |
| 16473 | } |
| 16474 | |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16475 | std::unique_ptr<HttpNetworkSession> session = |
| 16476 | CreateSession(&session_deps_); |
| 16477 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 16478 | |
| 16479 | // Begin the SSL handshake with the proxy. |
| 16480 | TestCompletionCallback callback; |
| 16481 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 16482 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16483 | |
| 16484 | // Complete the SSL handshake, which should abort due to requiring a |
| 16485 | // client certificate. |
| 16486 | rv = callback.WaitForResult(); |
| 16487 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 16488 | |
| 16489 | // Indicate that no certificate should be supplied. From the |
| 16490 | // perspective of SSLClientCertCache, NULL is just as meaningful as a |
| 16491 | // real certificate, so this is the same as supply a |
| 16492 | // legitimate-but-unacceptable certificate. |
| 16493 | rv = |
| 16494 | trans.RestartWithCertificate(nullptr, nullptr, callback.callback()); |
| 16495 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16496 | |
| 16497 | // Ensure the certificate was added to the client auth cache before |
| 16498 | // allowing the connection to continue restarting. |
| 16499 | scoped_refptr<X509Certificate> client_cert; |
| 16500 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16501 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16502 | HostPortPair("proxy", 70), &client_cert, &client_private_key)); |
| 16503 | ASSERT_FALSE(client_cert); |
| 16504 | // Ensure the certificate was NOT cached for the endpoint. This only |
| 16505 | // applies to HTTPS requests, but is fine to check for HTTP requests. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16506 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16507 | HostPortPair("www.example.com", 443), &client_cert, |
| 16508 | &client_private_key)); |
| 16509 | |
| 16510 | // Restart the handshake. This will consume ssl_data2. The result code |
| 16511 | // is checked against what ssl_data2 should return. |
| 16512 | rv = callback.WaitForResult(); |
| 16513 | ASSERT_THAT(rv, AnyOf(IsError(ERR_PROXY_CONNECTION_FAILED), |
| 16514 | IsError(reject_error))); |
| 16515 | |
| 16516 | // Now that the new handshake has failed, ensure that the client |
| 16517 | // certificate was removed from the client auth cache. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16518 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16519 | HostPortPair("proxy", 70), &client_cert, &client_private_key)); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16520 | ASSERT_FALSE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 16521 | HostPortPair("www.example.com", 443), &client_cert, |
| 16522 | &client_private_key)); |
| 16523 | } |
| 16524 | } |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 16525 | } |
| 16526 | } |
| 16527 | |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16528 | // Test that HttpNetworkTransaction correctly handles (mocked) certificate |
| 16529 | // requests during a TLS renegotiation. |
| 16530 | TEST_F(HttpNetworkTransactionTest, CertificateRequestInRenego) { |
| 16531 | HttpRequestInfo request_info; |
| 16532 | request_info.url = GURL("https://www.example.com/"); |
| 16533 | request_info.method = "GET"; |
| 16534 | request_info.load_flags = LOAD_NORMAL; |
| 16535 | request_info.traffic_annotation = |
| 16536 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 16537 | |
| 16538 | auto cert_request = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 16539 | cert_request->host_and_port = HostPortPair("www.example.com", 443); |
| 16540 | |
| 16541 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 16542 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 16543 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 16544 | ASSERT_TRUE(identity); |
| 16545 | |
| 16546 | // The first connection's handshake succeeds, but we get |
| 16547 | // ERR_SSL_CLIENT_AUTH_CERT_NEEDED instead of an HTTP response. |
| 16548 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 16549 | ssl_data1.cert_request_info = cert_request.get(); |
| 16550 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 16551 | MockWrite data1_writes[] = { |
| 16552 | MockWrite("GET / HTTP/1.1\r\n" |
| 16553 | "Host: www.example.com\r\n" |
| 16554 | "Connection: keep-alive\r\n\r\n"), |
| 16555 | }; |
| 16556 | MockRead data1_reads[] = { |
| 16557 | MockRead(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED), |
| 16558 | }; |
| 16559 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
| 16560 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16561 | |
| 16562 | // After supplying with certificate, we restart the request from the top, |
| 16563 | // which succeeds this time. |
| 16564 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 16565 | ssl_data2.expected_send_client_cert = true; |
| 16566 | ssl_data2.expected_client_cert = identity->certificate(); |
| 16567 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 16568 | MockWrite data2_writes[] = { |
| 16569 | MockWrite("GET / HTTP/1.1\r\n" |
| 16570 | "Host: www.example.com\r\n" |
| 16571 | "Connection: keep-alive\r\n\r\n"), |
| 16572 | }; |
| 16573 | MockRead data2_reads[] = { |
| 16574 | MockRead("HTTP/1.1 200 OK\r\n" |
| 16575 | "Content-Length: 0\r\n\r\n"), |
| 16576 | }; |
| 16577 | StaticSocketDataProvider data2(data2_reads, data2_writes); |
| 16578 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16579 | |
| 16580 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 16581 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 16582 | |
| 16583 | TestCompletionCallback callback; |
| 16584 | int rv = callback.GetResult( |
| 16585 | trans.Start(&request_info, callback.callback(), NetLogWithSource())); |
| 16586 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 16587 | |
| 16588 | rv = trans.RestartWithCertificate(identity->certificate(), |
| 16589 | identity->ssl_private_key(), |
| 16590 | callback.callback()); |
| 16591 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16592 | |
| 16593 | // Ensure the certificate was added to the client auth cache |
| 16594 | // allowing the connection to continue restarting. |
| 16595 | scoped_refptr<X509Certificate> client_cert; |
| 16596 | scoped_refptr<SSLPrivateKey> client_private_key; |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16597 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16598 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
| 16599 | EXPECT_TRUE(client_cert->EqualsIncludingChain(identity->certificate())); |
| 16600 | |
| 16601 | // Complete the handshake. The request now succeeds. |
| 16602 | rv = callback.WaitForResult(); |
| 16603 | ASSERT_THAT(rv, IsError(OK)); |
| 16604 | EXPECT_EQ(200, trans.GetResponseInfo()->headers->response_code()); |
| 16605 | |
| 16606 | // The client certificate remains in the cache. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 16607 | ASSERT_TRUE(session->ssl_client_context()->GetClientCertificate( |
David Benjamin | 1a056608 | 2019-04-30 07:36:19 | [diff] [blame] | 16608 | HostPortPair("www.example.com", 443), &client_cert, &client_private_key)); |
| 16609 | EXPECT_TRUE(client_cert->EqualsIncludingChain(identity->certificate())); |
| 16610 | } |
| 16611 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16612 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPooling) { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16613 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16614 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16615 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16616 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 16617 | AddSSLSocketData(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16618 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16619 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16620 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 16621 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16622 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16623 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16624 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16625 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16626 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16627 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16628 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16629 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16630 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16631 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16632 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16633 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16634 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16635 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16636 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 16637 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 16638 | MockRead(ASYNC, 0, 6), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16639 | }; |
| 16640 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 16641 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16642 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16643 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16644 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16645 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 16646 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16647 | HttpRequestInfo request1; |
| 16648 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16649 | request1.url = GURL("https://www.example.org/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16650 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16651 | request1.traffic_annotation = |
| 16652 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16653 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16654 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16655 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16656 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16657 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16658 | |
| 16659 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16660 | ASSERT_TRUE(response); |
| 16661 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16662 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16663 | |
| 16664 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16665 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16666 | EXPECT_EQ("hello!", response_data); |
| 16667 | |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16668 | // Preload mail.example.com into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16669 | rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 16670 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16671 | base::nullopt); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16672 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16673 | |
| 16674 | HttpRequestInfo request2; |
| 16675 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16676 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16677 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16678 | request2.traffic_annotation = |
| 16679 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16680 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16681 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16682 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16683 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16684 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16685 | |
| 16686 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16687 | ASSERT_TRUE(response); |
| 16688 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16689 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16690 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 16691 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16692 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16693 | EXPECT_EQ("hello!", response_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 16694 | } |
| 16695 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 16696 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPoolingAfterResolution) { |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16697 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16698 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 16699 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16700 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 16701 | AddSSLSocketData(); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16702 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16703 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 16704 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 16705 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16706 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16707 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16708 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16709 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16710 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16711 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16712 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16713 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16714 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16715 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 16716 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16717 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16718 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16719 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 16720 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 16721 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 16722 | MockRead(ASYNC, 0, 6), |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16723 | }; |
| 16724 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 16725 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16726 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16727 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 16728 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16729 | |
| 16730 | TestCompletionCallback callback; |
| 16731 | HttpRequestInfo request1; |
| 16732 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 16733 | request1.url = GURL("https://www.example.org/"); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16734 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16735 | request1.traffic_annotation = |
| 16736 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16737 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16738 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16739 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16740 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16741 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16742 | |
| 16743 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16744 | ASSERT_TRUE(response); |
| 16745 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16746 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16747 | |
| 16748 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16749 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16750 | EXPECT_EQ("hello!", response_data); |
| 16751 | |
| 16752 | HttpRequestInfo request2; |
| 16753 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 16754 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16755 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16756 | request2.traffic_annotation = |
| 16757 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 16758 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16759 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 16760 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16761 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16762 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16763 | |
| 16764 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 16765 | ASSERT_TRUE(response); |
| 16766 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 16767 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16768 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 16769 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 16770 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16771 | EXPECT_EQ("hello!", response_data); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 16772 | } |
| 16773 | |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16774 | // Regression test for https://crbug.com/546991. |
| 16775 | // The server might not be able to serve an IP pooled request, and might send a |
| 16776 | // 421 Misdirected Request response status to indicate this. |
| 16777 | // HttpNetworkTransaction should reset the request and retry without IP pooling. |
| 16778 | TEST_F(HttpNetworkTransactionTest, RetryWithoutConnectionPooling) { |
| 16779 | // Two hosts resolve to the same IP address. |
| 16780 | const std::string ip_addr = "1.2.3.4"; |
| 16781 | IPAddress ip; |
| 16782 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 16783 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 16784 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16785 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16786 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 16787 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 16788 | |
| 16789 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 16790 | |
| 16791 | // Two requests on the first connection. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16792 | spdy::SpdySerializedFrame req1( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16793 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 16794 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16795 | spdy::SpdySerializedFrame req2( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16796 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16797 | spdy::SpdySerializedFrame rst( |
| 16798 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16799 | MockWrite writes1[] = { |
| 16800 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 3), |
| 16801 | CreateMockWrite(rst, 6), |
| 16802 | }; |
| 16803 | |
| 16804 | // The first one succeeds, the second gets error 421 Misdirected Request. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16805 | spdy::SpdySerializedFrame resp1( |
| 16806 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16807 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 16808 | spdy::SpdyHeaderBlock response_headers; |
| 16809 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 16810 | spdy::SpdySerializedFrame resp2( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16811 | spdy_util_.ConstructSpdyReply(3, std::move(response_headers))); |
| 16812 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 16813 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 16814 | |
| 16815 | MockConnect connect1(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16816 | SequencedSocketData data1(connect1, reads1, writes1); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16817 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16818 | |
| 16819 | AddSSLSocketData(); |
| 16820 | |
| 16821 | // Retry the second request on a second connection. |
| 16822 | SpdyTestUtil spdy_util2; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16823 | spdy::SpdySerializedFrame req3( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16824 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 16825 | MockWrite writes2[] = { |
| 16826 | CreateMockWrite(req3, 0), |
| 16827 | }; |
| 16828 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16829 | spdy::SpdySerializedFrame resp3( |
| 16830 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16831 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16832 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 16833 | MockRead(ASYNC, 0, 3)}; |
| 16834 | |
| 16835 | MockConnect connect2(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16836 | SequencedSocketData data2(connect2, reads2, writes2); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16837 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16838 | |
| 16839 | AddSSLSocketData(); |
| 16840 | |
| 16841 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16842 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 16843 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16844 | base::nullopt); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16845 | EXPECT_THAT(rv, IsOk()); |
| 16846 | |
| 16847 | HttpRequestInfo request1; |
| 16848 | request1.method = "GET"; |
| 16849 | request1.url = GURL("https://www.example.org/"); |
| 16850 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16851 | request1.traffic_annotation = |
| 16852 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16853 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 16854 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16855 | TestCompletionCallback callback; |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16856 | rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 16857 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16858 | rv = callback.WaitForResult(); |
| 16859 | EXPECT_THAT(rv, IsOk()); |
| 16860 | |
| 16861 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 16862 | ASSERT_TRUE(response); |
| 16863 | ASSERT_TRUE(response->headers); |
| 16864 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16865 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16866 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16867 | std::string response_data; |
| 16868 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 16869 | EXPECT_EQ("hello!", response_data); |
| 16870 | |
| 16871 | HttpRequestInfo request2; |
| 16872 | request2.method = "GET"; |
| 16873 | request2.url = GURL("https://mail.example.org/"); |
| 16874 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16875 | request2.traffic_annotation = |
| 16876 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16877 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 16878 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 16879 | RecordingBoundTestNetLog log; |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16880 | rv = trans2.Start(&request2, callback.callback(), log.bound()); |
| 16881 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16882 | rv = callback.WaitForResult(); |
| 16883 | EXPECT_THAT(rv, IsOk()); |
| 16884 | |
| 16885 | response = trans2.GetResponseInfo(); |
| 16886 | ASSERT_TRUE(response); |
| 16887 | ASSERT_TRUE(response->headers); |
| 16888 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16889 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16890 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16891 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 16892 | EXPECT_EQ("hello!", response_data); |
| 16893 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 16894 | auto entries = log.GetEntries(); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16895 | ExpectLogContainsSomewhere( |
| 16896 | entries, 0, NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST, |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 16897 | NetLogEventPhase::NONE); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16898 | } |
| 16899 | |
| 16900 | // Test that HTTP 421 responses are properly returned to the caller if received |
| 16901 | // on the retry as well. HttpNetworkTransaction should not infinite loop or lose |
| 16902 | // portions of the response. |
| 16903 | TEST_F(HttpNetworkTransactionTest, ReturnHTTP421OnRetry) { |
| 16904 | // Two hosts resolve to the same IP address. |
| 16905 | const std::string ip_addr = "1.2.3.4"; |
| 16906 | IPAddress ip; |
| 16907 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 16908 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 16909 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 16910 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16911 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 16912 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 16913 | |
| 16914 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 16915 | |
| 16916 | // Two requests on the first connection. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16917 | spdy::SpdySerializedFrame req1( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16918 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 16919 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16920 | spdy::SpdySerializedFrame req2( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16921 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16922 | spdy::SpdySerializedFrame rst( |
| 16923 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16924 | MockWrite writes1[] = { |
| 16925 | CreateMockWrite(req1, 0), CreateMockWrite(req2, 3), |
| 16926 | CreateMockWrite(rst, 6), |
| 16927 | }; |
| 16928 | |
| 16929 | // The first one succeeds, the second gets error 421 Misdirected Request. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16930 | spdy::SpdySerializedFrame resp1( |
| 16931 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 16932 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 16933 | spdy::SpdyHeaderBlock response_headers; |
| 16934 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 16935 | spdy::SpdySerializedFrame resp2( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16936 | spdy_util_.ConstructSpdyReply(3, response_headers.Clone())); |
| 16937 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 16938 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 16939 | |
| 16940 | MockConnect connect1(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16941 | SequencedSocketData data1(connect1, reads1, writes1); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16942 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 16943 | |
| 16944 | AddSSLSocketData(); |
| 16945 | |
| 16946 | // Retry the second request on a second connection. It returns 421 Misdirected |
| 16947 | // Retry again. |
| 16948 | SpdyTestUtil spdy_util2; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16949 | spdy::SpdySerializedFrame req3( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16950 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 16951 | MockWrite writes2[] = { |
| 16952 | CreateMockWrite(req3, 0), |
| 16953 | }; |
| 16954 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16955 | spdy::SpdySerializedFrame resp3( |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16956 | spdy_util2.ConstructSpdyReply(1, std::move(response_headers))); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 16957 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16958 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 16959 | MockRead(ASYNC, 0, 3)}; |
| 16960 | |
| 16961 | MockConnect connect2(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 16962 | SequencedSocketData data2(connect2, reads2, writes2); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16963 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 16964 | |
| 16965 | AddSSLSocketData(); |
| 16966 | |
| 16967 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16968 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 16969 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 16970 | base::nullopt); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16971 | EXPECT_THAT(rv, IsOk()); |
| 16972 | |
| 16973 | HttpRequestInfo request1; |
| 16974 | request1.method = "GET"; |
| 16975 | request1.url = GURL("https://www.example.org/"); |
| 16976 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 16977 | request1.traffic_annotation = |
| 16978 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16979 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 16980 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 16981 | TestCompletionCallback callback; |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 16982 | rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
| 16983 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 16984 | rv = callback.WaitForResult(); |
| 16985 | EXPECT_THAT(rv, IsOk()); |
| 16986 | |
| 16987 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 16988 | ASSERT_TRUE(response); |
| 16989 | ASSERT_TRUE(response->headers); |
| 16990 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 16991 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 16992 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 16993 | std::string response_data; |
| 16994 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
| 16995 | EXPECT_EQ("hello!", response_data); |
| 16996 | |
| 16997 | HttpRequestInfo request2; |
| 16998 | request2.method = "GET"; |
| 16999 | request2.url = GURL("https://mail.example.org/"); |
| 17000 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17001 | request2.traffic_annotation = |
| 17002 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 17003 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 17004 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 17005 | RecordingBoundTestNetLog log; |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 17006 | rv = trans2.Start(&request2, callback.callback(), log.bound()); |
| 17007 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17008 | rv = callback.WaitForResult(); |
| 17009 | EXPECT_THAT(rv, IsOk()); |
| 17010 | |
| 17011 | // After a retry, the 421 Misdirected Request is reported back up to the |
| 17012 | // caller. |
| 17013 | response = trans2.GetResponseInfo(); |
| 17014 | ASSERT_TRUE(response); |
| 17015 | ASSERT_TRUE(response->headers); |
| 17016 | EXPECT_EQ("HTTP/1.1 421", response->headers->GetStatusLine()); |
| 17017 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 17018 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 17019 | EXPECT_TRUE(response->ssl_info.cert); |
| 17020 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
| 17021 | EXPECT_EQ("hello!", response_data); |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 17022 | } |
| 17023 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17024 | TEST_F(HttpNetworkTransactionTest, |
mmenke | 5c64213 | 2015-06-02 16:05:13 | [diff] [blame] | 17025 | UseIPConnectionPoolingWithHostCacheExpiration) { |
Eric Orth | 1da75de | 2019-02-20 21:10:34 | [diff] [blame] | 17026 | // Set up HostResolver to invalidate cached entries after 1 cached resolve. |
| 17027 | session_deps_.host_resolver = |
| 17028 | std::make_unique<MockCachingHostResolver>(1 /* cache_invalidation_num */); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17029 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17030 | |
bnc | 032658ba | 2016-09-26 18:17:15 | [diff] [blame] | 17031 | AddSSLSocketData(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17032 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17033 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17034 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17035 | spdy_util_.UpdateWithStreamDestruction(1); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17036 | spdy::SpdySerializedFrame host2_req( |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 17037 | spdy_util_.ConstructSpdyGet("https://mail.example.com", 3, LOWEST)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17038 | MockWrite spdy_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17039 | CreateMockWrite(host1_req, 0), CreateMockWrite(host2_req, 3), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17040 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17041 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17042 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17043 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17044 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17045 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17046 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17047 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17048 | spdy_util_.ConstructSpdyDataFrame(3, true)); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17049 | MockRead spdy_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17050 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
| 17051 | CreateMockRead(host2_resp, 4), CreateMockRead(host2_resp_body, 5), |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 17052 | MockRead(ASYNC, 0, 6), |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17053 | }; |
| 17054 | |
eroman | 36d84e5443 | 2016-03-17 03:23:02 | [diff] [blame] | 17055 | IPEndPoint peer_addr(IPAddress::IPv4Localhost(), 443); |
[email protected] | d2b5f09 | 2012-06-08 23:55:02 | [diff] [blame] | 17056 | MockConnect connect(ASYNC, OK, peer_addr); |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17057 | SequencedSocketData spdy_data(connect, spdy_reads, spdy_writes); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17058 | session_deps_.socket_factory->AddSocketDataProvider(&spdy_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17059 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 17060 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17061 | HttpRequestInfo request1; |
| 17062 | request1.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17063 | request1.url = GURL("https://www.example.org/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17064 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17065 | request1.traffic_annotation = |
| 17066 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17067 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17068 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17069 | int rv = trans1.Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17070 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17071 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17072 | |
| 17073 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17074 | ASSERT_TRUE(response); |
| 17075 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17076 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17077 | |
| 17078 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17079 | ASSERT_THAT(ReadTransaction(&trans1, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17080 | EXPECT_EQ("hello!", response_data); |
| 17081 | |
| 17082 | // Preload cache entries into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 17083 | rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 17084 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 17085 | base::nullopt); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17086 | EXPECT_THAT(rv, IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17087 | |
| 17088 | HttpRequestInfo request2; |
| 17089 | request2.method = "GET"; |
bnc | a4d611d | 2016-09-22 19:55:37 | [diff] [blame] | 17090 | request2.url = GURL("https://mail.example.com/"); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17091 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17092 | request2.traffic_annotation = |
| 17093 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17094 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17095 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17096 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17097 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17098 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17099 | |
| 17100 | response = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17101 | ASSERT_TRUE(response); |
| 17102 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17103 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17104 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17105 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17106 | ASSERT_THAT(ReadTransaction(&trans2, &response_data), IsOk()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17107 | EXPECT_EQ("hello!", response_data); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 17108 | } |
| 17109 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17110 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionForHttp) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17111 | const std::string https_url = "https://www.example.org:8080/"; |
| 17112 | const std::string http_url = "http://www.example.org:8080/"; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17113 | |
| 17114 | // SPDY GET for HTTPS URL |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17115 | spdy::SpdySerializedFrame req1( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17116 | spdy_util_.ConstructSpdyGet(https_url.c_str(), 1, LOWEST)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17117 | |
| 17118 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17119 | CreateMockWrite(req1, 0), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17120 | }; |
| 17121 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17122 | spdy::SpdySerializedFrame resp1( |
| 17123 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17124 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17125 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17126 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3)}; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17127 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17128 | SequencedSocketData data1(reads1, writes1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17129 | MockConnect connect_data1(ASYNC, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 17130 | data1.set_connect_data(connect_data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17131 | |
| 17132 | // HTTP GET for the HTTP URL |
| 17133 | MockWrite writes2[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 17134 | MockWrite(ASYNC, 0, |
lgarron | a91df87f | 2014-12-05 00:51:34 | [diff] [blame] | 17135 | "GET / HTTP/1.1\r\n" |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17136 | "Host: www.example.org:8080\r\n" |
lgarron | a91df87f | 2014-12-05 00:51:34 | [diff] [blame] | 17137 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17138 | }; |
| 17139 | |
| 17140 | MockRead reads2[] = { |
rch | 8e6c6c4 | 2015-05-01 14:05:13 | [diff] [blame] | 17141 | MockRead(ASYNC, 1, "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 17142 | MockRead(ASYNC, 2, "hello"), |
| 17143 | MockRead(ASYNC, OK, 3), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17144 | }; |
| 17145 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17146 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17147 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17148 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17149 | ssl.next_proto = kProtoHTTP2; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17150 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17151 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 17152 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17153 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17154 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17155 | |
| 17156 | // Start the first transaction to set up the SpdySession |
| 17157 | HttpRequestInfo request1; |
| 17158 | request1.method = "GET"; |
| 17159 | request1.url = GURL(https_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17160 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17161 | request1.traffic_annotation = |
| 17162 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17163 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17164 | TestCompletionCallback callback1; |
| 17165 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17166 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17167 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17168 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17169 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17170 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17171 | |
| 17172 | // Now, start the HTTP request |
| 17173 | HttpRequestInfo request2; |
| 17174 | request2.method = "GET"; |
| 17175 | request2.url = GURL(http_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17176 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17177 | request2.traffic_annotation = |
| 17178 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17179 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17180 | TestCompletionCallback callback2; |
| 17181 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17182 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17183 | base::RunLoop().RunUntilIdle(); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17184 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17185 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17186 | EXPECT_FALSE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17187 | } |
| 17188 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17189 | // Alternative service requires HTTP/2 (or SPDY), but HTTP/1.1 is negotiated |
| 17190 | // with the alternative server. That connection should not be used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17191 | TEST_F(HttpNetworkTransactionTest, AlternativeServiceNotOnHttp11) { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17192 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 17193 | HostPortPair alternative("www.example.org", 444); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17194 | |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17195 | // Negotiate HTTP/1.1 with alternative. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17196 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17197 | ssl.next_proto = kProtoHTTP11; |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17198 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17199 | |
| 17200 | // No data should be read from the alternative, because HTTP/1.1 is |
| 17201 | // negotiated. |
| 17202 | StaticSocketDataProvider data; |
| 17203 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17204 | |
| 17205 | // This test documents that an alternate Job should not be used if HTTP/1.1 is |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17206 | // negotiated. In order to test this, a failed connection to the server is |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17207 | // mocked. This way the request relies on the alternate Job. |
| 17208 | StaticSocketDataProvider data_refused; |
| 17209 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 17210 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 17211 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17212 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17213 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17214 | HttpServerProperties* http_server_properties = |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17215 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17216 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17217 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17218 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17219 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17220 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17221 | HttpRequestInfo request; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17222 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17223 | request.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17224 | request.url = GURL("https://www.example.org:443"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17225 | request.traffic_annotation = |
| 17226 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17227 | TestCompletionCallback callback; |
| 17228 | |
| 17229 | // HTTP/2 (or SPDY) is required for alternative service, if HTTP/1.1 is |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17230 | // negotiated, the alternate Job should fail with ERR_ALPN_NEGOTIATION_FAILED. |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17231 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17232 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_ALPN_NEGOTIATION_FAILED)); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17233 | } |
| 17234 | |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17235 | // A request to a server with an alternative service fires two Jobs: one to the |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17236 | // server, and an alternate one to the alternative server. If the former |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17237 | // succeeds, the request should succeed, even if the latter fails because |
| 17238 | // HTTP/1.1 is negotiated which is insufficient for alternative service. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17239 | TEST_F(HttpNetworkTransactionTest, FailedAlternativeServiceIsNotUserVisible) { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17240 | url::SchemeHostPort server("https", "www.example.org", 443); |
| 17241 | HostPortPair alternative("www.example.org", 444); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17242 | |
| 17243 | // Negotiate HTTP/1.1 with alternative. |
| 17244 | SSLSocketDataProvider alternative_ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17245 | alternative_ssl.next_proto = kProtoHTTP11; |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17246 | session_deps_.socket_factory->AddSSLSocketDataProvider(&alternative_ssl); |
| 17247 | |
| 17248 | // No data should be read from the alternative, because HTTP/1.1 is |
| 17249 | // negotiated. |
| 17250 | StaticSocketDataProvider data; |
| 17251 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17252 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17253 | // Negotiate HTTP/1.1 with server. |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17254 | SSLSocketDataProvider origin_ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17255 | origin_ssl.next_proto = kProtoHTTP11; |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17256 | session_deps_.socket_factory->AddSSLSocketDataProvider(&origin_ssl); |
| 17257 | |
| 17258 | MockWrite http_writes[] = { |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17259 | MockWrite("GET / HTTP/1.1\r\n" |
| 17260 | "Host: www.example.org\r\n" |
| 17261 | "Connection: keep-alive\r\n\r\n"), |
| 17262 | MockWrite("GET /second HTTP/1.1\r\n" |
| 17263 | "Host: www.example.org\r\n" |
| 17264 | "Connection: keep-alive\r\n\r\n"), |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17265 | }; |
| 17266 | |
| 17267 | MockRead http_reads[] = { |
| 17268 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17269 | MockRead("Content-Type: text/html\r\n"), |
| 17270 | MockRead("Content-Length: 6\r\n\r\n"), |
| 17271 | MockRead("foobar"), |
| 17272 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17273 | MockRead("Content-Type: text/html\r\n"), |
| 17274 | MockRead("Content-Length: 7\r\n\r\n"), |
| 17275 | MockRead("another"), |
| 17276 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17277 | StaticSocketDataProvider http_data(http_reads, http_writes); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17278 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17279 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17280 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17281 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17282 | HttpServerProperties* http_server_properties = |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17283 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17284 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17285 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17286 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17287 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17288 | |
| 17289 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
| 17290 | HttpRequestInfo request1; |
| 17291 | request1.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17292 | request1.url = GURL("https://www.example.org:443"); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17293 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17294 | request1.traffic_annotation = |
| 17295 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17296 | TestCompletionCallback callback1; |
| 17297 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17298 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17299 | rv = callback1.GetResult(rv); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17300 | EXPECT_THAT(rv, IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17301 | |
| 17302 | const HttpResponseInfo* response1 = trans1.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17303 | ASSERT_TRUE(response1); |
| 17304 | ASSERT_TRUE(response1->headers); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17305 | EXPECT_EQ("HTTP/1.1 200 OK", response1->headers->GetStatusLine()); |
| 17306 | |
| 17307 | std::string response_data1; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17308 | ASSERT_THAT(ReadTransaction(&trans1, &response_data1), IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17309 | EXPECT_EQ("foobar", response_data1); |
| 17310 | |
| 17311 | // Alternative should be marked as broken, because HTTP/1.1 is not sufficient |
| 17312 | // for alternative service. |
Matt Menke | b32ba512 | 2019-09-10 19:17:05 | [diff] [blame] | 17313 | EXPECT_TRUE(http_server_properties->IsAlternativeServiceBroken( |
| 17314 | alternative_service, NetworkIsolationKey())); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17315 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17316 | // Since |alternative_service| is broken, a second transaction to server |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17317 | // should not start an alternate Job. It should pool to existing connection |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17318 | // to server. |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17319 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
| 17320 | HttpRequestInfo request2; |
| 17321 | request2.method = "GET"; |
bnc | 8bef8da2 | 2016-05-30 01:28:25 | [diff] [blame] | 17322 | request2.url = GURL("https://www.example.org:443/second"); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17323 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17324 | request2.traffic_annotation = |
| 17325 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17326 | TestCompletionCallback callback2; |
| 17327 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17328 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17329 | rv = callback2.GetResult(rv); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17330 | EXPECT_THAT(rv, IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17331 | |
| 17332 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17333 | ASSERT_TRUE(response2); |
| 17334 | ASSERT_TRUE(response2->headers); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17335 | EXPECT_EQ("HTTP/1.1 200 OK", response2->headers->GetStatusLine()); |
| 17336 | |
| 17337 | std::string response_data2; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17338 | ASSERT_THAT(ReadTransaction(&trans2, &response_data2), IsOk()); |
bnc | 40448a53 | 2015-05-11 19:13:14 | [diff] [blame] | 17339 | EXPECT_EQ("another", response_data2); |
| 17340 | } |
| 17341 | |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17342 | // Alternative service requires HTTP/2 (or SPDY), but there is already a |
| 17343 | // HTTP/1.1 socket open to the alternative server. That socket should not be |
| 17344 | // used. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17345 | TEST_F(HttpNetworkTransactionTest, AlternativeServiceShouldNotPoolToHttp11) { |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17346 | url::SchemeHostPort server("https", "origin.example.org", 443); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17347 | HostPortPair alternative("alternative.example.org", 443); |
| 17348 | std::string origin_url = "https://origin.example.org:443"; |
| 17349 | std::string alternative_url = "https://alternative.example.org:443"; |
| 17350 | |
| 17351 | // Negotiate HTTP/1.1 with alternative.example.org. |
| 17352 | SSLSocketDataProvider ssl(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17353 | ssl.next_proto = kProtoHTTP11; |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17354 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 17355 | |
| 17356 | // HTTP/1.1 data for |request1| and |request2|. |
| 17357 | MockWrite http_writes[] = { |
| 17358 | MockWrite( |
| 17359 | "GET / HTTP/1.1\r\n" |
| 17360 | "Host: alternative.example.org\r\n" |
| 17361 | "Connection: keep-alive\r\n\r\n"), |
| 17362 | MockWrite( |
| 17363 | "GET / HTTP/1.1\r\n" |
| 17364 | "Host: alternative.example.org\r\n" |
| 17365 | "Connection: keep-alive\r\n\r\n"), |
| 17366 | }; |
| 17367 | |
| 17368 | MockRead http_reads[] = { |
| 17369 | MockRead( |
| 17370 | "HTTP/1.1 200 OK\r\n" |
| 17371 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 17372 | "Content-Length: 40\r\n\r\n" |
| 17373 | "first HTTP/1.1 response from alternative"), |
| 17374 | MockRead( |
| 17375 | "HTTP/1.1 200 OK\r\n" |
| 17376 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 17377 | "Content-Length: 41\r\n\r\n" |
| 17378 | "second HTTP/1.1 response from alternative"), |
| 17379 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17380 | StaticSocketDataProvider http_data(http_reads, http_writes); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17381 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17382 | |
| 17383 | // This test documents that an alternate Job should not pool to an already |
| 17384 | // existing HTTP/1.1 connection. In order to test this, a failed connection |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17385 | // to the server is mocked. This way |request2| relies on the alternate Job. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17386 | StaticSocketDataProvider data_refused; |
| 17387 | data_refused.set_connect_data(MockConnect(ASYNC, ERR_CONNECTION_REFUSED)); |
| 17388 | session_deps_.socket_factory->AddSocketDataProvider(&data_refused); |
| 17389 | |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17390 | // Set up alternative service for server. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17391 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 525e175a | 2016-06-20 12:36:40 | [diff] [blame] | 17392 | HttpServerProperties* http_server_properties = |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17393 | session->http_server_properties(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 17394 | AlternativeService alternative_service(kProtoHTTP2, alternative); |
bnc | 7dc7e1b4 | 2015-07-28 14:43:12 | [diff] [blame] | 17395 | base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
zhongyi | e537a00 | 2017-06-27 16:48:21 | [diff] [blame] | 17396 | http_server_properties->SetHttp2AlternativeService( |
Matt Menke | 9aa8626 | 2019-08-21 15:52:07 | [diff] [blame] | 17397 | server, NetworkIsolationKey(), alternative_service, expiration); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17398 | |
| 17399 | // First transaction to alternative to open an HTTP/1.1 socket. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17400 | HttpRequestInfo request1; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17401 | HttpNetworkTransaction trans1(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17402 | request1.method = "GET"; |
| 17403 | request1.url = GURL(alternative_url); |
| 17404 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17405 | request1.traffic_annotation = |
| 17406 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17407 | TestCompletionCallback callback1; |
| 17408 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17409 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17410 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17411 | const HttpResponseInfo* response1 = trans1.GetResponseInfo(); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17412 | ASSERT_TRUE(response1); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17413 | ASSERT_TRUE(response1->headers); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17414 | EXPECT_EQ("HTTP/1.1 200 OK", response1->headers->GetStatusLine()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17415 | EXPECT_TRUE(response1->was_alpn_negotiated); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17416 | EXPECT_FALSE(response1->was_fetched_via_spdy); |
| 17417 | std::string response_data1; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17418 | ASSERT_THAT(ReadTransaction(&trans1, &response_data1), IsOk()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17419 | EXPECT_EQ("first HTTP/1.1 response from alternative", response_data1); |
| 17420 | |
| 17421 | // Request for origin.example.org, which has an alternative service. This |
| 17422 | // will start two Jobs: the alternative looks for connections to pool to, |
| 17423 | // finds one which is HTTP/1.1, and should ignore it, and should not try to |
zhongyi | 3d4a55e7 | 2016-04-22 20:36:46 | [diff] [blame] | 17424 | // open other connections to alternative server. The Job to server fails, so |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17425 | // this request fails. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17426 | HttpRequestInfo request2; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17427 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17428 | request2.method = "GET"; |
| 17429 | request2.url = GURL(origin_url); |
| 17430 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17431 | request2.traffic_annotation = |
| 17432 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17433 | TestCompletionCallback callback2; |
| 17434 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17435 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17436 | EXPECT_THAT(callback2.GetResult(rv), IsError(ERR_CONNECTION_REFUSED)); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17437 | |
| 17438 | // Another transaction to alternative. This is to test that the HTTP/1.1 |
| 17439 | // socket is still open and in the pool. |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17440 | HttpRequestInfo request3; |
krasin | c06a72a | 2016-12-21 03:42:46 | [diff] [blame] | 17441 | HttpNetworkTransaction trans3(DEFAULT_PRIORITY, session.get()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17442 | request3.method = "GET"; |
| 17443 | request3.url = GURL(alternative_url); |
| 17444 | request3.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17445 | request3.traffic_annotation = |
| 17446 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17447 | TestCompletionCallback callback3; |
| 17448 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17449 | rv = trans3.Start(&request3, callback3.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17450 | EXPECT_THAT(callback3.GetResult(rv), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17451 | const HttpResponseInfo* response3 = trans3.GetResponseInfo(); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17452 | ASSERT_TRUE(response3); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17453 | ASSERT_TRUE(response3->headers); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17454 | EXPECT_EQ("HTTP/1.1 200 OK", response3->headers->GetStatusLine()); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17455 | EXPECT_TRUE(response3->was_alpn_negotiated); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17456 | EXPECT_FALSE(response3->was_fetched_via_spdy); |
| 17457 | std::string response_data3; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17458 | ASSERT_THAT(ReadTransaction(&trans3, &response_data3), IsOk()); |
bnc | 5452e2a | 2015-05-08 16:27:42 | [diff] [blame] | 17459 | EXPECT_EQ("second HTTP/1.1 response from alternative", response_data3); |
| 17460 | } |
| 17461 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17462 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionForHttpOverTunnel) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17463 | const std::string https_url = "https://www.example.org:8080/"; |
| 17464 | const std::string http_url = "http://www.example.org:8080/"; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17465 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17466 | // Separate SPDY util instance for naked and wrapped requests. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17467 | SpdyTestUtil spdy_util_wrapped; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17468 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17469 | // SPDY GET for HTTPS URL (through CONNECT tunnel) |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17470 | const HostPortPair host_port_pair("www.example.org", 8080); |
Matt Menke | 6e879bd | 2019-03-18 17:26:04 | [diff] [blame] | 17471 | spdy::SpdySerializedFrame connect(spdy_util_.ConstructSpdyConnect( |
| 17472 | nullptr, 0, 1, HttpProxyConnectJob::kH2QuicTunnelPriority, |
| 17473 | host_port_pair)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17474 | spdy::SpdySerializedFrame req1( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17475 | spdy_util_wrapped.ConstructSpdyGet(https_url.c_str(), 1, LOWEST)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17476 | spdy::SpdySerializedFrame wrapped_req1( |
[email protected] | 23e48228 | 2013-06-14 16:08:02 | [diff] [blame] | 17477 | spdy_util_.ConstructWrappedSpdyFrame(req1, 1)); |
[email protected] | 601e03f1 | 2014-04-06 16:26:39 | [diff] [blame] | 17478 | |
| 17479 | // SPDY GET for HTTP URL (through the proxy, but not the tunnel). |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17480 | spdy::SpdyHeaderBlock req2_block; |
| 17481 | req2_block[spdy::kHttp2MethodHeader] = "GET"; |
| 17482 | req2_block[spdy::kHttp2AuthorityHeader] = "www.example.org:8080"; |
| 17483 | req2_block[spdy::kHttp2SchemeHeader] = "http"; |
| 17484 | req2_block[spdy::kHttp2PathHeader] = "/"; |
| 17485 | spdy::SpdySerializedFrame req2( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17486 | spdy_util_.ConstructSpdyHeaders(3, std::move(req2_block), MEDIUM, true)); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17487 | |
| 17488 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17489 | CreateMockWrite(connect, 0), CreateMockWrite(wrapped_req1, 2), |
| 17490 | CreateMockWrite(req2, 6), |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17491 | }; |
| 17492 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17493 | spdy::SpdySerializedFrame conn_resp( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17494 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17495 | spdy::SpdySerializedFrame resp1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17496 | spdy_util_wrapped.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17497 | spdy::SpdySerializedFrame body1( |
| 17498 | spdy_util_wrapped.ConstructSpdyDataFrame(1, true)); |
| 17499 | spdy::SpdySerializedFrame wrapped_resp1( |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17500 | spdy_util_wrapped.ConstructWrappedSpdyFrame(resp1, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17501 | spdy::SpdySerializedFrame wrapped_body1( |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17502 | spdy_util_wrapped.ConstructWrappedSpdyFrame(body1, 1)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17503 | spdy::SpdySerializedFrame resp2( |
| 17504 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 3)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17505 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(3, true)); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17506 | MockRead reads1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17507 | CreateMockRead(conn_resp, 1), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17508 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17509 | CreateMockRead(wrapped_resp1, 4), |
| 17510 | CreateMockRead(wrapped_body1, 5), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17511 | MockRead(ASYNC, ERR_IO_PENDING, 7), |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17512 | CreateMockRead(resp2, 8), |
| 17513 | CreateMockRead(body2, 9), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17514 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 10), |
| 17515 | }; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17516 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17517 | SequencedSocketData data1(reads1, writes1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17518 | MockConnect connect_data1(ASYNC, OK); |
[email protected] | dd54bd8 | 2012-07-19 23:44:57 | [diff] [blame] | 17519 | data1.set_connect_data(connect_data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17520 | |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 17521 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 17522 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 17523 | "HTTPS proxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 17524 | RecordingTestNetLog log; |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17525 | session_deps_.net_log = &log; |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17526 | SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17527 | ssl1.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17528 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17529 | SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17530 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17531 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17532 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17533 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17534 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17535 | |
| 17536 | // Start the first transaction to set up the SpdySession |
| 17537 | HttpRequestInfo request1; |
| 17538 | request1.method = "GET"; |
| 17539 | request1.url = GURL(https_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17540 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17541 | request1.traffic_annotation = |
| 17542 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17543 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17544 | TestCompletionCallback callback1; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17545 | int rv = trans1.Start(&request1, callback1.callback(), NetLogWithSource()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17546 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17547 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17548 | data1.RunUntilPaused(); |
| 17549 | base::RunLoop().RunUntilIdle(); |
| 17550 | data1.Resume(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17551 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17552 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17553 | |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 17554 | LoadTimingInfo load_timing_info1; |
| 17555 | EXPECT_TRUE(trans1.GetLoadTimingInfo(&load_timing_info1)); |
| 17556 | TestLoadTimingNotReusedWithPac(load_timing_info1, |
| 17557 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 17558 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17559 | // Now, start the HTTP request. |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17560 | HttpRequestInfo request2; |
| 17561 | request2.method = "GET"; |
| 17562 | request2.url = GURL(http_url); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17563 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17564 | request2.traffic_annotation = |
| 17565 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17566 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17567 | TestCompletionCallback callback2; |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17568 | rv = trans2.Start(&request2, callback2.callback(), NetLogWithSource()); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17569 | |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17570 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17571 | data1.RunUntilPaused(); |
| 17572 | base::RunLoop().RunUntilIdle(); |
| 17573 | data1.Resume(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17574 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17575 | |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17576 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
[email protected] | f6c63db5 | 2013-02-02 00:35:22 | [diff] [blame] | 17577 | |
| 17578 | LoadTimingInfo load_timing_info2; |
| 17579 | EXPECT_TRUE(trans2.GetLoadTimingInfo(&load_timing_info2)); |
| 17580 | // The established SPDY sessions is considered reused by the HTTP request. |
| 17581 | TestLoadTimingReusedWithPac(load_timing_info2); |
| 17582 | // HTTP requests over a SPDY session should have a different connection |
| 17583 | // socket_log_id than requests over a tunnel. |
| 17584 | EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id); |
[email protected] | 8450d72 | 2012-07-02 19:14:04 | [diff] [blame] | 17585 | } |
| 17586 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17587 | // Test that in the case where we have a SPDY session to a SPDY proxy |
| 17588 | // that we do not pool other origins that resolve to the same IP when |
| 17589 | // the certificate does not match the new origin. |
| 17590 | // http://crbug.com/134690 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17591 | TEST_F(HttpNetworkTransactionTest, DoNotUseSpdySessionIfCertDoesNotMatch) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17592 | const std::string url1 = "http://www.example.org/"; |
| 17593 | const std::string url2 = "https://news.example.org/"; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17594 | const std::string ip_addr = "1.2.3.4"; |
| 17595 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17596 | // Second SpdyTestUtil instance for the second socket. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17597 | SpdyTestUtil spdy_util_secure; |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17598 | |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17599 | // SPDY GET for HTTP URL (through SPDY proxy) |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17600 | spdy::SpdyHeaderBlock headers( |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17601 | spdy_util_.ConstructGetHeaderBlockForProxy("http://www.example.org/")); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17602 | spdy::SpdySerializedFrame req1( |
bnc | 4233140 | 2016-07-25 13:36:15 | [diff] [blame] | 17603 | spdy_util_.ConstructSpdyHeaders(1, std::move(headers), LOWEST, true)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17604 | |
| 17605 | MockWrite writes1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17606 | CreateMockWrite(req1, 0), |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17607 | }; |
| 17608 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17609 | spdy::SpdySerializedFrame resp1( |
| 17610 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17611 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17612 | MockRead reads1[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17613 | MockRead(ASYNC, ERR_IO_PENDING, 1), CreateMockRead(resp1, 2), |
| 17614 | CreateMockRead(body1, 3), MockRead(ASYNC, OK, 4), // EOF |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17615 | }; |
| 17616 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17617 | SequencedSocketData data1(reads1, writes1); |
martijn | fe9636e | 2016-02-06 14:33:32 | [diff] [blame] | 17618 | IPAddress ip; |
martijn | 654c8c4 | 2016-02-10 22:10:59 | [diff] [blame] | 17619 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17620 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 17621 | MockConnect connect_data1(ASYNC, OK, peer_addr); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17622 | data1.set_connect_data(connect_data1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17623 | |
| 17624 | // SPDY GET for HTTPS URL (direct) |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17625 | spdy::SpdySerializedFrame req2( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17626 | spdy_util_secure.ConstructSpdyGet(url2.c_str(), 1, MEDIUM)); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17627 | |
| 17628 | MockWrite writes2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17629 | CreateMockWrite(req2, 0), |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17630 | }; |
| 17631 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17632 | spdy::SpdySerializedFrame resp2( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17633 | spdy_util_secure.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17634 | spdy::SpdySerializedFrame body2( |
| 17635 | spdy_util_secure.ConstructSpdyDataFrame(1, true)); |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17636 | MockRead reads2[] = {CreateMockRead(resp2, 1), CreateMockRead(body2, 2), |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17637 | MockRead(ASYNC, OK, 3)}; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17638 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17639 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17640 | MockConnect connect_data2(ASYNC, OK); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17641 | data2.set_connect_data(connect_data2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17642 | |
| 17643 | // Set up a proxy config that sends HTTP requests to a proxy, and |
| 17644 | // all others direct. |
| 17645 | ProxyConfig proxy_config; |
| 17646 | proxy_config.proxy_rules().ParseFromString("http=https://proxy:443"); |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 17647 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 17648 | std::make_unique<ConfiguredProxyResolutionService>( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 17649 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 17650 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 17651 | nullptr, nullptr, /*quick_check_enabled=*/true); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17652 | |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17653 | SSLSocketDataProvider ssl1(ASYNC, OK); // to the proxy |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17654 | ssl1.next_proto = kProtoHTTP2; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17655 | // Load a valid cert. Note, that this does not need to |
| 17656 | // be valid for proxy because the MockSSLClientSocket does |
| 17657 | // not actually verify it. But SpdySession will use this |
| 17658 | // to see if it is valid for the new origin |
Ryan Sleevi | 4f83209 | 2017-11-21 23:25:49 | [diff] [blame] | 17659 | ssl1.ssl_info.cert = |
| 17660 | ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem"); |
| 17661 | ASSERT_TRUE(ssl1.ssl_info.cert); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17662 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17663 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17664 | |
| 17665 | SSLSocketDataProvider ssl2(ASYNC, OK); // to the server |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17666 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17667 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17668 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17669 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17670 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17671 | session_deps_.host_resolver->rules()->AddRule("news.example.org", ip_addr); |
[email protected] | bb88e1d3 | 2013-05-03 23:11:07 | [diff] [blame] | 17672 | session_deps_.host_resolver->rules()->AddRule("proxy", ip_addr); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17673 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17674 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17675 | |
| 17676 | // Start the first transaction to set up the SpdySession |
| 17677 | HttpRequestInfo request1; |
| 17678 | request1.method = "GET"; |
| 17679 | request1.url = GURL(url1); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17680 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17681 | request1.traffic_annotation = |
| 17682 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17683 | HttpNetworkTransaction trans1(LOWEST, session.get()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17684 | TestCompletionCallback callback1; |
| 17685 | ASSERT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17686 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
mmenke | 666a6fea | 2015-12-19 04:16:33 | [diff] [blame] | 17687 | // This pause is a hack to avoid running into https://crbug.com/497228. |
| 17688 | data1.RunUntilPaused(); |
| 17689 | base::RunLoop().RunUntilIdle(); |
| 17690 | data1.Resume(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17691 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17692 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17693 | EXPECT_TRUE(trans1.GetResponseInfo()->was_fetched_via_spdy); |
| 17694 | |
| 17695 | // Now, start the HTTP request |
| 17696 | HttpRequestInfo request2; |
| 17697 | request2.method = "GET"; |
| 17698 | request2.url = GURL(url2); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17699 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17700 | request2.traffic_annotation = |
| 17701 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17702 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17703 | TestCompletionCallback callback2; |
| 17704 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17705 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
fdoray | 92e35a7 | 2016-06-10 15:54:55 | [diff] [blame] | 17706 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17707 | |
| 17708 | ASSERT_TRUE(callback2.have_result()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17709 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 17710 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17711 | } |
| 17712 | |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17713 | // Test to verify that a failed socket read (due to an ERR_CONNECTION_CLOSED |
| 17714 | // error) in SPDY session, removes the socket from pool and closes the SPDY |
| 17715 | // session. Verify that new url's from the same HttpNetworkSession (and a new |
| 17716 | // SpdySession) do work. http://crbug.com/224701 |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17717 | TEST_F(HttpNetworkTransactionTest, ErrorSocketNotConnected) { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17718 | const std::string https_url = "https://www.example.org/"; |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17719 | |
| 17720 | MockRead reads1[] = { |
| 17721 | MockRead(SYNCHRONOUS, ERR_CONNECTION_CLOSED, 0) |
| 17722 | }; |
| 17723 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17724 | SequencedSocketData data1(reads1, base::span<MockWrite>()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17725 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17726 | spdy::SpdySerializedFrame req2( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17727 | spdy_util_.ConstructSpdyGet(https_url.c_str(), 1, MEDIUM)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17728 | MockWrite writes2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17729 | CreateMockWrite(req2, 0), |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17730 | }; |
| 17731 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17732 | spdy::SpdySerializedFrame resp2( |
| 17733 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17734 | spdy::SpdySerializedFrame body2(spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17735 | MockRead reads2[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17736 | CreateMockRead(resp2, 1), CreateMockRead(body2, 2), |
| 17737 | MockRead(ASYNC, OK, 3) // EOF |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17738 | }; |
| 17739 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17740 | SequencedSocketData data2(reads2, writes2); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17741 | |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17742 | SSLSocketDataProvider ssl1(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17743 | ssl1.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17744 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17745 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17746 | |
| 17747 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17748 | ssl2.next_proto = kProtoHTTP2; |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17749 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17750 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17751 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17752 | std::unique_ptr<HttpNetworkSession> session( |
mmenke | 11eb515 | 2015-06-09 14:50:50 | [diff] [blame] | 17753 | SpdySessionDependencies::SpdyCreateSession(&session_deps_)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17754 | |
| 17755 | // Start the first transaction to set up the SpdySession and verify that |
| 17756 | // connection was closed. |
| 17757 | HttpRequestInfo request1; |
| 17758 | request1.method = "GET"; |
| 17759 | request1.url = GURL(https_url); |
| 17760 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17761 | request1.traffic_annotation = |
| 17762 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17763 | HttpNetworkTransaction trans1(MEDIUM, session.get()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17764 | TestCompletionCallback callback1; |
| 17765 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17766 | trans1.Start(&request1, callback1.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17767 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_CLOSED)); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17768 | |
| 17769 | // Now, start the second request and make sure it succeeds. |
| 17770 | HttpRequestInfo request2; |
| 17771 | request2.method = "GET"; |
| 17772 | request2.url = GURL(https_url); |
| 17773 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17774 | request2.traffic_annotation = |
| 17775 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 17776 | HttpNetworkTransaction trans2(MEDIUM, session.get()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17777 | TestCompletionCallback callback2; |
| 17778 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17779 | trans2.Start(&request2, callback2.callback(), NetLogWithSource())); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17780 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17781 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 85f9734 | 2013-04-17 06:12:24 | [diff] [blame] | 17782 | EXPECT_TRUE(trans2.GetResponseInfo()->was_fetched_via_spdy); |
| 17783 | } |
| 17784 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17785 | TEST_F(HttpNetworkTransactionTest, CloseIdleSpdySessionToOpenNewOne) { |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17786 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 17787 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 17788 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 17789 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 17790 | |
| 17791 | // Use two different hosts with different IPs so they don't get pooled. |
| 17792 | session_deps_.host_resolver->rules()->AddRule("www.a.com", "10.0.0.1"); |
| 17793 | session_deps_.host_resolver->rules()->AddRule("www.b.com", "10.0.0.2"); |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17794 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17795 | |
| 17796 | SSLSocketDataProvider ssl1(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17797 | ssl1.next_proto = kProtoHTTP2; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17798 | SSLSocketDataProvider ssl2(ASYNC, OK); |
bnc | 3cf2a59 | 2016-08-11 14:48:36 | [diff] [blame] | 17799 | ssl2.next_proto = kProtoHTTP2; |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17800 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 17801 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 17802 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17803 | spdy::SpdySerializedFrame host1_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17804 | spdy_util_.ConstructSpdyGet("https://www.a.com", 1, DEFAULT_PRIORITY)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17805 | MockWrite spdy1_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17806 | CreateMockWrite(host1_req, 0), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17807 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17808 | spdy::SpdySerializedFrame host1_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17809 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17810 | spdy::SpdySerializedFrame host1_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17811 | spdy_util_.ConstructSpdyDataFrame(1, true)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17812 | MockRead spdy1_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17813 | CreateMockRead(host1_resp, 1), CreateMockRead(host1_resp_body, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17814 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17815 | }; |
| 17816 | |
rdsmith | ebb50aa | 2015-11-12 03:44:38 | [diff] [blame] | 17817 | // Use a separate test instance for the separate SpdySession that will be |
| 17818 | // created. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17819 | SpdyTestUtil spdy_util_2; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17820 | SequencedSocketData spdy1_data(spdy1_reads, spdy1_writes); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 17821 | session_deps_.socket_factory->AddSocketDataProvider(&spdy1_data); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17822 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17823 | spdy::SpdySerializedFrame host2_req( |
bnc | 38dcd39 | 2016-02-09 23:19:49 | [diff] [blame] | 17824 | spdy_util_2.ConstructSpdyGet("https://www.b.com", 1, DEFAULT_PRIORITY)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17825 | MockWrite spdy2_writes[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17826 | CreateMockWrite(host2_req, 0), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17827 | }; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17828 | spdy::SpdySerializedFrame host2_resp( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 17829 | spdy_util_2.ConstructSpdyGetReply(nullptr, 0, 1)); |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 17830 | spdy::SpdySerializedFrame host2_resp_body( |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17831 | spdy_util_2.ConstructSpdyDataFrame(1, true)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17832 | MockRead spdy2_reads[] = { |
bnc | df80d44fd | 2016-07-15 20:27:41 | [diff] [blame] | 17833 | CreateMockRead(host2_resp, 1), CreateMockRead(host2_resp_body, 2), |
mmenke | e2401192 | 2015-12-17 22:12:59 | [diff] [blame] | 17834 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17835 | }; |
| 17836 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17837 | SequencedSocketData spdy2_data(spdy2_reads, spdy2_writes); |
Bence Béky | 53a5aef | 2018-03-29 21:54:12 | [diff] [blame] | 17838 | session_deps_.socket_factory->AddSocketDataProvider(&spdy2_data); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17839 | |
| 17840 | MockWrite http_write[] = { |
| 17841 | MockWrite("GET / HTTP/1.1\r\n" |
| 17842 | "Host: www.a.com\r\n" |
| 17843 | "Connection: keep-alive\r\n\r\n"), |
| 17844 | }; |
| 17845 | |
| 17846 | MockRead http_read[] = { |
| 17847 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 17848 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 17849 | MockRead("Content-Length: 6\r\n\r\n"), |
| 17850 | MockRead("hello!"), |
| 17851 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 17852 | StaticSocketDataProvider http_data(http_read, http_write); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17853 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 17854 | |
| 17855 | HostPortPair host_port_pair_a("www.a.com", 443); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17856 | SpdySessionKey spdy_session_key_a( |
| 17857 | host_port_pair_a, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17858 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17859 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17860 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17861 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17862 | |
| 17863 | TestCompletionCallback callback; |
| 17864 | HttpRequestInfo request1; |
| 17865 | request1.method = "GET"; |
| 17866 | request1.url = GURL("https://www.a.com/"); |
| 17867 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17868 | request1.traffic_annotation = |
| 17869 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17870 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17871 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17872 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17873 | int rv = trans->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17874 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17875 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17876 | |
| 17877 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17878 | ASSERT_TRUE(response); |
| 17879 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17880 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17881 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17882 | EXPECT_TRUE(response->was_alpn_negotiated); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17883 | |
| 17884 | std::string response_data; |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17885 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17886 | EXPECT_EQ("hello!", response_data); |
| 17887 | trans.reset(); |
| 17888 | EXPECT_TRUE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17889 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17890 | |
| 17891 | HostPortPair host_port_pair_b("www.b.com", 443); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17892 | SpdySessionKey spdy_session_key_b( |
| 17893 | host_port_pair_b, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17894 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17895 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17896 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17897 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17898 | HttpRequestInfo request2; |
| 17899 | request2.method = "GET"; |
| 17900 | request2.url = GURL("https://www.b.com/"); |
| 17901 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17902 | request2.traffic_annotation = |
| 17903 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17904 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17905 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17906 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17907 | rv = trans->Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17908 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17909 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17910 | |
| 17911 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17912 | ASSERT_TRUE(response); |
| 17913 | ASSERT_TRUE(response->headers); |
bnc | 84e7fb5 | 2015-12-02 11:50:02 | [diff] [blame] | 17914 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17915 | EXPECT_TRUE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17916 | EXPECT_TRUE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17917 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17918 | EXPECT_EQ("hello!", response_data); |
| 17919 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17920 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17921 | EXPECT_TRUE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17922 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17923 | |
| 17924 | HostPortPair host_port_pair_a1("www.a.com", 80); |
Matt Menke | 2436b2f | 2018-12-11 18:07:11 | [diff] [blame] | 17925 | SpdySessionKey spdy_session_key_a1( |
| 17926 | host_port_pair_a1, ProxyServer::Direct(), PRIVACY_MODE_DISABLED, |
dalyk | 51ab46b | 2019-10-15 15:14:34 | [diff] [blame] | 17927 | SpdySessionKey::IsProxySession::kFalse, SocketTag(), |
| 17928 | NetworkIsolationKey(), false /* disable_secure_dns */); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17929 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17930 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a1)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17931 | HttpRequestInfo request3; |
| 17932 | request3.method = "GET"; |
| 17933 | request3.url = GURL("http://www.a.com/"); |
| 17934 | request3.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17935 | request3.traffic_annotation = |
| 17936 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 17937 | trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 17938 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17939 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17940 | rv = trans->Start(&request3, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17941 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 17942 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17943 | |
| 17944 | response = trans->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 17945 | ASSERT_TRUE(response); |
| 17946 | ASSERT_TRUE(response->headers); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17947 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 17948 | EXPECT_FALSE(response->was_fetched_via_spdy); |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 17949 | EXPECT_FALSE(response->was_alpn_negotiated); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17950 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17951 | EXPECT_EQ("hello!", response_data); |
| 17952 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17953 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_a)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17954 | EXPECT_FALSE( |
[email protected] | 41d64e8 | 2013-07-03 22:44:26 | [diff] [blame] | 17955 | HasSpdySession(session->spdy_session_pool(), spdy_session_key_b)); |
[email protected] | 483fa20 | 2013-05-14 01:07:03 | [diff] [blame] | 17956 | } |
| 17957 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17958 | TEST_F(HttpNetworkTransactionTest, HttpSyncConnectError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17959 | HttpRequestInfo request; |
| 17960 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17961 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17962 | request.traffic_annotation = |
| 17963 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17964 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17965 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17966 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17967 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17968 | MockConnect mock_connect(SYNCHRONOUS, ERR_NAME_NOT_RESOLVED); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17969 | StaticSocketDataProvider data; |
| 17970 | data.set_connect_data(mock_connect); |
| 17971 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 17972 | |
| 17973 | TestCompletionCallback callback; |
| 17974 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 17975 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17976 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17977 | |
| 17978 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17979 | EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17980 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17981 | ConnectionAttempts attempts; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17982 | trans.GetConnectionAttempts(&attempts); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 17983 | ASSERT_EQ(1u, attempts.size()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 17984 | EXPECT_THAT(attempts[0].result, IsError(ERR_NAME_NOT_RESOLVED)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17985 | |
| 17986 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17987 | EXPECT_FALSE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 17988 | EXPECT_TRUE(endpoint.address().empty()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17989 | } |
| 17990 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 17991 | TEST_F(HttpNetworkTransactionTest, HttpAsyncConnectError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17992 | HttpRequestInfo request; |
| 17993 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 17994 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 17995 | request.traffic_annotation = |
| 17996 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 17997 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 17998 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 17999 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18000 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 18001 | MockConnect mock_connect(ASYNC, ERR_NAME_NOT_RESOLVED); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18002 | StaticSocketDataProvider data; |
| 18003 | data.set_connect_data(mock_connect); |
| 18004 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18005 | |
| 18006 | TestCompletionCallback callback; |
| 18007 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18008 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18009 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18010 | |
| 18011 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18012 | EXPECT_THAT(rv, IsError(ERR_NAME_NOT_RESOLVED)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18013 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 18014 | ConnectionAttempts attempts; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18015 | trans.GetConnectionAttempts(&attempts); |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 18016 | ASSERT_EQ(1u, attempts.size()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18017 | EXPECT_THAT(attempts[0].result, IsError(ERR_NAME_NOT_RESOLVED)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 18018 | |
| 18019 | IPEndPoint endpoint; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18020 | EXPECT_FALSE(trans.GetRemoteEndpoint(&endpoint)); |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 18021 | EXPECT_TRUE(endpoint.address().empty()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18022 | } |
| 18023 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18024 | TEST_F(HttpNetworkTransactionTest, HttpSyncWriteError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18025 | HttpRequestInfo request; |
| 18026 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18027 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18028 | request.traffic_annotation = |
| 18029 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18030 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18031 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18032 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18033 | |
| 18034 | MockWrite data_writes[] = { |
| 18035 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18036 | }; |
| 18037 | MockRead data_reads[] = { |
| 18038 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. |
| 18039 | }; |
| 18040 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18041 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18042 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18043 | |
| 18044 | TestCompletionCallback callback; |
| 18045 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18046 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18047 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18048 | |
| 18049 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18050 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18051 | } |
| 18052 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18053 | TEST_F(HttpNetworkTransactionTest, HttpAsyncWriteError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18054 | HttpRequestInfo request; |
| 18055 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18056 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18057 | request.traffic_annotation = |
| 18058 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18059 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18060 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18061 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18062 | |
| 18063 | MockWrite data_writes[] = { |
| 18064 | MockWrite(ASYNC, ERR_CONNECTION_RESET), |
| 18065 | }; |
| 18066 | MockRead data_reads[] = { |
| 18067 | MockRead(SYNCHRONOUS, ERR_UNEXPECTED), // Should not be reached. |
| 18068 | }; |
| 18069 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18070 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18071 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18072 | |
| 18073 | TestCompletionCallback callback; |
| 18074 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18075 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18076 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18077 | |
| 18078 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18079 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18080 | } |
| 18081 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18082 | TEST_F(HttpNetworkTransactionTest, HttpSyncReadError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18083 | HttpRequestInfo request; |
| 18084 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18085 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18086 | request.traffic_annotation = |
| 18087 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18088 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18089 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18090 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18091 | |
| 18092 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18093 | MockWrite( |
| 18094 | "GET / HTTP/1.1\r\n" |
| 18095 | "Host: www.example.org\r\n" |
| 18096 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18097 | }; |
| 18098 | MockRead data_reads[] = { |
| 18099 | MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18100 | }; |
| 18101 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18102 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18103 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18104 | |
| 18105 | TestCompletionCallback callback; |
| 18106 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18107 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18108 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18109 | |
| 18110 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18111 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18112 | } |
| 18113 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18114 | TEST_F(HttpNetworkTransactionTest, HttpAsyncReadError) { |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18115 | HttpRequestInfo request; |
| 18116 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18117 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18118 | request.traffic_annotation = |
| 18119 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18120 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18121 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18122 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18123 | |
| 18124 | MockWrite data_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18125 | MockWrite( |
| 18126 | "GET / HTTP/1.1\r\n" |
| 18127 | "Host: www.example.org\r\n" |
| 18128 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18129 | }; |
| 18130 | MockRead data_reads[] = { |
| 18131 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
| 18132 | }; |
| 18133 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18134 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18135 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18136 | |
| 18137 | TestCompletionCallback callback; |
| 18138 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18139 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18140 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18141 | |
| 18142 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18143 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 18144 | } |
| 18145 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18146 | // Tests that when a used socket is returned to the SSL socket pool, it's closed |
| 18147 | // if the transport socket pool is stalled on the global socket limit. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18148 | TEST_F(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18149 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 18150 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18151 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 18152 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18153 | |
| 18154 | // Set up SSL request. |
| 18155 | |
| 18156 | HttpRequestInfo ssl_request; |
| 18157 | ssl_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18158 | ssl_request.url = GURL("https://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18159 | ssl_request.traffic_annotation = |
| 18160 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18161 | |
| 18162 | MockWrite ssl_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18163 | MockWrite( |
| 18164 | "GET / HTTP/1.1\r\n" |
| 18165 | "Host: www.example.org\r\n" |
| 18166 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18167 | }; |
| 18168 | MockRead ssl_reads[] = { |
| 18169 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18170 | MockRead("Content-Length: 11\r\n\r\n"), |
| 18171 | MockRead("hello world"), |
| 18172 | MockRead(SYNCHRONOUS, OK), |
| 18173 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18174 | StaticSocketDataProvider ssl_data(ssl_reads, ssl_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18175 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 18176 | |
| 18177 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18178 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18179 | |
| 18180 | // Set up HTTP request. |
| 18181 | |
| 18182 | HttpRequestInfo http_request; |
| 18183 | http_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18184 | http_request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18185 | http_request.traffic_annotation = |
| 18186 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18187 | |
| 18188 | MockWrite http_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18189 | MockWrite( |
| 18190 | "GET / HTTP/1.1\r\n" |
| 18191 | "Host: www.example.org\r\n" |
| 18192 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18193 | }; |
| 18194 | MockRead http_reads[] = { |
| 18195 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18196 | MockRead("Content-Length: 7\r\n\r\n"), |
| 18197 | MockRead("falafel"), |
| 18198 | MockRead(SYNCHRONOUS, OK), |
| 18199 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18200 | StaticSocketDataProvider http_data(http_reads, http_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18201 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 18202 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18203 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18204 | |
| 18205 | // Start the SSL request. |
| 18206 | TestCompletionCallback ssl_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18207 | HttpNetworkTransaction ssl_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18208 | ASSERT_EQ(ERR_IO_PENDING, |
| 18209 | ssl_trans.Start(&ssl_request, ssl_callback.callback(), |
| 18210 | NetLogWithSource())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18211 | |
| 18212 | // Start the HTTP request. Pool should stall. |
| 18213 | TestCompletionCallback http_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18214 | HttpNetworkTransaction http_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18215 | ASSERT_EQ(ERR_IO_PENDING, |
| 18216 | http_trans.Start(&http_request, http_callback.callback(), |
| 18217 | NetLogWithSource())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18218 | EXPECT_TRUE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18219 | |
| 18220 | // Wait for response from SSL request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18221 | ASSERT_THAT(ssl_callback.WaitForResult(), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18222 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18223 | ASSERT_THAT(ReadTransaction(&ssl_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18224 | EXPECT_EQ("hello world", response_data); |
| 18225 | |
| 18226 | // The SSL socket should automatically be closed, so the HTTP request can |
| 18227 | // start. |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 18228 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18229 | ASSERT_FALSE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18230 | |
| 18231 | // The HTTP request can now complete. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18232 | ASSERT_THAT(http_callback.WaitForResult(), IsOk()); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18233 | ASSERT_THAT(ReadTransaction(&http_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18234 | EXPECT_EQ("falafel", response_data); |
| 18235 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18236 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18237 | } |
| 18238 | |
| 18239 | // Tests that when a SSL connection is established but there's no corresponding |
| 18240 | // request that needs it, the new socket is closed if the transport socket pool |
| 18241 | // is stalled on the global socket limit. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18242 | TEST_F(HttpNetworkTransactionTest, CloseSSLSocketOnIdleForHttpRequest2) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18243 | ClientSocketPoolManager::set_max_sockets_per_group( |
| 18244 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18245 | ClientSocketPoolManager::set_max_sockets_per_pool( |
| 18246 | HttpNetworkSession::NORMAL_SOCKET_POOL, 1); |
| 18247 | |
| 18248 | // Set up an ssl request. |
| 18249 | |
| 18250 | HttpRequestInfo ssl_request; |
| 18251 | ssl_request.method = "GET"; |
| 18252 | ssl_request.url = GURL("https://www.foopy.com/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18253 | ssl_request.traffic_annotation = |
| 18254 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18255 | |
| 18256 | // No data will be sent on the SSL socket. |
| 18257 | StaticSocketDataProvider ssl_data; |
| 18258 | session_deps_.socket_factory->AddSocketDataProvider(&ssl_data); |
| 18259 | |
| 18260 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18261 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18262 | |
| 18263 | // Set up HTTP request. |
| 18264 | |
| 18265 | HttpRequestInfo http_request; |
| 18266 | http_request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18267 | http_request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18268 | http_request.traffic_annotation = |
| 18269 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18270 | |
| 18271 | MockWrite http_writes[] = { |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18272 | MockWrite( |
| 18273 | "GET / HTTP/1.1\r\n" |
| 18274 | "Host: www.example.org\r\n" |
| 18275 | "Connection: keep-alive\r\n\r\n"), |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18276 | }; |
| 18277 | MockRead http_reads[] = { |
| 18278 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 18279 | MockRead("Content-Length: 7\r\n\r\n"), |
| 18280 | MockRead("falafel"), |
| 18281 | MockRead(SYNCHRONOUS, OK), |
| 18282 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18283 | StaticSocketDataProvider http_data(http_reads, http_writes); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18284 | session_deps_.socket_factory->AddSocketDataProvider(&http_data); |
| 18285 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18286 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18287 | |
| 18288 | // Preconnect an SSL socket. A preconnect is needed because connect jobs are |
| 18289 | // cancelled when a normal transaction is cancelled. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 18290 | HttpStreamFactory* http_stream_factory = session->http_stream_factory(); |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 18291 | http_stream_factory->PreconnectStreams(1, ssl_request); |
Matt Menke | 9d5e2c9 | 2019-02-05 01:42:23 | [diff] [blame] | 18292 | EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18293 | |
| 18294 | // Start the HTTP request. Pool should stall. |
| 18295 | TestCompletionCallback http_callback; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18296 | HttpNetworkTransaction http_trans(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18297 | ASSERT_EQ(ERR_IO_PENDING, |
| 18298 | http_trans.Start(&http_request, http_callback.callback(), |
| 18299 | NetLogWithSource())); |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18300 | EXPECT_TRUE(IsTransportSocketPoolStalled(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18301 | |
| 18302 | // The SSL connection will automatically be closed once the connection is |
| 18303 | // established, to let the HTTP request start. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18304 | ASSERT_THAT(http_callback.WaitForResult(), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18305 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18306 | ASSERT_THAT(ReadTransaction(&http_trans, &response_data), IsOk()); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18307 | EXPECT_EQ("falafel", response_data); |
| 18308 | |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 18309 | EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session.get())); |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 18310 | } |
| 18311 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18312 | TEST_F(HttpNetworkTransactionTest, PostReadsErrorResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18313 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18314 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18315 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18316 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18317 | |
| 18318 | HttpRequestInfo request; |
| 18319 | request.method = "POST"; |
| 18320 | request.url = GURL("http://www.foo.com/"); |
| 18321 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18322 | request.traffic_annotation = |
| 18323 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18324 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18325 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18326 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18327 | // Send headers successfully, but get an error while sending the body. |
| 18328 | MockWrite data_writes[] = { |
| 18329 | MockWrite("POST / HTTP/1.1\r\n" |
| 18330 | "Host: www.foo.com\r\n" |
| 18331 | "Connection: keep-alive\r\n" |
| 18332 | "Content-Length: 3\r\n\r\n"), |
| 18333 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18334 | }; |
| 18335 | |
| 18336 | MockRead data_reads[] = { |
| 18337 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18338 | MockRead("hello world"), |
| 18339 | MockRead(SYNCHRONOUS, OK), |
| 18340 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18341 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18342 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18343 | |
| 18344 | TestCompletionCallback callback; |
| 18345 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18346 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18347 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18348 | |
| 18349 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18350 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18351 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18352 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18353 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18354 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18355 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18356 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18357 | |
| 18358 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18359 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18360 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18361 | EXPECT_EQ("hello world", response_data); |
| 18362 | } |
| 18363 | |
| 18364 | // This test makes sure the retry logic doesn't trigger when reading an error |
| 18365 | // response from a server that rejected a POST with a CONNECTION_RESET. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18366 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18367 | PostReadsErrorResponseAfterResetOnReusedSocket) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18368 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18369 | MockWrite data_writes[] = { |
| 18370 | MockWrite("GET / HTTP/1.1\r\n" |
| 18371 | "Host: www.foo.com\r\n" |
| 18372 | "Connection: keep-alive\r\n\r\n"), |
| 18373 | MockWrite("POST / HTTP/1.1\r\n" |
| 18374 | "Host: www.foo.com\r\n" |
| 18375 | "Connection: keep-alive\r\n" |
| 18376 | "Content-Length: 3\r\n\r\n"), |
| 18377 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18378 | }; |
| 18379 | |
| 18380 | MockRead data_reads[] = { |
| 18381 | MockRead("HTTP/1.1 200 Peachy\r\n" |
| 18382 | "Content-Length: 14\r\n\r\n"), |
| 18383 | MockRead("first response"), |
| 18384 | MockRead("HTTP/1.1 400 Not OK\r\n" |
| 18385 | "Content-Length: 15\r\n\r\n"), |
| 18386 | MockRead("second response"), |
| 18387 | MockRead(SYNCHRONOUS, OK), |
| 18388 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18389 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18390 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18391 | |
| 18392 | TestCompletionCallback callback; |
| 18393 | HttpRequestInfo request1; |
| 18394 | request1.method = "GET"; |
| 18395 | request1.url = GURL("http://www.foo.com/"); |
| 18396 | request1.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18397 | request1.traffic_annotation = |
| 18398 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18399 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 18400 | auto trans1 = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18401 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18402 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18403 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18404 | |
| 18405 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18406 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18407 | |
| 18408 | const HttpResponseInfo* response1 = trans1->GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18409 | ASSERT_TRUE(response1); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18410 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18411 | EXPECT_TRUE(response1->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18412 | EXPECT_EQ("HTTP/1.1 200 Peachy", response1->headers->GetStatusLine()); |
| 18413 | |
| 18414 | std::string response_data1; |
| 18415 | rv = ReadTransaction(trans1.get(), &response_data1); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18416 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18417 | EXPECT_EQ("first response", response_data1); |
| 18418 | // Delete the transaction to release the socket back into the socket pool. |
| 18419 | trans1.reset(); |
| 18420 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18421 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18422 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18423 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18424 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18425 | |
| 18426 | HttpRequestInfo request2; |
| 18427 | request2.method = "POST"; |
| 18428 | request2.url = GURL("http://www.foo.com/"); |
| 18429 | request2.upload_data_stream = &upload_data_stream; |
| 18430 | request2.load_flags = 0; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18431 | request2.traffic_annotation = |
| 18432 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18433 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18434 | HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18435 | rv = trans2.Start(&request2, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18436 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18437 | |
| 18438 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18439 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18440 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18441 | const HttpResponseInfo* response2 = trans2.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18442 | ASSERT_TRUE(response2); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18443 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18444 | EXPECT_TRUE(response2->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18445 | EXPECT_EQ("HTTP/1.1 400 Not OK", response2->headers->GetStatusLine()); |
| 18446 | |
| 18447 | std::string response_data2; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18448 | rv = ReadTransaction(&trans2, &response_data2); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18449 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18450 | EXPECT_EQ("second response", response_data2); |
| 18451 | } |
| 18452 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18453 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18454 | PostReadsErrorResponseAfterResetPartialBodySent) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18455 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18456 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18457 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18458 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18459 | |
| 18460 | HttpRequestInfo request; |
| 18461 | request.method = "POST"; |
| 18462 | request.url = GURL("http://www.foo.com/"); |
| 18463 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18464 | request.traffic_annotation = |
| 18465 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18466 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18467 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18468 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18469 | // Send headers successfully, but get an error while sending the body. |
| 18470 | MockWrite data_writes[] = { |
| 18471 | MockWrite("POST / HTTP/1.1\r\n" |
| 18472 | "Host: www.foo.com\r\n" |
| 18473 | "Connection: keep-alive\r\n" |
| 18474 | "Content-Length: 3\r\n\r\n" |
| 18475 | "fo"), |
| 18476 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18477 | }; |
| 18478 | |
| 18479 | MockRead data_reads[] = { |
| 18480 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18481 | MockRead("hello world"), |
| 18482 | MockRead(SYNCHRONOUS, OK), |
| 18483 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18484 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18485 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18486 | |
| 18487 | TestCompletionCallback callback; |
| 18488 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18489 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18490 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18491 | |
| 18492 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18493 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18494 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18495 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18496 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18497 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18498 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18499 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18500 | |
| 18501 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18502 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18503 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18504 | EXPECT_EQ("hello world", response_data); |
| 18505 | } |
| 18506 | |
| 18507 | // This tests the more common case than the previous test, where headers and |
| 18508 | // body are not merged into a single request. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18509 | TEST_F(HttpNetworkTransactionTest, ChunkedPostReadsErrorResponseAfterReset) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 18510 | ChunkedUploadDataStream upload_data_stream(0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18511 | |
| 18512 | HttpRequestInfo request; |
| 18513 | request.method = "POST"; |
| 18514 | request.url = GURL("http://www.foo.com/"); |
| 18515 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18516 | request.traffic_annotation = |
| 18517 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18518 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18519 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18520 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18521 | // Send headers successfully, but get an error while sending the body. |
| 18522 | MockWrite data_writes[] = { |
| 18523 | MockWrite("POST / HTTP/1.1\r\n" |
| 18524 | "Host: www.foo.com\r\n" |
| 18525 | "Connection: keep-alive\r\n" |
| 18526 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 18527 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18528 | }; |
| 18529 | |
| 18530 | MockRead data_reads[] = { |
| 18531 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18532 | MockRead("hello world"), |
| 18533 | MockRead(SYNCHRONOUS, OK), |
| 18534 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18535 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18536 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18537 | |
| 18538 | TestCompletionCallback callback; |
| 18539 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18540 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18541 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18542 | // Make sure the headers are sent before adding a chunk. This ensures that |
| 18543 | // they can't be merged with the body in a single send. Not currently |
| 18544 | // necessary since a chunked body is never merged with headers, but this makes |
| 18545 | // the test more future proof. |
| 18546 | base::RunLoop().RunUntilIdle(); |
| 18547 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 18548 | upload_data_stream.AppendData("last chunk", 10, true); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18549 | |
| 18550 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18551 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18552 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18553 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18554 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18555 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18556 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18557 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18558 | |
| 18559 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18560 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18561 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18562 | EXPECT_EQ("hello world", response_data); |
| 18563 | } |
| 18564 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18565 | TEST_F(HttpNetworkTransactionTest, PostReadsErrorResponseAfterResetAnd100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18566 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18567 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18568 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18569 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18570 | |
| 18571 | HttpRequestInfo request; |
| 18572 | request.method = "POST"; |
| 18573 | request.url = GURL("http://www.foo.com/"); |
| 18574 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18575 | request.traffic_annotation = |
| 18576 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18577 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18578 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18579 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18580 | |
| 18581 | MockWrite data_writes[] = { |
| 18582 | MockWrite("POST / HTTP/1.1\r\n" |
| 18583 | "Host: www.foo.com\r\n" |
| 18584 | "Connection: keep-alive\r\n" |
| 18585 | "Content-Length: 3\r\n\r\n"), |
| 18586 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18587 | }; |
| 18588 | |
| 18589 | MockRead data_reads[] = { |
| 18590 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 18591 | MockRead("HTTP/1.0 400 Not OK\r\n\r\n"), |
| 18592 | MockRead("hello world"), |
| 18593 | MockRead(SYNCHRONOUS, OK), |
| 18594 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18595 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18596 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18597 | |
| 18598 | TestCompletionCallback callback; |
| 18599 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18600 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18601 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18602 | |
| 18603 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18604 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18605 | |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18606 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18607 | ASSERT_TRUE(response); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18608 | |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18609 | EXPECT_TRUE(response->headers); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18610 | EXPECT_EQ("HTTP/1.0 400 Not OK", response->headers->GetStatusLine()); |
| 18611 | |
| 18612 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18613 | rv = ReadTransaction(&trans, &response_data); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18614 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18615 | EXPECT_EQ("hello world", response_data); |
| 18616 | } |
| 18617 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18618 | TEST_F(HttpNetworkTransactionTest, PostIgnoresNonErrorResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18619 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18620 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18621 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18622 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18623 | |
| 18624 | HttpRequestInfo request; |
| 18625 | request.method = "POST"; |
| 18626 | request.url = GURL("http://www.foo.com/"); |
| 18627 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18628 | request.traffic_annotation = |
| 18629 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18630 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18631 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18632 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18633 | // Send headers successfully, but get an error while sending the body. |
| 18634 | MockWrite data_writes[] = { |
| 18635 | MockWrite("POST / HTTP/1.1\r\n" |
| 18636 | "Host: www.foo.com\r\n" |
| 18637 | "Connection: keep-alive\r\n" |
| 18638 | "Content-Length: 3\r\n\r\n"), |
| 18639 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18640 | }; |
| 18641 | |
| 18642 | MockRead data_reads[] = { |
| 18643 | MockRead("HTTP/1.0 200 Just Dandy\r\n\r\n"), |
| 18644 | MockRead("hello world"), |
| 18645 | MockRead(SYNCHRONOUS, OK), |
| 18646 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18647 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18648 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18649 | |
| 18650 | TestCompletionCallback callback; |
| 18651 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18652 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18653 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18654 | |
| 18655 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18656 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18657 | } |
| 18658 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18659 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18660 | PostIgnoresNonErrorResponseAfterResetAnd100) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18661 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18662 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18663 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18664 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18665 | |
| 18666 | HttpRequestInfo request; |
| 18667 | request.method = "POST"; |
| 18668 | request.url = GURL("http://www.foo.com/"); |
| 18669 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18670 | request.traffic_annotation = |
| 18671 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18672 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18673 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18674 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18675 | // Send headers successfully, but get an error while sending the body. |
| 18676 | MockWrite data_writes[] = { |
| 18677 | MockWrite("POST / HTTP/1.1\r\n" |
| 18678 | "Host: www.foo.com\r\n" |
| 18679 | "Connection: keep-alive\r\n" |
| 18680 | "Content-Length: 3\r\n\r\n"), |
| 18681 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18682 | }; |
| 18683 | |
| 18684 | MockRead data_reads[] = { |
| 18685 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 18686 | MockRead("HTTP/1.0 302 Redirect\r\n"), |
| 18687 | MockRead("Location: http://somewhere-else.com/\r\n"), |
| 18688 | MockRead("Content-Length: 0\r\n\r\n"), |
| 18689 | MockRead(SYNCHRONOUS, OK), |
| 18690 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18691 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18692 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18693 | |
| 18694 | TestCompletionCallback callback; |
| 18695 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18696 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18697 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18698 | |
| 18699 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18700 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18701 | } |
| 18702 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18703 | TEST_F(HttpNetworkTransactionTest, PostIgnoresHttp09ResponseAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18704 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18705 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18706 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18707 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18708 | |
| 18709 | HttpRequestInfo request; |
| 18710 | request.method = "POST"; |
| 18711 | request.url = GURL("http://www.foo.com/"); |
| 18712 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18713 | request.traffic_annotation = |
| 18714 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18715 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18716 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18717 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18718 | // Send headers successfully, but get an error while sending the body. |
| 18719 | MockWrite data_writes[] = { |
| 18720 | MockWrite("POST / HTTP/1.1\r\n" |
| 18721 | "Host: www.foo.com\r\n" |
| 18722 | "Connection: keep-alive\r\n" |
| 18723 | "Content-Length: 3\r\n\r\n"), |
| 18724 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18725 | }; |
| 18726 | |
| 18727 | MockRead data_reads[] = { |
| 18728 | MockRead("HTTP 0.9 rocks!"), |
| 18729 | MockRead(SYNCHRONOUS, OK), |
| 18730 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18731 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18732 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18733 | |
| 18734 | TestCompletionCallback callback; |
| 18735 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18736 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18737 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18738 | |
| 18739 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18740 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18741 | } |
| 18742 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18743 | TEST_F(HttpNetworkTransactionTest, PostIgnoresPartial400HeadersAfterReset) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18744 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18745 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18746 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 18747 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18748 | |
| 18749 | HttpRequestInfo request; |
| 18750 | request.method = "POST"; |
| 18751 | request.url = GURL("http://www.foo.com/"); |
| 18752 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18753 | request.traffic_annotation = |
| 18754 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18755 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18756 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 18757 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18758 | // Send headers successfully, but get an error while sending the body. |
| 18759 | MockWrite data_writes[] = { |
| 18760 | MockWrite("POST / HTTP/1.1\r\n" |
| 18761 | "Host: www.foo.com\r\n" |
| 18762 | "Connection: keep-alive\r\n" |
| 18763 | "Content-Length: 3\r\n\r\n"), |
| 18764 | MockWrite(SYNCHRONOUS, ERR_CONNECTION_RESET), |
| 18765 | }; |
| 18766 | |
| 18767 | MockRead data_reads[] = { |
| 18768 | MockRead("HTTP/1.0 400 Not a Full Response\r\n"), |
| 18769 | MockRead(SYNCHRONOUS, OK), |
| 18770 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18771 | StaticSocketDataProvider data(data_reads, data_writes); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18772 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18773 | |
| 18774 | TestCompletionCallback callback; |
| 18775 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18776 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18777 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18778 | |
| 18779 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18780 | EXPECT_THAT(rv, IsError(ERR_CONNECTION_RESET)); |
[email protected] | 02d74a0 | 2014-04-23 18:10:54 | [diff] [blame] | 18781 | } |
| 18782 | |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18783 | #if BUILDFLAG(ENABLE_WEBSOCKETS) |
| 18784 | |
| 18785 | namespace { |
| 18786 | |
| 18787 | void AddWebSocketHeaders(HttpRequestHeaders* headers) { |
| 18788 | headers->SetHeader("Connection", "Upgrade"); |
| 18789 | headers->SetHeader("Upgrade", "websocket"); |
| 18790 | headers->SetHeader("Origin", "http://www.example.org"); |
| 18791 | headers->SetHeader("Sec-WebSocket-Version", "13"); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18792 | } |
| 18793 | |
| 18794 | } // namespace |
| 18795 | |
| 18796 | TEST_F(HttpNetworkTransactionTest, CreateWebSocketHandshakeStream) { |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18797 | for (bool secure : {true, false}) { |
| 18798 | MockWrite data_writes[] = { |
| 18799 | MockWrite("GET / HTTP/1.1\r\n" |
| 18800 | "Host: www.example.org\r\n" |
| 18801 | "Connection: Upgrade\r\n" |
| 18802 | "Upgrade: websocket\r\n" |
| 18803 | "Origin: http://www.example.org\r\n" |
| 18804 | "Sec-WebSocket-Version: 13\r\n" |
| 18805 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18806 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18807 | "client_max_window_bits\r\n\r\n")}; |
| 18808 | |
| 18809 | MockRead data_reads[] = { |
| 18810 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 18811 | "Upgrade: websocket\r\n" |
| 18812 | "Connection: Upgrade\r\n" |
| 18813 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
| 18814 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18815 | StaticSocketDataProvider data(data_reads, data_writes); |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18816 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18817 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18818 | if (secure) |
| 18819 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18820 | |
| 18821 | HttpRequestInfo request; |
| 18822 | request.method = "GET"; |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18823 | request.url = |
| 18824 | GURL(secure ? "ws://www.example.org/" : "wss://www.example.org/"); |
| 18825 | AddWebSocketHeaders(&request.extra_headers); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18826 | request.traffic_annotation = |
| 18827 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18828 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18829 | TestWebSocketHandshakeStreamCreateHelper |
| 18830 | websocket_handshake_stream_create_helper; |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18831 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18832 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18833 | HttpNetworkTransaction trans(LOW, session.get()); |
| 18834 | trans.SetWebSocketHandshakeStreamCreateHelper( |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18835 | &websocket_handshake_stream_create_helper); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18836 | |
| 18837 | TestCompletionCallback callback; |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18838 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 18839 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18840 | |
Bence Béky | 2fcf4fa | 2018-04-06 20:06:01 | [diff] [blame] | 18841 | const HttpStreamRequest* stream_request = trans.stream_request_.get(); |
| 18842 | ASSERT_TRUE(stream_request); |
| 18843 | EXPECT_EQ(&websocket_handshake_stream_create_helper, |
| 18844 | stream_request->websocket_handshake_stream_create_helper()); |
| 18845 | |
| 18846 | rv = callback.WaitForResult(); |
| 18847 | EXPECT_THAT(rv, IsOk()); |
| 18848 | |
| 18849 | EXPECT_TRUE(data.AllReadDataConsumed()); |
| 18850 | EXPECT_TRUE(data.AllWriteDataConsumed()); |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 18851 | } |
| 18852 | } |
| 18853 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18854 | // Verify that proxy headers are not sent to the destination server when |
| 18855 | // establishing a tunnel for a secure WebSocket connection. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18856 | TEST_F(HttpNetworkTransactionTest, ProxyHeadersNotSentOverWssTunnel) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18857 | HttpRequestInfo request; |
| 18858 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18859 | request.url = GURL("wss://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18860 | request.traffic_annotation = |
| 18861 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18862 | AddWebSocketHeaders(&request.extra_headers); |
| 18863 | |
| 18864 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 18865 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 18866 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 18867 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18868 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18869 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18870 | |
| 18871 | // Since a proxy is configured, try to establish a tunnel. |
| 18872 | MockWrite data_writes[] = { |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18873 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 18874 | "Host: www.example.org:443\r\n" |
| 18875 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18876 | |
| 18877 | // After calling trans->RestartWithAuth(), this is the request we should |
| 18878 | // be issuing -- the final header line contains the credentials. |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18879 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 18880 | "Host: www.example.org:443\r\n" |
| 18881 | "Proxy-Connection: keep-alive\r\n" |
| 18882 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18883 | |
rsleevi | db16bb0 | 2015-11-12 23:47:17 | [diff] [blame] | 18884 | MockWrite("GET / HTTP/1.1\r\n" |
| 18885 | "Host: www.example.org\r\n" |
| 18886 | "Connection: Upgrade\r\n" |
| 18887 | "Upgrade: websocket\r\n" |
| 18888 | "Origin: http://www.example.org\r\n" |
| 18889 | "Sec-WebSocket-Version: 13\r\n" |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18890 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18891 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18892 | "client_max_window_bits\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18893 | |
| 18894 | // The proxy responds to the connect with a 407, using a persistent |
| 18895 | // connection. |
| 18896 | MockRead data_reads[] = { |
| 18897 | // No credentials. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18898 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 18899 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 18900 | "Content-Length: 0\r\n" |
| 18901 | "Proxy-Connection: keep-alive\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18902 | |
| 18903 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 18904 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18905 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 18906 | "Upgrade: websocket\r\n" |
| 18907 | "Connection: Upgrade\r\n" |
| 18908 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18909 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 18910 | StaticSocketDataProvider data(data_reads, data_writes); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18911 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 18912 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 18913 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 18914 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18915 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 18916 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 18917 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 18918 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18919 | trans->SetWebSocketHandshakeStreamCreateHelper( |
| 18920 | &websocket_stream_create_helper); |
| 18921 | |
| 18922 | { |
| 18923 | TestCompletionCallback callback; |
| 18924 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 18925 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18926 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18927 | |
| 18928 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18929 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18930 | } |
| 18931 | |
| 18932 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 18933 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18934 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18935 | EXPECT_EQ(407, response->headers->response_code()); |
| 18936 | |
| 18937 | { |
| 18938 | TestCompletionCallback callback; |
| 18939 | |
| 18940 | int rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), |
| 18941 | callback.callback()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18942 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18943 | |
| 18944 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 18945 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18946 | } |
| 18947 | |
| 18948 | response = trans->GetResponseInfo(); |
| 18949 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 18950 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18951 | |
| 18952 | EXPECT_EQ(101, response->headers->response_code()); |
| 18953 | |
| 18954 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 18955 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18956 | } |
| 18957 | |
| 18958 | // Verify that proxy headers are not sent to the destination server when |
| 18959 | // establishing a tunnel for an insecure WebSocket connection. |
| 18960 | // This requires the authentication info to be injected into the auth cache |
| 18961 | // due to crbug.com/395064 |
| 18962 | // TODO(ricea): Change to use a 407 response once issue 395064 is fixed. |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 18963 | TEST_F(HttpNetworkTransactionTest, ProxyHeadersNotSentOverWsTunnel) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18964 | HttpRequestInfo request; |
| 18965 | request.method = "GET"; |
bnc | ce36dca2 | 2015-04-21 22:11:23 | [diff] [blame] | 18966 | request.url = GURL("ws://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 18967 | request.traffic_annotation = |
| 18968 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18969 | AddWebSocketHeaders(&request.extra_headers); |
| 18970 | |
| 18971 | // Configure against proxy server "myproxy:70". |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 18972 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 18973 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 18974 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18975 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 18976 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18977 | |
| 18978 | MockWrite data_writes[] = { |
| 18979 | // Try to establish a tunnel for the WebSocket connection, with |
| 18980 | // credentials. Because WebSockets have a separate set of socket pools, |
| 18981 | // they cannot and will not use the same TCP/IP connection as the |
| 18982 | // preflight HTTP request. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18983 | MockWrite("CONNECT www.example.org:80 HTTP/1.1\r\n" |
| 18984 | "Host: www.example.org:80\r\n" |
| 18985 | "Proxy-Connection: keep-alive\r\n" |
| 18986 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18987 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 18988 | MockWrite("GET / HTTP/1.1\r\n" |
| 18989 | "Host: www.example.org\r\n" |
| 18990 | "Connection: Upgrade\r\n" |
| 18991 | "Upgrade: websocket\r\n" |
| 18992 | "Origin: http://www.example.org\r\n" |
| 18993 | "Sec-WebSocket-Version: 13\r\n" |
| 18994 | "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" |
| 18995 | "Sec-WebSocket-Extensions: permessage-deflate; " |
| 18996 | "client_max_window_bits\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 18997 | |
| 18998 | MockRead data_reads[] = { |
| 18999 | // HTTP CONNECT with credentials. |
| 19000 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 19001 | |
| 19002 | // WebSocket connection established inside tunnel. |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 19003 | MockRead("HTTP/1.1 101 Switching Protocols\r\n" |
| 19004 | "Upgrade: websocket\r\n" |
| 19005 | "Connection: Upgrade\r\n" |
| 19006 | "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")}; |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19007 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19008 | StaticSocketDataProvider data(data_reads, data_writes); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19009 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19010 | |
| 19011 | session->http_auth_cache()->Add( |
Matt Menke | 96092e6 | 2019-10-18 04:09:33 | [diff] [blame] | 19012 | GURL("http://myproxy:70/"), HttpAuth::AUTH_PROXY, "MyRealm1", |
Matt Menke | be09042 | 2019-10-18 20:25:26 | [diff] [blame] | 19013 | HttpAuth::AUTH_SCHEME_BASIC, NetworkIsolationKey(), |
| 19014 | "Basic realm=MyRealm1", AuthCredentials(kFoo, kBar), "/"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19015 | |
Bence Béky | 8d1c605 | 2018-02-07 12:48:15 | [diff] [blame] | 19016 | TestWebSocketHandshakeStreamCreateHelper websocket_stream_create_helper; |
| 19017 | |
bnc | 87dcefc | 2017-05-25 12:47:58 | [diff] [blame] | 19018 | auto trans = |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 19019 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19020 | trans->SetWebSocketHandshakeStreamCreateHelper( |
| 19021 | &websocket_stream_create_helper); |
| 19022 | |
| 19023 | TestCompletionCallback callback; |
| 19024 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19025 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19026 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19027 | |
| 19028 | rv = callback.WaitForResult(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19029 | EXPECT_THAT(rv, IsOk()); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19030 | |
| 19031 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 19032 | ASSERT_TRUE(response); |
wez | ca107093 | 2016-05-26 20:30:52 | [diff] [blame] | 19033 | ASSERT_TRUE(response->headers); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19034 | |
| 19035 | EXPECT_EQ(101, response->headers->response_code()); |
| 19036 | |
| 19037 | trans.reset(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 19038 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 19039 | } |
| 19040 | |
Matt Menke | 1d6093e3 | 2019-03-22 17:33:43 | [diff] [blame] | 19041 | // WebSockets over QUIC is not supported, including over QUIC proxies. |
| 19042 | TEST_F(HttpNetworkTransactionTest, WebSocketNotSentOverQuicProxy) { |
| 19043 | for (bool secure : {true, false}) { |
| 19044 | SCOPED_TRACE(secure); |
| 19045 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 19046 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Matt Menke | 1d6093e3 | 2019-03-22 17:33:43 | [diff] [blame] | 19047 | "QUIC myproxy.org:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19048 | session_deps_.enable_quic = true; |
| 19049 | |
| 19050 | HttpRequestInfo request; |
| 19051 | request.url = |
| 19052 | GURL(secure ? "ws://www.example.org/" : "wss://www.example.org/"); |
| 19053 | AddWebSocketHeaders(&request.extra_headers); |
| 19054 | request.traffic_annotation = |
| 19055 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19056 | |
| 19057 | TestWebSocketHandshakeStreamCreateHelper |
| 19058 | websocket_handshake_stream_create_helper; |
| 19059 | |
| 19060 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19061 | HttpNetworkTransaction trans(LOW, session.get()); |
| 19062 | trans.SetWebSocketHandshakeStreamCreateHelper( |
| 19063 | &websocket_handshake_stream_create_helper); |
| 19064 | |
| 19065 | TestCompletionCallback callback; |
| 19066 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19067 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19068 | |
| 19069 | rv = callback.WaitForResult(); |
| 19070 | EXPECT_THAT(rv, IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 19071 | } |
| 19072 | } |
| 19073 | |
Bence Béky | dca6bd9 | 2018-01-30 13:43:06 | [diff] [blame] | 19074 | #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
| 19075 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 19076 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesPost) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19077 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19078 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 19079 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19080 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19081 | |
| 19082 | HttpRequestInfo request; |
| 19083 | request.method = "POST"; |
| 19084 | request.url = GURL("http://www.foo.com/"); |
| 19085 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19086 | request.traffic_annotation = |
| 19087 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19088 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19089 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19090 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19091 | MockWrite data_writes[] = { |
| 19092 | MockWrite("POST / HTTP/1.1\r\n" |
| 19093 | "Host: www.foo.com\r\n" |
| 19094 | "Connection: keep-alive\r\n" |
| 19095 | "Content-Length: 3\r\n\r\n"), |
| 19096 | MockWrite("foo"), |
| 19097 | }; |
| 19098 | |
| 19099 | MockRead data_reads[] = { |
| 19100 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19101 | MockRead(SYNCHRONOUS, OK), |
| 19102 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19103 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19104 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19105 | |
| 19106 | TestCompletionCallback callback; |
| 19107 | |
| 19108 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19109 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19110 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19111 | |
| 19112 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19113 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19114 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19115 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19116 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19117 | } |
| 19118 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 19119 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesPost100Continue) { |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19120 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19121 | element_readers.push_back( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 19122 | std::make_unique<UploadBytesElementReader>("foo", 3)); |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 19123 | ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19124 | |
| 19125 | HttpRequestInfo request; |
| 19126 | request.method = "POST"; |
| 19127 | request.url = GURL("http://www.foo.com/"); |
| 19128 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19129 | request.traffic_annotation = |
| 19130 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19131 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19132 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19133 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19134 | MockWrite data_writes[] = { |
| 19135 | MockWrite("POST / HTTP/1.1\r\n" |
| 19136 | "Host: www.foo.com\r\n" |
| 19137 | "Connection: keep-alive\r\n" |
| 19138 | "Content-Length: 3\r\n\r\n"), |
| 19139 | MockWrite("foo"), |
| 19140 | }; |
| 19141 | |
| 19142 | MockRead data_reads[] = { |
| 19143 | MockRead("HTTP/1.1 100 Continue\r\n\r\n"), |
| 19144 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19145 | MockRead(SYNCHRONOUS, OK), |
| 19146 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19147 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19148 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19149 | |
| 19150 | TestCompletionCallback callback; |
| 19151 | |
| 19152 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19153 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19154 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19155 | |
| 19156 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19157 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19158 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19159 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19160 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19161 | } |
| 19162 | |
bnc | d16676a | 2016-07-20 16:23:01 | [diff] [blame] | 19163 | TEST_F(HttpNetworkTransactionTest, TotalNetworkBytesChunkedPost) { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19164 | ChunkedUploadDataStream upload_data_stream(0); |
| 19165 | |
| 19166 | HttpRequestInfo request; |
| 19167 | request.method = "POST"; |
| 19168 | request.url = GURL("http://www.foo.com/"); |
| 19169 | request.upload_data_stream = &upload_data_stream; |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19170 | request.traffic_annotation = |
| 19171 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19172 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 19173 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19174 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19175 | // Send headers successfully, but get an error while sending the body. |
| 19176 | MockWrite data_writes[] = { |
| 19177 | MockWrite("POST / HTTP/1.1\r\n" |
| 19178 | "Host: www.foo.com\r\n" |
| 19179 | "Connection: keep-alive\r\n" |
| 19180 | "Transfer-Encoding: chunked\r\n\r\n"), |
| 19181 | MockWrite("1\r\nf\r\n"), MockWrite("2\r\noo\r\n"), MockWrite("0\r\n\r\n"), |
| 19182 | }; |
| 19183 | |
| 19184 | MockRead data_reads[] = { |
| 19185 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"), |
| 19186 | MockRead(SYNCHRONOUS, OK), |
| 19187 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19188 | StaticSocketDataProvider data(data_reads, data_writes); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19189 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19190 | |
| 19191 | TestCompletionCallback callback; |
| 19192 | |
| 19193 | EXPECT_EQ(ERR_IO_PENDING, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 19194 | trans.Start(&request, callback.callback(), NetLogWithSource())); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19195 | |
| 19196 | base::RunLoop().RunUntilIdle(); |
| 19197 | upload_data_stream.AppendData("f", 1, false); |
| 19198 | |
| 19199 | base::RunLoop().RunUntilIdle(); |
| 19200 | upload_data_stream.AppendData("oo", 2, true); |
| 19201 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 19202 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19203 | |
| 19204 | std::string response_data; |
bnc | 691fda6 | 2016-08-12 00:43:16 | [diff] [blame] | 19205 | EXPECT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19206 | |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19207 | EXPECT_EQ(CountWriteBytes(data_writes), trans.GetTotalSentBytes()); |
| 19208 | EXPECT_EQ(CountReadBytes(data_reads), trans.GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 19209 | } |
| 19210 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19211 | void CheckContentEncodingMatching(SpdySessionDependencies* session_deps, |
| 19212 | const std::string& accept_encoding, |
| 19213 | const std::string& content_encoding, |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19214 | const std::string& location, |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19215 | bool should_match) { |
| 19216 | HttpRequestInfo request; |
| 19217 | request.method = "GET"; |
| 19218 | request.url = GURL("http://www.foo.com/"); |
| 19219 | request.extra_headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 19220 | accept_encoding); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19221 | request.traffic_annotation = |
| 19222 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19223 | |
| 19224 | std::unique_ptr<HttpNetworkSession> session(CreateSession(session_deps)); |
| 19225 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19226 | // Send headers successfully, but get an error while sending the body. |
| 19227 | MockWrite data_writes[] = { |
| 19228 | MockWrite("GET / HTTP/1.1\r\n" |
| 19229 | "Host: www.foo.com\r\n" |
| 19230 | "Connection: keep-alive\r\n" |
| 19231 | "Accept-Encoding: "), |
| 19232 | MockWrite(accept_encoding.data()), MockWrite("\r\n\r\n"), |
| 19233 | }; |
| 19234 | |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19235 | std::string response_code = "200 OK"; |
| 19236 | std::string extra; |
| 19237 | if (!location.empty()) { |
| 19238 | response_code = "301 Redirect\r\nLocation: "; |
| 19239 | response_code.append(location); |
| 19240 | } |
| 19241 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19242 | MockRead data_reads[] = { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19243 | MockRead("HTTP/1.0 "), |
| 19244 | MockRead(response_code.data()), |
| 19245 | MockRead("\r\nContent-Encoding: "), |
| 19246 | MockRead(content_encoding.data()), |
| 19247 | MockRead("\r\n\r\n"), |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19248 | MockRead(SYNCHRONOUS, OK), |
| 19249 | }; |
Ryan Sleevi | b8d7ea0 | 2018-05-07 20:01:01 | [diff] [blame] | 19250 | StaticSocketDataProvider data(data_reads, data_writes); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19251 | session_deps->socket_factory->AddSocketDataProvider(&data); |
| 19252 | |
| 19253 | TestCompletionCallback callback; |
| 19254 | |
| 19255 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19256 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19257 | |
| 19258 | rv = callback.WaitForResult(); |
| 19259 | if (should_match) { |
| 19260 | EXPECT_THAT(rv, IsOk()); |
| 19261 | } else { |
| 19262 | EXPECT_THAT(rv, IsError(ERR_CONTENT_DECODING_FAILED)); |
| 19263 | } |
| 19264 | } |
| 19265 | |
| 19266 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding1) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19267 | CheckContentEncodingMatching(&session_deps_, "gzip,sdch", "br", "", false); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19268 | } |
| 19269 | |
| 19270 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding2) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19271 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "", "", |
| 19272 | true); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19273 | } |
| 19274 | |
| 19275 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding3) { |
| 19276 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "gzip", |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 19277 | "", false); |
| 19278 | } |
| 19279 | |
| 19280 | TEST_F(HttpNetworkTransactionTest, MatchContentEncoding4) { |
| 19281 | CheckContentEncodingMatching(&session_deps_, "identity;q=1, *;q=0", "gzip", |
| 19282 | "www.foo.com/other", true); |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 19283 | } |
| 19284 | |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19285 | TEST_F(HttpNetworkTransactionTest, ProxyResolutionFailsSync) { |
| 19286 | ProxyConfig proxy_config; |
| 19287 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 19288 | proxy_config.set_pac_mandatory(true); |
| 19289 | MockAsyncProxyResolver resolver; |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 19290 | session_deps_.proxy_resolution_service.reset( |
| 19291 | new ConfiguredProxyResolutionService( |
| 19292 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 19293 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 19294 | std::make_unique<FailingProxyResolverFactory>(), nullptr, |
| 19295 | /*quick_check_enabled=*/true)); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19296 | |
| 19297 | HttpRequestInfo request; |
| 19298 | request.method = "GET"; |
| 19299 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19300 | request.traffic_annotation = |
| 19301 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19302 | |
| 19303 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19304 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19305 | |
| 19306 | TestCompletionCallback callback; |
| 19307 | |
| 19308 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19309 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19310 | EXPECT_THAT(callback.WaitForResult(), |
| 19311 | IsError(ERR_MANDATORY_PROXY_CONFIGURATION_FAILED)); |
| 19312 | } |
| 19313 | |
| 19314 | TEST_F(HttpNetworkTransactionTest, ProxyResolutionFailsAsync) { |
| 19315 | ProxyConfig proxy_config; |
| 19316 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
| 19317 | proxy_config.set_pac_mandatory(true); |
| 19318 | MockAsyncProxyResolverFactory* proxy_resolver_factory = |
| 19319 | new MockAsyncProxyResolverFactory(false); |
| 19320 | MockAsyncProxyResolver resolver; |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 19321 | session_deps_.proxy_resolution_service.reset( |
| 19322 | new ConfiguredProxyResolutionService( |
| 19323 | std::make_unique<ProxyConfigServiceFixed>(ProxyConfigWithAnnotation( |
| 19324 | proxy_config, TRAFFIC_ANNOTATION_FOR_TESTS)), |
Eric Roman | 3be01ba | 2020-04-03 21:37:09 | [diff] [blame] | 19325 | base::WrapUnique(proxy_resolver_factory), nullptr, |
| 19326 | /*quick_check_enabled=*/true)); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19327 | HttpRequestInfo request; |
| 19328 | request.method = "GET"; |
| 19329 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19330 | request.traffic_annotation = |
| 19331 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19332 | |
| 19333 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19334 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19335 | |
| 19336 | TestCompletionCallback callback; |
| 19337 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19338 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19339 | |
| 19340 | proxy_resolver_factory->pending_requests()[0]->CompleteNowWithForwarder( |
| 19341 | ERR_FAILED, &resolver); |
| 19342 | EXPECT_THAT(callback.WaitForResult(), |
| 19343 | IsError(ERR_MANDATORY_PROXY_CONFIGURATION_FAILED)); |
| 19344 | } |
| 19345 | |
| 19346 | TEST_F(HttpNetworkTransactionTest, NoSupportedProxies) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 19347 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 19348 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 19349 | "QUIC myproxy.org:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19350 | session_deps_.enable_quic = false; |
| 19351 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19352 | |
| 19353 | HttpRequestInfo request; |
| 19354 | request.method = "GET"; |
| 19355 | request.url = GURL("http://www.example.org/"); |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 19356 | request.traffic_annotation = |
| 19357 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 19358 | |
| 19359 | TestCompletionCallback callback; |
| 19360 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19361 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 19362 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19363 | |
| 19364 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 19365 | } |
| 19366 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19367 | //----------------------------------------------------------------------------- |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19368 | // Reporting tests |
| 19369 | |
| 19370 | #if BUILDFLAG(ENABLE_REPORTING) |
| 19371 | class HttpNetworkTransactionReportingTest : public HttpNetworkTransactionTest { |
| 19372 | protected: |
| 19373 | void SetUp() override { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19374 | HttpNetworkTransactionTest::SetUp(); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19375 | auto test_reporting_context = std::make_unique<TestReportingContext>( |
| 19376 | &clock_, &tick_clock_, ReportingPolicy()); |
| 19377 | test_reporting_context_ = test_reporting_context.get(); |
| 19378 | session_deps_.reporting_service = |
| 19379 | ReportingService::CreateForTesting(std::move(test_reporting_context)); |
| 19380 | } |
| 19381 | |
| 19382 | TestReportingContext* reporting_context() const { |
| 19383 | return test_reporting_context_; |
| 19384 | } |
| 19385 | |
| 19386 | void clear_reporting_service() { |
| 19387 | session_deps_.reporting_service.reset(); |
| 19388 | test_reporting_context_ = nullptr; |
| 19389 | } |
| 19390 | |
| 19391 | // Makes an HTTPS request that should install a valid Reporting policy. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19392 | void RequestPolicy(CertStatus cert_status = 0) { |
| 19393 | HttpRequestInfo request; |
| 19394 | request.method = "GET"; |
| 19395 | request.url = GURL(url_); |
| 19396 | request.traffic_annotation = |
| 19397 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19398 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19399 | MockWrite data_writes[] = { |
| 19400 | MockWrite("GET / HTTP/1.1\r\n" |
| 19401 | "Host: www.example.org\r\n" |
| 19402 | "Connection: keep-alive\r\n\r\n"), |
| 19403 | }; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19404 | MockRead data_reads[] = { |
| 19405 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19406 | MockRead("Report-To: {\"group\": \"nel\", \"max_age\": 86400, " |
| 19407 | "\"endpoints\": [{\"url\": " |
| 19408 | "\"https://www.example.org/upload/\"}]}\r\n"), |
| 19409 | MockRead("\r\n"), |
| 19410 | MockRead("hello world"), |
| 19411 | MockRead(SYNCHRONOUS, OK), |
| 19412 | }; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19413 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19414 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19415 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19416 | |
| 19417 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19418 | if (request.url.SchemeIsCryptographic()) { |
| 19419 | ssl.ssl_info.cert = |
| 19420 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19421 | ASSERT_TRUE(ssl.ssl_info.cert); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19422 | ssl.ssl_info.cert_status = cert_status; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19423 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19424 | } |
| 19425 | |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19426 | TestCompletionCallback callback; |
| 19427 | auto session = CreateSession(&session_deps_); |
| 19428 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19429 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19430 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19431 | } |
| 19432 | |
| 19433 | protected: |
| 19434 | std::string url_ = "https://www.example.org/"; |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19435 | |
| 19436 | private: |
| 19437 | TestReportingContext* test_reporting_context_; |
| 19438 | }; |
| 19439 | |
| 19440 | TEST_F(HttpNetworkTransactionReportingTest, |
| 19441 | DontProcessReportToHeaderNoService) { |
| 19442 | base::HistogramTester histograms; |
| 19443 | clear_reporting_service(); |
| 19444 | RequestPolicy(); |
| 19445 | histograms.ExpectBucketCount( |
| 19446 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19447 | ReportingHeaderParser::HeaderOutcome::DISCARDED_NO_REPORTING_SERVICE, 1); |
| 19448 | } |
| 19449 | |
| 19450 | TEST_F(HttpNetworkTransactionReportingTest, DontProcessReportToHeaderHttp) { |
| 19451 | base::HistogramTester histograms; |
| 19452 | url_ = "http://www.example.org/"; |
| 19453 | RequestPolicy(); |
| 19454 | histograms.ExpectBucketCount( |
| 19455 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19456 | ReportingHeaderParser::HeaderOutcome::DISCARDED_INVALID_SSL_INFO, 1); |
| 19457 | } |
| 19458 | |
| 19459 | TEST_F(HttpNetworkTransactionReportingTest, ProcessReportToHeaderHttps) { |
| 19460 | RequestPolicy(); |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19461 | ASSERT_EQ(1u, reporting_context()->cache()->GetEndpointCount()); |
Lily Chen | fc92ff4 | 2019-05-06 22:59:10 | [diff] [blame] | 19462 | const ReportingEndpoint endpoint = |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19463 | reporting_context()->cache()->GetEndpointForTesting( |
Lily Chen | ad5dd080 | 2020-03-10 21:58:09 | [diff] [blame] | 19464 | ReportingEndpointGroupKey( |
| 19465 | NetworkIsolationKey(), |
| 19466 | url::Origin::Create(GURL("https://www.example.org/")), "nel"), |
Lily Chen | efb6fcf | 2019-04-19 04:17:54 | [diff] [blame] | 19467 | GURL("https://www.example.org/upload/")); |
| 19468 | EXPECT_TRUE(endpoint); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19469 | } |
| 19470 | |
| 19471 | TEST_F(HttpNetworkTransactionReportingTest, |
| 19472 | DontProcessReportToHeaderInvalidHttps) { |
| 19473 | base::HistogramTester histograms; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19474 | CertStatus cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
| 19475 | RequestPolicy(cert_status); |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 19476 | histograms.ExpectBucketCount( |
| 19477 | ReportingHeaderParser::kHeaderOutcomeHistogram, |
| 19478 | ReportingHeaderParser::HeaderOutcome::DISCARDED_CERT_STATUS_ERROR, 1); |
| 19479 | } |
| 19480 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 19481 | |
| 19482 | //----------------------------------------------------------------------------- |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19483 | // Network Error Logging tests |
| 19484 | |
| 19485 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19486 | namespace { |
| 19487 | |
| 19488 | const char kUserAgent[] = "Mozilla/1.0"; |
| 19489 | const char kReferrer[] = "https://www.referrer.org/"; |
| 19490 | |
| 19491 | } // namespace |
| 19492 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19493 | class HttpNetworkTransactionNetworkErrorLoggingTest |
| 19494 | : public HttpNetworkTransactionTest { |
| 19495 | protected: |
| 19496 | void SetUp() override { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19497 | HttpNetworkTransactionTest::SetUp(); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19498 | auto network_error_logging_service = |
| 19499 | std::make_unique<TestNetworkErrorLoggingService>(); |
| 19500 | test_network_error_logging_service_ = network_error_logging_service.get(); |
| 19501 | session_deps_.network_error_logging_service = |
| 19502 | std::move(network_error_logging_service); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19503 | |
| 19504 | extra_headers_.SetHeader("User-Agent", kUserAgent); |
| 19505 | extra_headers_.SetHeader("Referer", kReferrer); |
| 19506 | |
| 19507 | request_.method = "GET"; |
| 19508 | request_.url = GURL(url_); |
| 19509 | request_.extra_headers = extra_headers_; |
| 19510 | request_.reporting_upload_depth = reporting_upload_depth_; |
| 19511 | request_.traffic_annotation = |
| 19512 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19513 | } |
| 19514 | |
| 19515 | TestNetworkErrorLoggingService* network_error_logging_service() const { |
| 19516 | return test_network_error_logging_service_; |
| 19517 | } |
| 19518 | |
| 19519 | void clear_network_error_logging_service() { |
| 19520 | session_deps_.network_error_logging_service.reset(); |
| 19521 | test_network_error_logging_service_ = nullptr; |
| 19522 | } |
| 19523 | |
| 19524 | // Makes an HTTPS request that should install a valid NEL policy. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19525 | void RequestPolicy(CertStatus cert_status = 0) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19526 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19527 | MockWrite data_writes[] = { |
| 19528 | MockWrite("GET / HTTP/1.1\r\n" |
| 19529 | "Host: www.example.org\r\n" |
| 19530 | "Connection: keep-alive\r\n"), |
| 19531 | MockWrite(ASYNC, extra_header_string.data(), |
| 19532 | extra_header_string.size()), |
| 19533 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19534 | MockRead data_reads[] = { |
| 19535 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19536 | MockRead("NEL: {\"report_to\": \"nel\", \"max_age\": 86400}\r\n"), |
| 19537 | MockRead("\r\n"), |
| 19538 | MockRead("hello world"), |
| 19539 | MockRead(SYNCHRONOUS, OK), |
| 19540 | }; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19541 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19542 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19543 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19544 | |
| 19545 | SSLSocketDataProvider ssl(ASYNC, OK); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19546 | if (request_.url.SchemeIsCryptographic()) { |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19547 | ssl.ssl_info.cert = |
| 19548 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19549 | ASSERT_TRUE(ssl.ssl_info.cert); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19550 | ssl.ssl_info.cert_status = cert_status; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19551 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19552 | } |
| 19553 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19554 | TestCompletionCallback callback; |
| 19555 | auto session = CreateSession(&session_deps_); |
| 19556 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19557 | int rv = trans.Start(&request_, callback.callback(), NetLogWithSource()); |
| 19558 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19559 | |
| 19560 | std::string response_data; |
| 19561 | ASSERT_THAT(ReadTransaction(&trans, &response_data), IsOk()); |
| 19562 | EXPECT_EQ("hello world", response_data); |
| 19563 | } |
| 19564 | |
| 19565 | void CheckReport(size_t index, |
| 19566 | int status_code, |
| 19567 | int error_type, |
| 19568 | IPAddress server_ip = IPAddress::IPv4Localhost()) { |
| 19569 | ASSERT_LT(index, network_error_logging_service()->errors().size()); |
| 19570 | |
| 19571 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19572 | network_error_logging_service()->errors()[index]; |
| 19573 | EXPECT_EQ(url_, error.uri); |
| 19574 | EXPECT_EQ(kReferrer, error.referrer); |
| 19575 | EXPECT_EQ(kUserAgent, error.user_agent); |
| 19576 | EXPECT_EQ(server_ip, error.server_ip); |
| 19577 | EXPECT_EQ("http/1.1", error.protocol); |
| 19578 | EXPECT_EQ("GET", error.method); |
| 19579 | EXPECT_EQ(status_code, error.status_code); |
| 19580 | EXPECT_EQ(error_type, error.type); |
| 19581 | EXPECT_EQ(0, error.reporting_upload_depth); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19582 | } |
| 19583 | |
| 19584 | protected: |
| 19585 | std::string url_ = "https://www.example.org/"; |
| 19586 | CertStatus cert_status_ = 0; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19587 | HttpRequestInfo request_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19588 | HttpRequestHeaders extra_headers_; |
| 19589 | int reporting_upload_depth_ = 0; |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19590 | |
| 19591 | private: |
| 19592 | TestNetworkErrorLoggingService* test_network_error_logging_service_; |
| 19593 | }; |
| 19594 | |
| 19595 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19596 | DontProcessNelHeaderNoService) { |
| 19597 | base::HistogramTester histograms; |
| 19598 | clear_network_error_logging_service(); |
| 19599 | RequestPolicy(); |
| 19600 | histograms.ExpectBucketCount( |
| 19601 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19602 | NetworkErrorLoggingService::HeaderOutcome:: |
| 19603 | DISCARDED_NO_NETWORK_ERROR_LOGGING_SERVICE, |
| 19604 | 1); |
| 19605 | } |
| 19606 | |
| 19607 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19608 | DontProcessNelHeaderHttp) { |
| 19609 | base::HistogramTester histograms; |
| 19610 | url_ = "http://www.example.org/"; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19611 | request_.url = GURL(url_); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19612 | RequestPolicy(); |
| 19613 | histograms.ExpectBucketCount( |
| 19614 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19615 | NetworkErrorLoggingService::HeaderOutcome::DISCARDED_INVALID_SSL_INFO, 1); |
| 19616 | } |
| 19617 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 19618 | // Don't set NEL policies received on a proxied connection. |
| 19619 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19620 | DontProcessNelHeaderProxy) { |
| 19621 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 19622 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 19623 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 19624 | RecordingBoundTestNetLog log; |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 19625 | session_deps_.net_log = log.bound().net_log(); |
| 19626 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19627 | |
| 19628 | HttpRequestInfo request; |
| 19629 | request.method = "GET"; |
| 19630 | request.url = GURL("https://www.example.org/"); |
| 19631 | request.traffic_annotation = |
| 19632 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 19633 | |
| 19634 | // Since we have proxy, should try to establish tunnel. |
| 19635 | MockWrite data_writes1[] = { |
| 19636 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 19637 | "Host: www.example.org:443\r\n" |
| 19638 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 19639 | |
| 19640 | MockWrite("GET / HTTP/1.1\r\n" |
| 19641 | "Host: www.example.org\r\n" |
| 19642 | "Connection: keep-alive\r\n\r\n"), |
| 19643 | }; |
| 19644 | |
| 19645 | MockRead data_reads1[] = { |
| 19646 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 19647 | |
| 19648 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 19649 | MockRead("NEL: {\"report_to\": \"nel\", \"max_age\": 86400}\r\n"), |
| 19650 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19651 | MockRead("Content-Length: 100\r\n\r\n"), |
| 19652 | MockRead(SYNCHRONOUS, OK), |
| 19653 | }; |
| 19654 | |
| 19655 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19656 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19657 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19658 | ssl.ssl_info.cert = |
| 19659 | ImportCertFromFile(GetTestCertsDirectory(), "wildcard.pem"); |
| 19660 | ASSERT_TRUE(ssl.ssl_info.cert); |
| 19661 | ssl.ssl_info.cert_status = 0; |
| 19662 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19663 | |
| 19664 | TestCompletionCallback callback1; |
| 19665 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 19666 | |
| 19667 | int rv = trans.Start(&request, callback1.callback(), log.bound()); |
| 19668 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 19669 | |
| 19670 | rv = callback1.WaitForResult(); |
| 19671 | EXPECT_THAT(rv, IsOk()); |
| 19672 | |
| 19673 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 19674 | ASSERT_TRUE(response); |
| 19675 | EXPECT_EQ(200, response->headers->response_code()); |
| 19676 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 19677 | |
| 19678 | // No NEL header was set. |
| 19679 | EXPECT_EQ(0u, network_error_logging_service()->headers().size()); |
| 19680 | } |
| 19681 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19682 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, ProcessNelHeaderHttps) { |
| 19683 | RequestPolicy(); |
| 19684 | ASSERT_EQ(1u, network_error_logging_service()->headers().size()); |
| 19685 | const auto& header = network_error_logging_service()->headers()[0]; |
| 19686 | EXPECT_EQ(url::Origin::Create(GURL("https://www.example.org/")), |
| 19687 | header.origin); |
| 19688 | EXPECT_EQ(IPAddress::IPv4Localhost(), header.received_ip_address); |
| 19689 | EXPECT_EQ("{\"report_to\": \"nel\", \"max_age\": 86400}", header.value); |
| 19690 | } |
| 19691 | |
| 19692 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19693 | DontProcessNelHeaderInvalidHttps) { |
| 19694 | base::HistogramTester histograms; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19695 | CertStatus cert_status = CERT_STATUS_COMMON_NAME_INVALID; |
| 19696 | RequestPolicy(cert_status); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 19697 | histograms.ExpectBucketCount( |
| 19698 | NetworkErrorLoggingService::kHeaderOutcomeHistogram, |
| 19699 | NetworkErrorLoggingService::HeaderOutcome::DISCARDED_CERT_STATUS_ERROR, |
| 19700 | 1); |
| 19701 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19702 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19703 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, CreateReportSuccess) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 19704 | RequestPolicy(); |
| 19705 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19706 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 19707 | } |
| 19708 | |
| 19709 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19710 | CreateReportErrorAfterStart) { |
| 19711 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19712 | auto trans = |
| 19713 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19714 | |
| 19715 | MockConnect mock_connect(SYNCHRONOUS, ERR_NAME_NOT_RESOLVED); |
| 19716 | StaticSocketDataProvider data; |
| 19717 | data.set_connect_data(mock_connect); |
| 19718 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19719 | |
| 19720 | TestCompletionCallback callback; |
| 19721 | |
| 19722 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19723 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NAME_NOT_RESOLVED)); |
| 19724 | |
| 19725 | trans.reset(); |
| 19726 | |
| 19727 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19728 | CheckReport(0 /* index */, 0 /* status_code */, ERR_NAME_NOT_RESOLVED, |
| 19729 | IPAddress() /* server_ip */); |
| 19730 | } |
| 19731 | |
| 19732 | // Same as above except the error is ASYNC |
| 19733 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19734 | CreateReportErrorAfterStartAsync) { |
| 19735 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19736 | auto trans = |
| 19737 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19738 | |
| 19739 | MockConnect mock_connect(ASYNC, ERR_NAME_NOT_RESOLVED); |
| 19740 | StaticSocketDataProvider data; |
| 19741 | data.set_connect_data(mock_connect); |
| 19742 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 19743 | |
| 19744 | TestCompletionCallback callback; |
| 19745 | |
| 19746 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19747 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NAME_NOT_RESOLVED)); |
| 19748 | |
| 19749 | trans.reset(); |
| 19750 | |
| 19751 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19752 | CheckReport(0 /* index */, 0 /* status_code */, ERR_NAME_NOT_RESOLVED, |
| 19753 | IPAddress() /* server_ip */); |
| 19754 | } |
| 19755 | |
| 19756 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19757 | CreateReportReadBodyError) { |
| 19758 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19759 | MockWrite data_writes[] = { |
| 19760 | MockWrite("GET / HTTP/1.1\r\n" |
| 19761 | "Host: www.example.org\r\n" |
| 19762 | "Connection: keep-alive\r\n"), |
| 19763 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19764 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19765 | MockRead data_reads[] = { |
| 19766 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19767 | MockRead("Content-Length: 100\r\n\r\n"), // wrong content length |
| 19768 | MockRead("hello world"), |
| 19769 | MockRead(SYNCHRONOUS, OK), |
| 19770 | }; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19771 | |
| 19772 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19773 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
| 19774 | |
| 19775 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19776 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19777 | |
| 19778 | // Log start time |
| 19779 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19780 | |
| 19781 | TestCompletionCallback callback; |
| 19782 | auto session = CreateSession(&session_deps_); |
| 19783 | auto trans = |
| 19784 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19785 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19786 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19787 | |
| 19788 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 19789 | ASSERT_TRUE(response); |
| 19790 | |
| 19791 | EXPECT_TRUE(response->headers); |
| 19792 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 19793 | |
| 19794 | std::string response_data; |
| 19795 | rv = ReadTransaction(trans.get(), &response_data); |
| 19796 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
| 19797 | |
| 19798 | trans.reset(); |
| 19799 | |
| 19800 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19801 | |
| 19802 | CheckReport(0 /* index */, 200 /* status_code */, |
| 19803 | ERR_CONTENT_LENGTH_MISMATCH); |
| 19804 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19805 | network_error_logging_service()->errors()[0]; |
| 19806 | EXPECT_LE(error.elapsed_time, base::TimeTicks::Now() - start_time); |
| 19807 | } |
| 19808 | |
| 19809 | // Same as above except the final read is ASYNC. |
| 19810 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19811 | CreateReportReadBodyErrorAsync) { |
| 19812 | std::string extra_header_string = extra_headers_.ToString(); |
Lily Chen | 29c0403 | 2019-02-25 21:50:53 | [diff] [blame] | 19813 | MockWrite data_writes[] = { |
| 19814 | MockWrite("GET / HTTP/1.1\r\n" |
| 19815 | "Host: www.example.org\r\n" |
| 19816 | "Connection: keep-alive\r\n"), |
| 19817 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19818 | }; |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 19819 | MockRead data_reads[] = { |
| 19820 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 19821 | MockRead("Content-Length: 100\r\n\r\n"), // wrong content length |
| 19822 | MockRead("hello world"), |
| 19823 | MockRead(ASYNC, OK), |
| 19824 | }; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 19825 | |
| 19826 | StaticSocketDataProvider reads(data_reads, data_writes); |
| 19827 | session_deps_.socket_factory->AddSocketDataProvider(&reads); |
| 19828 | |
| 19829 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 19830 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 19831 | |
| 19832 | // Log start time |
| 19833 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19834 | |
| 19835 | TestCompletionCallback callback; |
| 19836 | auto session = CreateSession(&session_deps_); |
| 19837 | auto trans = |
| 19838 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19839 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 19840 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 19841 | |
| 19842 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 19843 | ASSERT_TRUE(response); |
| 19844 | |
| 19845 | EXPECT_TRUE(response->headers); |
| 19846 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 19847 | |
| 19848 | std::string response_data; |
| 19849 | rv = ReadTransaction(trans.get(), &response_data); |
| 19850 | EXPECT_THAT(rv, IsError(ERR_CONTENT_LENGTH_MISMATCH)); |
| 19851 | |
| 19852 | trans.reset(); |
| 19853 | |
| 19854 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19855 | |
| 19856 | CheckReport(0 /* index */, 200 /* status_code */, |
| 19857 | ERR_CONTENT_LENGTH_MISMATCH); |
| 19858 | const NetworkErrorLoggingService::RequestDetails& error = |
| 19859 | network_error_logging_service()->errors()[0]; |
| 19860 | EXPECT_LE(error.elapsed_time, base::TimeTicks::Now() - start_time); |
| 19861 | } |
| 19862 | |
| 19863 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19864 | CreateReportRestartWithAuth) { |
| 19865 | std::string extra_header_string = extra_headers_.ToString(); |
| 19866 | static const base::TimeDelta kSleepDuration = |
| 19867 | base::TimeDelta::FromMilliseconds(10); |
| 19868 | |
| 19869 | MockWrite data_writes1[] = { |
| 19870 | MockWrite("GET / HTTP/1.1\r\n" |
| 19871 | "Host: www.example.org\r\n" |
| 19872 | "Connection: keep-alive\r\n"), |
| 19873 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19874 | }; |
| 19875 | |
| 19876 | MockRead data_reads1[] = { |
| 19877 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 19878 | // Give a couple authenticate options (only the middle one is actually |
| 19879 | // supported). |
| 19880 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 19881 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 19882 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 19883 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19884 | // Large content-length -- won't matter, as connection will be reset. |
| 19885 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 19886 | MockRead(SYNCHRONOUS, ERR_FAILED), |
| 19887 | }; |
| 19888 | |
| 19889 | // After calling trans->RestartWithAuth(), this is the request we should |
| 19890 | // be issuing -- the final header line contains the credentials. |
| 19891 | MockWrite data_writes2[] = { |
| 19892 | MockWrite("GET / HTTP/1.1\r\n" |
| 19893 | "Host: www.example.org\r\n" |
| 19894 | "Connection: keep-alive\r\n" |
| 19895 | "Authorization: Basic Zm9vOmJhcg==\r\n"), |
| 19896 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19897 | }; |
| 19898 | |
| 19899 | // Lastly, the server responds with the actual content. |
| 19900 | MockRead data_reads2[] = { |
| 19901 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 19902 | MockRead("hello world"), |
| 19903 | MockRead(SYNCHRONOUS, OK), |
| 19904 | }; |
| 19905 | |
| 19906 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 19907 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 19908 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 19909 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 19910 | |
| 19911 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 19912 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 19913 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 19914 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 19915 | |
| 19916 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 19917 | base::TimeTicks restart_time; |
| 19918 | |
| 19919 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 19920 | auto trans = |
| 19921 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 19922 | |
| 19923 | TestCompletionCallback callback1; |
| 19924 | |
| 19925 | int rv = trans->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 19926 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 19927 | |
| 19928 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 19929 | |
| 19930 | TestCompletionCallback callback2; |
| 19931 | |
| 19932 | // Wait 10 ms then restart with auth |
| 19933 | FastForwardBy(kSleepDuration); |
| 19934 | restart_time = base::TimeTicks::Now(); |
| 19935 | rv = |
| 19936 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 19937 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 19938 | |
| 19939 | std::string response_data; |
| 19940 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 19941 | EXPECT_EQ("hello world", response_data); |
| 19942 | |
| 19943 | trans.reset(); |
| 19944 | |
| 19945 | // One 401 report for the auth challenge, then a 200 report for the successful |
| 19946 | // retry. Note that we don't report the error draining the body, as the first |
| 19947 | // request already generated a report for the auth challenge. |
| 19948 | ASSERT_EQ(2u, network_error_logging_service()->errors().size()); |
| 19949 | |
| 19950 | // Check error report contents |
| 19951 | CheckReport(0 /* index */, 401 /* status_code */, OK); |
| 19952 | CheckReport(1 /* index */, 200 /* status_code */, OK); |
| 19953 | |
| 19954 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 19955 | network_error_logging_service()->errors()[0]; |
| 19956 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 19957 | network_error_logging_service()->errors()[1]; |
| 19958 | |
| 19959 | // Sanity-check elapsed time values |
| 19960 | EXPECT_EQ(error1.elapsed_time, restart_time - start_time - kSleepDuration); |
| 19961 | // Check that the start time is refreshed when restarting with auth. |
| 19962 | EXPECT_EQ(error2.elapsed_time, base::TimeTicks::Now() - restart_time); |
| 19963 | } |
| 19964 | |
| 19965 | // Same as above, except draining the body before restarting fails |
| 19966 | // asynchronously. |
| 19967 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 19968 | CreateReportRestartWithAuthAsync) { |
| 19969 | std::string extra_header_string = extra_headers_.ToString(); |
| 19970 | static const base::TimeDelta kSleepDuration = |
| 19971 | base::TimeDelta::FromMilliseconds(10); |
| 19972 | |
| 19973 | MockWrite data_writes1[] = { |
| 19974 | MockWrite("GET / HTTP/1.1\r\n" |
| 19975 | "Host: www.example.org\r\n" |
| 19976 | "Connection: keep-alive\r\n"), |
| 19977 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 19978 | }; |
| 19979 | |
| 19980 | MockRead data_reads1[] = { |
| 19981 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 19982 | // Give a couple authenticate options (only the middle one is actually |
| 19983 | // supported). |
| 19984 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
| 19985 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 19986 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 19987 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 19988 | // Large content-length -- won't matter, as connection will be reset. |
| 19989 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 19990 | MockRead(ASYNC, ERR_FAILED), |
| 19991 | }; |
| 19992 | |
| 19993 | // After calling trans->RestartWithAuth(), this is the request we should |
| 19994 | // be issuing -- the final header line contains the credentials. |
| 19995 | MockWrite data_writes2[] = { |
| 19996 | MockWrite("GET / HTTP/1.1\r\n" |
| 19997 | "Host: www.example.org\r\n" |
| 19998 | "Connection: keep-alive\r\n" |
| 19999 | "Authorization: Basic Zm9vOmJhcg==\r\n"), |
| 20000 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20001 | }; |
| 20002 | |
| 20003 | // Lastly, the server responds with the actual content. |
| 20004 | MockRead data_reads2[] = { |
| 20005 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 20006 | MockRead("hello world"), |
| 20007 | MockRead(SYNCHRONOUS, OK), |
| 20008 | }; |
| 20009 | |
| 20010 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20011 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 20012 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20013 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20014 | |
| 20015 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 20016 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20017 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 20018 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 20019 | |
| 20020 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 20021 | base::TimeTicks restart_time; |
| 20022 | |
| 20023 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20024 | auto trans = |
| 20025 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20026 | |
| 20027 | TestCompletionCallback callback1; |
| 20028 | |
| 20029 | int rv = trans->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 20030 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20031 | |
| 20032 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20033 | |
| 20034 | TestCompletionCallback callback2; |
| 20035 | |
| 20036 | // Wait 10 ms then restart with auth |
| 20037 | FastForwardBy(kSleepDuration); |
| 20038 | restart_time = base::TimeTicks::Now(); |
| 20039 | rv = |
| 20040 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 20041 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20042 | |
| 20043 | std::string response_data; |
| 20044 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20045 | EXPECT_EQ("hello world", response_data); |
| 20046 | |
| 20047 | trans.reset(); |
| 20048 | |
| 20049 | // One 401 report for the auth challenge, then a 200 report for the successful |
| 20050 | // retry. Note that we don't report the error draining the body, as the first |
| 20051 | // request already generated a report for the auth challenge. |
| 20052 | ASSERT_EQ(2u, network_error_logging_service()->errors().size()); |
| 20053 | |
| 20054 | // Check error report contents |
| 20055 | CheckReport(0 /* index */, 401 /* status_code */, OK); |
| 20056 | CheckReport(1 /* index */, 200 /* status_code */, OK); |
| 20057 | |
| 20058 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 20059 | network_error_logging_service()->errors()[0]; |
| 20060 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 20061 | network_error_logging_service()->errors()[1]; |
| 20062 | |
| 20063 | // Sanity-check elapsed time values |
| 20064 | EXPECT_EQ(error1.elapsed_time, restart_time - start_time - kSleepDuration); |
| 20065 | // Check that the start time is refreshed when restarting with auth. |
| 20066 | EXPECT_EQ(error2.elapsed_time, base::TimeTicks::Now() - restart_time); |
| 20067 | } |
| 20068 | |
| 20069 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20070 | CreateReportRetryKeepAliveConnectionReset) { |
| 20071 | std::string extra_header_string = extra_headers_.ToString(); |
| 20072 | MockWrite data_writes1[] = { |
| 20073 | MockWrite("GET / HTTP/1.1\r\n" |
| 20074 | "Host: www.example.org\r\n" |
| 20075 | "Connection: keep-alive\r\n"), |
| 20076 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20077 | MockWrite("GET / HTTP/1.1\r\n" |
| 20078 | "Host: www.example.org\r\n" |
| 20079 | "Connection: keep-alive\r\n"), |
| 20080 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20081 | }; |
| 20082 | |
| 20083 | MockRead data_reads1[] = { |
| 20084 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20085 | MockRead("hello"), |
| 20086 | // Connection is reset |
| 20087 | MockRead(ASYNC, ERR_CONNECTION_RESET), |
| 20088 | }; |
| 20089 | |
| 20090 | // Successful retry |
| 20091 | MockRead data_reads2[] = { |
| 20092 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20093 | MockRead("world"), |
| 20094 | MockRead(ASYNC, OK), |
| 20095 | }; |
| 20096 | |
| 20097 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20098 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
| 20099 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20100 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20101 | |
| 20102 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 20103 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20104 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 20105 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 20106 | |
| 20107 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20108 | auto trans1 = |
| 20109 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20110 | |
| 20111 | TestCompletionCallback callback1; |
| 20112 | |
| 20113 | int rv = trans1->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 20114 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20115 | |
| 20116 | std::string response_data; |
| 20117 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20118 | EXPECT_EQ("hello", response_data); |
| 20119 | |
| 20120 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20121 | |
| 20122 | auto trans2 = |
| 20123 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20124 | |
| 20125 | TestCompletionCallback callback2; |
| 20126 | |
| 20127 | rv = trans2->Start(&request_, callback2.callback(), NetLogWithSource()); |
| 20128 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20129 | |
| 20130 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20131 | EXPECT_EQ("world", response_data); |
| 20132 | |
| 20133 | trans1.reset(); |
| 20134 | trans2.reset(); |
| 20135 | |
| 20136 | // One OK report from first request, then a ERR_CONNECTION_RESET report from |
| 20137 | // the second request, then an OK report from the successful retry. |
| 20138 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20139 | |
| 20140 | // Check error report contents |
| 20141 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20142 | CheckReport(1 /* index */, 0 /* status_code */, ERR_CONNECTION_RESET); |
| 20143 | CheckReport(2 /* index */, 200 /* status_code */, OK); |
| 20144 | } |
| 20145 | |
| 20146 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20147 | CreateReportRetryKeepAlive408) { |
| 20148 | std::string extra_header_string = extra_headers_.ToString(); |
| 20149 | MockWrite data_writes1[] = { |
| 20150 | MockWrite("GET / HTTP/1.1\r\n" |
| 20151 | "Host: www.example.org\r\n" |
| 20152 | "Connection: keep-alive\r\n"), |
| 20153 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20154 | MockWrite("GET / HTTP/1.1\r\n" |
| 20155 | "Host: www.example.org\r\n" |
| 20156 | "Connection: keep-alive\r\n"), |
| 20157 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20158 | }; |
| 20159 | |
| 20160 | MockRead data_reads1[] = { |
| 20161 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20162 | MockRead("hello"), |
| 20163 | // 408 Request Timeout |
| 20164 | MockRead(SYNCHRONOUS, |
| 20165 | "HTTP/1.1 408 Request Timeout\r\n" |
| 20166 | "Connection: Keep-Alive\r\n" |
| 20167 | "Content-Length: 6\r\n\r\n" |
| 20168 | "Pickle"), |
| 20169 | }; |
| 20170 | |
| 20171 | // Successful retry |
| 20172 | MockRead data_reads2[] = { |
| 20173 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 20174 | MockRead("world"), |
| 20175 | MockRead(ASYNC, OK), |
| 20176 | }; |
| 20177 | |
| 20178 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20179 | StaticSocketDataProvider data2(data_reads2, base::span<MockWrite>()); |
| 20180 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20181 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20182 | |
| 20183 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 20184 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 20185 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 20186 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 20187 | |
| 20188 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20189 | auto trans1 = |
| 20190 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20191 | |
| 20192 | TestCompletionCallback callback1; |
| 20193 | |
| 20194 | int rv = trans1->Start(&request_, callback1.callback(), NetLogWithSource()); |
| 20195 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20196 | |
| 20197 | std::string response_data; |
| 20198 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20199 | EXPECT_EQ("hello", response_data); |
| 20200 | |
| 20201 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20202 | |
| 20203 | auto trans2 = |
| 20204 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20205 | |
| 20206 | TestCompletionCallback callback2; |
| 20207 | |
| 20208 | rv = trans2->Start(&request_, callback2.callback(), NetLogWithSource()); |
| 20209 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20210 | |
| 20211 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20212 | EXPECT_EQ("world", response_data); |
| 20213 | |
| 20214 | trans1.reset(); |
| 20215 | trans2.reset(); |
| 20216 | |
| 20217 | // One 200 report from first request, then a 408 report from |
| 20218 | // the second request, then a 200 report from the successful retry. |
| 20219 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20220 | |
| 20221 | // Check error report contents |
| 20222 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20223 | CheckReport(1 /* index */, 408 /* status_code */, OK); |
| 20224 | CheckReport(2 /* index */, 200 /* status_code */, OK); |
| 20225 | } |
| 20226 | |
| 20227 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20228 | CreateReportRetry421WithoutConnectionPooling) { |
| 20229 | // Two hosts resolve to the same IP address. |
| 20230 | const std::string ip_addr = "1.2.3.4"; |
| 20231 | IPAddress ip; |
| 20232 | ASSERT_TRUE(ip.AssignFromIPLiteral(ip_addr)); |
| 20233 | IPEndPoint peer_addr = IPEndPoint(ip, 443); |
| 20234 | |
| 20235 | session_deps_.host_resolver = std::make_unique<MockCachingHostResolver>(); |
| 20236 | session_deps_.host_resolver->rules()->AddRule("www.example.org", ip_addr); |
| 20237 | session_deps_.host_resolver->rules()->AddRule("mail.example.org", ip_addr); |
| 20238 | |
| 20239 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20240 | |
| 20241 | // Two requests on the first connection. |
| 20242 | spdy::SpdySerializedFrame req1( |
| 20243 | spdy_util_.ConstructSpdyGet("https://www.example.org", 1, LOWEST)); |
| 20244 | spdy_util_.UpdateWithStreamDestruction(1); |
| 20245 | spdy::SpdySerializedFrame req2( |
| 20246 | spdy_util_.ConstructSpdyGet("https://mail.example.org", 3, LOWEST)); |
| 20247 | spdy::SpdySerializedFrame rst( |
| 20248 | spdy_util_.ConstructSpdyRstStream(3, spdy::ERROR_CODE_CANCEL)); |
| 20249 | MockWrite writes1[] = { |
| 20250 | CreateMockWrite(req1, 0), |
| 20251 | CreateMockWrite(req2, 3), |
| 20252 | CreateMockWrite(rst, 6), |
| 20253 | }; |
| 20254 | |
| 20255 | // The first one succeeds, the second gets error 421 Misdirected Request. |
| 20256 | spdy::SpdySerializedFrame resp1( |
| 20257 | spdy_util_.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 20258 | spdy::SpdySerializedFrame body1(spdy_util_.ConstructSpdyDataFrame(1, true)); |
| 20259 | spdy::SpdyHeaderBlock response_headers; |
| 20260 | response_headers[spdy::kHttp2StatusHeader] = "421"; |
| 20261 | spdy::SpdySerializedFrame resp2( |
| 20262 | spdy_util_.ConstructSpdyReply(3, std::move(response_headers))); |
| 20263 | MockRead reads1[] = {CreateMockRead(resp1, 1), CreateMockRead(body1, 2), |
| 20264 | CreateMockRead(resp2, 4), MockRead(ASYNC, 0, 5)}; |
| 20265 | |
| 20266 | MockConnect connect1(ASYNC, OK, peer_addr); |
| 20267 | SequencedSocketData data1(connect1, reads1, writes1); |
| 20268 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20269 | |
| 20270 | AddSSLSocketData(); |
| 20271 | |
| 20272 | // Retry the second request on a second connection. |
| 20273 | SpdyTestUtil spdy_util2; |
| 20274 | spdy::SpdySerializedFrame req3( |
| 20275 | spdy_util2.ConstructSpdyGet("https://mail.example.org", 1, LOWEST)); |
| 20276 | MockWrite writes2[] = { |
| 20277 | CreateMockWrite(req3, 0), |
| 20278 | }; |
| 20279 | |
| 20280 | spdy::SpdySerializedFrame resp3( |
| 20281 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 20282 | spdy::SpdySerializedFrame body3(spdy_util2.ConstructSpdyDataFrame(1, true)); |
| 20283 | MockRead reads2[] = {CreateMockRead(resp3, 1), CreateMockRead(body3, 2), |
| 20284 | MockRead(ASYNC, 0, 3)}; |
| 20285 | |
| 20286 | MockConnect connect2(ASYNC, OK, peer_addr); |
| 20287 | SequencedSocketData data2(connect2, reads2, writes2); |
| 20288 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20289 | |
| 20290 | AddSSLSocketData(); |
| 20291 | |
| 20292 | // Preload mail.example.org into HostCache. |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20293 | int rv = session_deps_.host_resolver->LoadIntoCache( |
Matt Menke | cd43923 | 2019-11-05 15:15:33 | [diff] [blame] | 20294 | HostPortPair("mail.example.com", 443), NetworkIsolationKey(), |
| 20295 | base::nullopt); |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20296 | EXPECT_THAT(rv, IsOk()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20297 | |
| 20298 | HttpRequestInfo request1; |
| 20299 | request1.method = "GET"; |
| 20300 | request1.url = GURL("https://www.example.org/"); |
| 20301 | request1.load_flags = 0; |
| 20302 | request1.traffic_annotation = |
| 20303 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20304 | auto trans1 = |
| 20305 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20306 | |
Eric Orth | f4db66a | 2019-02-19 21:35:33 | [diff] [blame] | 20307 | TestCompletionCallback callback; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20308 | rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 20309 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20310 | |
| 20311 | const HttpResponseInfo* response = trans1->GetResponseInfo(); |
| 20312 | ASSERT_TRUE(response); |
| 20313 | ASSERT_TRUE(response->headers); |
| 20314 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 20315 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 20316 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 20317 | std::string response_data; |
| 20318 | ASSERT_THAT(ReadTransaction(trans1.get(), &response_data), IsOk()); |
| 20319 | EXPECT_EQ("hello!", response_data); |
| 20320 | |
| 20321 | trans1.reset(); |
| 20322 | |
| 20323 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20324 | |
| 20325 | HttpRequestInfo request2; |
| 20326 | request2.method = "GET"; |
| 20327 | request2.url = GURL("https://mail.example.org/"); |
| 20328 | request2.load_flags = 0; |
| 20329 | request2.traffic_annotation = |
| 20330 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20331 | auto trans2 = |
| 20332 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20333 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 20334 | RecordingBoundTestNetLog log; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20335 | rv = trans2->Start(&request2, callback.callback(), log.bound()); |
| 20336 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20337 | |
| 20338 | response = trans2->GetResponseInfo(); |
| 20339 | ASSERT_TRUE(response); |
| 20340 | ASSERT_TRUE(response->headers); |
| 20341 | EXPECT_EQ("HTTP/1.1 200", response->headers->GetStatusLine()); |
| 20342 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 20343 | EXPECT_TRUE(response->was_alpn_negotiated); |
| 20344 | ASSERT_THAT(ReadTransaction(trans2.get(), &response_data), IsOk()); |
| 20345 | EXPECT_EQ("hello!", response_data); |
| 20346 | |
| 20347 | trans2.reset(); |
| 20348 | |
| 20349 | // One 200 report from the first request, then a 421 report from the |
| 20350 | // second request, then a 200 report from the successful retry. |
| 20351 | ASSERT_EQ(3u, network_error_logging_service()->errors().size()); |
| 20352 | |
| 20353 | // Check error report contents |
| 20354 | const NetworkErrorLoggingService::RequestDetails& error1 = |
| 20355 | network_error_logging_service()->errors()[0]; |
| 20356 | EXPECT_EQ(GURL("https://www.example.org/"), error1.uri); |
| 20357 | EXPECT_TRUE(error1.referrer.is_empty()); |
| 20358 | EXPECT_EQ("", error1.user_agent); |
| 20359 | EXPECT_EQ(ip, error1.server_ip); |
| 20360 | EXPECT_EQ("h2", error1.protocol); |
| 20361 | EXPECT_EQ("GET", error1.method); |
| 20362 | EXPECT_EQ(200, error1.status_code); |
| 20363 | EXPECT_EQ(OK, error1.type); |
| 20364 | EXPECT_EQ(0, error1.reporting_upload_depth); |
| 20365 | |
| 20366 | const NetworkErrorLoggingService::RequestDetails& error2 = |
| 20367 | network_error_logging_service()->errors()[1]; |
| 20368 | EXPECT_EQ(GURL("https://mail.example.org/"), error2.uri); |
| 20369 | EXPECT_TRUE(error2.referrer.is_empty()); |
| 20370 | EXPECT_EQ("", error2.user_agent); |
| 20371 | EXPECT_EQ(ip, error2.server_ip); |
| 20372 | EXPECT_EQ("h2", error2.protocol); |
| 20373 | EXPECT_EQ("GET", error2.method); |
| 20374 | EXPECT_EQ(421, error2.status_code); |
| 20375 | EXPECT_EQ(OK, error2.type); |
| 20376 | EXPECT_EQ(0, error2.reporting_upload_depth); |
| 20377 | |
| 20378 | const NetworkErrorLoggingService::RequestDetails& error3 = |
| 20379 | network_error_logging_service()->errors()[2]; |
| 20380 | EXPECT_EQ(GURL("https://mail.example.org/"), error3.uri); |
| 20381 | EXPECT_TRUE(error3.referrer.is_empty()); |
| 20382 | EXPECT_EQ("", error3.user_agent); |
| 20383 | EXPECT_EQ(ip, error3.server_ip); |
| 20384 | EXPECT_EQ("h2", error3.protocol); |
| 20385 | EXPECT_EQ("GET", error3.method); |
| 20386 | EXPECT_EQ(200, error3.status_code); |
| 20387 | EXPECT_EQ(OK, error3.type); |
| 20388 | EXPECT_EQ(0, error3.reporting_upload_depth); |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20389 | } |
| 20390 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 20391 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20392 | CreateReportCancelAfterStart) { |
| 20393 | StaticSocketDataProvider data; |
| 20394 | data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 20395 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20396 | |
| 20397 | TestCompletionCallback callback; |
| 20398 | auto session = CreateSession(&session_deps_); |
| 20399 | auto trans = |
| 20400 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20401 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20402 | EXPECT_EQ(rv, ERR_IO_PENDING); |
| 20403 | |
| 20404 | // Cancel after start. |
| 20405 | trans.reset(); |
| 20406 | |
| 20407 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20408 | CheckReport(0 /* index */, 0 /* status_code */, ERR_ABORTED, |
| 20409 | IPAddress() /* server_ip */); |
| 20410 | } |
| 20411 | |
| 20412 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20413 | CreateReportCancelBeforeReadingBody) { |
| 20414 | std::string extra_header_string = extra_headers_.ToString(); |
| 20415 | MockWrite data_writes[] = { |
| 20416 | MockWrite("GET / HTTP/1.1\r\n" |
| 20417 | "Host: www.example.org\r\n" |
| 20418 | "Connection: keep-alive\r\n"), |
| 20419 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20420 | }; |
| 20421 | MockRead data_reads[] = { |
| 20422 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 20423 | MockRead("Content-Length: 100\r\n\r\n"), // Body is never read. |
| 20424 | }; |
| 20425 | |
| 20426 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20427 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20428 | |
| 20429 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20430 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20431 | |
| 20432 | TestCompletionCallback callback; |
| 20433 | auto session = CreateSession(&session_deps_); |
| 20434 | auto trans = |
| 20435 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20436 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20437 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20438 | |
| 20439 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20440 | ASSERT_TRUE(response); |
| 20441 | |
| 20442 | EXPECT_TRUE(response->headers); |
| 20443 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 20444 | |
| 20445 | // Cancel before reading the body. |
| 20446 | trans.reset(); |
| 20447 | |
| 20448 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20449 | CheckReport(0 /* index */, 200 /* status_code */, ERR_ABORTED); |
| 20450 | } |
| 20451 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20452 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, DontCreateReportHttp) { |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20453 | RequestPolicy(); |
| 20454 | EXPECT_EQ(1u, network_error_logging_service()->headers().size()); |
| 20455 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20456 | |
| 20457 | // Make HTTP request |
| 20458 | std::string extra_header_string = extra_headers_.ToString(); |
| 20459 | MockRead data_reads[] = { |
| 20460 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 20461 | MockRead("hello world"), |
| 20462 | MockRead(SYNCHRONOUS, OK), |
| 20463 | }; |
| 20464 | MockWrite data_writes[] = { |
| 20465 | MockWrite("GET / HTTP/1.1\r\n" |
| 20466 | "Host: www.example.org\r\n" |
| 20467 | "Connection: keep-alive\r\n"), |
| 20468 | MockWrite(ASYNC, extra_header_string.data(), extra_header_string.size()), |
| 20469 | }; |
| 20470 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 20471 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20472 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20473 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20474 | // Insecure url |
| 20475 | url_ = "http://www.example.org/"; |
| 20476 | request_.url = GURL(url_); |
| 20477 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20478 | TestCompletionCallback callback; |
| 20479 | auto session = CreateSession(&session_deps_); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20480 | auto trans = |
| 20481 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20482 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20483 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20484 | |
| 20485 | std::string response_data; |
| 20486 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20487 | EXPECT_EQ("hello world", response_data); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20488 | |
| 20489 | // Insecure request does not generate a report |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20490 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20491 | } |
| 20492 | |
| 20493 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20494 | DontCreateReportHttpError) { |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20495 | RequestPolicy(); |
| 20496 | EXPECT_EQ(1u, network_error_logging_service()->headers().size()); |
| 20497 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20498 | |
| 20499 | // Make HTTP request that fails |
| 20500 | MockRead data_reads[] = { |
| 20501 | MockRead("hello world"), |
| 20502 | MockRead(SYNCHRONOUS, OK), |
| 20503 | }; |
| 20504 | |
| 20505 | StaticSocketDataProvider data(data_reads, base::span<MockWrite>()); |
| 20506 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20507 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20508 | url_ = "http://www.originwithoutpolicy.com:2000/"; |
| 20509 | request_.url = GURL(url_); |
| 20510 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20511 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20512 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20513 | auto trans = |
| 20514 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20515 | TestCompletionCallback callback; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20516 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20517 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_INVALID_HTTP_RESPONSE)); |
| 20518 | |
| 20519 | // Insecure request does not generate a report, regardless of existence of a |
| 20520 | // policy for the origin. |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 20521 | EXPECT_EQ(1u, network_error_logging_service()->errors().size()); |
| 20522 | } |
| 20523 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 20524 | // Don't report on proxy auth challenges, don't report if connecting through a |
| 20525 | // proxy. |
| 20526 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, DontCreateReportProxy) { |
| 20527 | HttpRequestInfo request; |
| 20528 | request.method = "GET"; |
| 20529 | request.url = GURL("https://www.example.org/"); |
| 20530 | request.traffic_annotation = |
| 20531 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20532 | |
| 20533 | // Configure against proxy server "myproxy:70". |
| 20534 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 20535 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 20536 | "PROXY myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20537 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20538 | |
| 20539 | // Since we have proxy, should try to establish tunnel. |
| 20540 | MockWrite data_writes1[] = { |
| 20541 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 20542 | "Host: www.example.org:443\r\n" |
| 20543 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 20544 | }; |
| 20545 | |
| 20546 | // The proxy responds to the connect with a 407, using a non-persistent |
| 20547 | // connection. |
| 20548 | MockRead data_reads1[] = { |
| 20549 | // No credentials. |
| 20550 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 20551 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 20552 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 20553 | }; |
| 20554 | |
| 20555 | MockWrite data_writes2[] = { |
| 20556 | // After calling trans->RestartWithAuth(), this is the request we should |
| 20557 | // be issuing -- the final header line contains the credentials. |
| 20558 | MockWrite("CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 20559 | "Host: www.example.org:443\r\n" |
| 20560 | "Proxy-Connection: keep-alive\r\n" |
| 20561 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 20562 | |
| 20563 | MockWrite("GET / HTTP/1.1\r\n" |
| 20564 | "Host: www.example.org\r\n" |
| 20565 | "Connection: keep-alive\r\n\r\n"), |
| 20566 | }; |
| 20567 | |
| 20568 | MockRead data_reads2[] = { |
| 20569 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 20570 | |
| 20571 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20572 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 20573 | MockRead("Content-Length: 5\r\n\r\n"), |
| 20574 | MockRead(SYNCHRONOUS, "hello"), |
| 20575 | }; |
| 20576 | |
| 20577 | StaticSocketDataProvider data1(data_reads1, data_writes1); |
| 20578 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 20579 | StaticSocketDataProvider data2(data_reads2, data_writes2); |
| 20580 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 20581 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20582 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20583 | |
| 20584 | TestCompletionCallback callback1; |
| 20585 | |
| 20586 | auto trans = |
| 20587 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20588 | |
| 20589 | int rv = trans->Start(&request, callback1.callback(), NetLogWithSource()); |
| 20590 | EXPECT_THAT(callback1.GetResult(rv), IsOk()); |
| 20591 | |
| 20592 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20593 | EXPECT_EQ(407, response->headers->response_code()); |
| 20594 | |
| 20595 | std::string response_data; |
| 20596 | rv = ReadTransaction(trans.get(), &response_data); |
| 20597 | EXPECT_THAT(rv, IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
| 20598 | |
| 20599 | // No NEL report is generated for the 407. |
| 20600 | EXPECT_EQ(0u, network_error_logging_service()->errors().size()); |
| 20601 | |
| 20602 | TestCompletionCallback callback2; |
| 20603 | |
| 20604 | rv = |
| 20605 | trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback2.callback()); |
| 20606 | EXPECT_THAT(callback2.GetResult(rv), IsOk()); |
| 20607 | |
| 20608 | response = trans->GetResponseInfo(); |
| 20609 | EXPECT_EQ(200, response->headers->response_code()); |
| 20610 | |
| 20611 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20612 | EXPECT_EQ("hello", response_data); |
| 20613 | |
| 20614 | trans.reset(); |
| 20615 | |
| 20616 | // No NEL report is generated because we are behind a proxy. |
| 20617 | EXPECT_EQ(0u, network_error_logging_service()->errors().size()); |
| 20618 | } |
| 20619 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20620 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, |
| 20621 | ReportContainsUploadDepth) { |
| 20622 | reporting_upload_depth_ = 7; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20623 | request_.reporting_upload_depth = reporting_upload_depth_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20624 | RequestPolicy(); |
| 20625 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20626 | const NetworkErrorLoggingService::RequestDetails& error = |
| 20627 | network_error_logging_service()->errors()[0]; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20628 | EXPECT_EQ(7, error.reporting_upload_depth); |
| 20629 | } |
| 20630 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20631 | TEST_F(HttpNetworkTransactionNetworkErrorLoggingTest, ReportElapsedTime) { |
| 20632 | std::string extra_header_string = extra_headers_.ToString(); |
| 20633 | static const base::TimeDelta kSleepDuration = |
| 20634 | base::TimeDelta::FromMilliseconds(10); |
| 20635 | |
| 20636 | std::vector<MockWrite> data_writes = { |
| 20637 | MockWrite(ASYNC, 0, |
| 20638 | "GET / HTTP/1.1\r\n" |
| 20639 | "Host: www.example.org\r\n" |
| 20640 | "Connection: keep-alive\r\n"), |
| 20641 | MockWrite(ASYNC, 1, extra_header_string.data()), |
| 20642 | }; |
| 20643 | |
| 20644 | std::vector<MockRead> data_reads = { |
| 20645 | // Write one byte of the status line, followed by a pause. |
| 20646 | MockRead(ASYNC, 2, "H"), |
| 20647 | MockRead(ASYNC, ERR_IO_PENDING, 3), |
| 20648 | MockRead(ASYNC, 4, "TTP/1.1 200 OK\r\n\r\n"), |
| 20649 | MockRead(ASYNC, 5, "hello world"), |
| 20650 | MockRead(SYNCHRONOUS, OK, 6), |
| 20651 | }; |
| 20652 | |
| 20653 | SequencedSocketData data(data_reads, data_writes); |
| 20654 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20655 | |
| 20656 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 20657 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20658 | |
| 20659 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20660 | |
| 20661 | auto trans = |
| 20662 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20663 | |
| 20664 | TestCompletionCallback callback; |
| 20665 | |
| 20666 | int rv = trans->Start(&request_, callback.callback(), NetLogWithSource()); |
| 20667 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20668 | |
| 20669 | data.RunUntilPaused(); |
| 20670 | ASSERT_TRUE(data.IsPaused()); |
| 20671 | FastForwardBy(kSleepDuration); |
| 20672 | data.Resume(); |
| 20673 | |
| 20674 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 20675 | |
| 20676 | std::string response_data; |
| 20677 | ASSERT_THAT(ReadTransaction(trans.get(), &response_data), IsOk()); |
| 20678 | EXPECT_EQ("hello world", response_data); |
| 20679 | |
| 20680 | trans.reset(); |
| 20681 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20682 | ASSERT_EQ(1u, network_error_logging_service()->errors().size()); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20683 | |
| 20684 | CheckReport(0 /* index */, 200 /* status_code */, OK); |
| 20685 | |
| 20686 | const NetworkErrorLoggingService::RequestDetails& error = |
| 20687 | network_error_logging_service()->errors()[0]; |
| 20688 | |
| 20689 | // Sanity-check elapsed time in error report |
| 20690 | EXPECT_EQ(kSleepDuration, error.elapsed_time); |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 20691 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 20692 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 20693 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 20694 | |
Batalov Vladislav | a4e97a50 | 2019-04-11 15:35:23 | [diff] [blame] | 20695 | TEST_F(HttpNetworkTransactionTest, AlwaysFailRequestToCache) { |
| 20696 | HttpRequestInfo request; |
| 20697 | request.method = "GET"; |
| 20698 | request.url = GURL("http://example.org/"); |
| 20699 | |
| 20700 | request.load_flags = LOAD_ONLY_FROM_CACHE; |
| 20701 | |
| 20702 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20703 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 20704 | TestCompletionCallback callback1; |
| 20705 | int rv = trans.Start(&request, callback1.callback(), NetLogWithSource()); |
| 20706 | |
| 20707 | EXPECT_THAT(rv, IsError(ERR_CACHE_MISS)); |
| 20708 | } |
| 20709 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20710 | TEST_F(HttpNetworkTransactionTest, ZeroRTTDoesntConfirm) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20711 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20712 | HttpRequestInfo request; |
| 20713 | request.method = "GET"; |
| 20714 | request.url = GURL("https://www.example.org/"); |
| 20715 | request.traffic_annotation = |
| 20716 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20717 | |
| 20718 | MockWrite data_writes[] = { |
| 20719 | MockWrite("GET / HTTP/1.1\r\n" |
| 20720 | "Host: www.example.org\r\n" |
| 20721 | "Connection: keep-alive\r\n\r\n"), |
| 20722 | }; |
| 20723 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20724 | MockRead data_reads[] = { |
| 20725 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20726 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20727 | MockRead(SYNCHRONOUS, "1"), |
| 20728 | }; |
| 20729 | |
| 20730 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20731 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20732 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20733 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20734 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20735 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20736 | session_deps_.enable_early_data = true; |
| 20737 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20738 | |
| 20739 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20740 | |
| 20741 | TestCompletionCallback callback; |
| 20742 | auto trans = |
| 20743 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20744 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20745 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20746 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20747 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20748 | |
| 20749 | rv = callback.WaitForResult(); |
| 20750 | EXPECT_THAT(rv, IsOk()); |
| 20751 | |
| 20752 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20753 | ASSERT_TRUE(response); |
| 20754 | ASSERT_TRUE(response->headers); |
| 20755 | EXPECT_EQ(200, response->headers->response_code()); |
| 20756 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20757 | |
| 20758 | // Check that ConfirmHandshake wasn't called. |
| 20759 | ASSERT_FALSE(ssl.ConfirmDataConsumed()); |
| 20760 | ASSERT_TRUE(ssl.WriteBeforeConfirm()); |
| 20761 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20762 | // The handshake time should include the time it took to run Connect(), but |
| 20763 | // not ConfirmHandshake(). |
| 20764 | LoadTimingInfo load_timing_info; |
| 20765 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20766 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20767 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20768 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + kDelay); |
| 20769 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, start_time + kDelay); |
| 20770 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20771 | trans.reset(); |
| 20772 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 20773 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20774 | } |
| 20775 | |
| 20776 | TEST_F(HttpNetworkTransactionTest, ZeroRTTSyncConfirmSyncWrite) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20777 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20778 | HttpRequestInfo request; |
| 20779 | request.method = "POST"; |
| 20780 | request.url = GURL("https://www.example.org/"); |
| 20781 | request.traffic_annotation = |
| 20782 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20783 | |
| 20784 | MockWrite data_writes[] = { |
| 20785 | MockWrite(SYNCHRONOUS, |
| 20786 | "POST / HTTP/1.1\r\n" |
| 20787 | "Host: www.example.org\r\n" |
| 20788 | "Connection: keep-alive\r\n" |
| 20789 | "Content-Length: 0\r\n\r\n"), |
| 20790 | }; |
| 20791 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20792 | MockRead data_reads[] = { |
| 20793 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20794 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20795 | MockRead(SYNCHRONOUS, "1"), |
| 20796 | }; |
| 20797 | |
| 20798 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20799 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20800 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20801 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20802 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20803 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20804 | session_deps_.enable_early_data = true; |
| 20805 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20806 | |
| 20807 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20808 | |
| 20809 | TestCompletionCallback callback; |
| 20810 | auto trans = |
| 20811 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20812 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20813 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20814 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20815 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20816 | |
| 20817 | rv = callback.WaitForResult(); |
| 20818 | EXPECT_THAT(rv, IsOk()); |
| 20819 | |
| 20820 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20821 | ASSERT_TRUE(response); |
| 20822 | ASSERT_TRUE(response->headers); |
| 20823 | EXPECT_EQ(200, response->headers->response_code()); |
| 20824 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20825 | |
| 20826 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20827 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20828 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20829 | // The handshake time should include the time it took to run Connect(), but |
| 20830 | // not ConfirmHandshake(). If ConfirmHandshake() returns synchronously, we |
| 20831 | // assume the connection did not negotiate 0-RTT or the handshake was already |
| 20832 | // confirmed. |
| 20833 | LoadTimingInfo load_timing_info; |
| 20834 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20835 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20836 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20837 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + kDelay); |
| 20838 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, start_time + kDelay); |
| 20839 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20840 | trans.reset(); |
| 20841 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 20842 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20843 | } |
| 20844 | |
| 20845 | TEST_F(HttpNetworkTransactionTest, ZeroRTTSyncConfirmAsyncWrite) { |
| 20846 | HttpRequestInfo request; |
| 20847 | request.method = "POST"; |
| 20848 | request.url = GURL("https://www.example.org/"); |
| 20849 | request.traffic_annotation = |
| 20850 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20851 | |
| 20852 | MockWrite data_writes[] = { |
| 20853 | MockWrite(ASYNC, |
| 20854 | "POST / HTTP/1.1\r\n" |
| 20855 | "Host: www.example.org\r\n" |
| 20856 | "Connection: keep-alive\r\n" |
| 20857 | "Content-Length: 0\r\n\r\n"), |
| 20858 | }; |
| 20859 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20860 | MockRead data_reads[] = { |
| 20861 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20862 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20863 | MockRead(SYNCHRONOUS, "1"), |
| 20864 | }; |
| 20865 | |
| 20866 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20867 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20868 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 20869 | ssl.confirm = MockConfirm(SYNCHRONOUS, OK); |
| 20870 | session_deps_.enable_early_data = true; |
| 20871 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20872 | |
| 20873 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20874 | |
| 20875 | TestCompletionCallback callback; |
| 20876 | auto trans = |
| 20877 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20878 | |
| 20879 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20880 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20881 | |
| 20882 | rv = callback.WaitForResult(); |
| 20883 | EXPECT_THAT(rv, IsOk()); |
| 20884 | |
| 20885 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20886 | ASSERT_TRUE(response); |
| 20887 | ASSERT_TRUE(response->headers); |
| 20888 | EXPECT_EQ(200, response->headers->response_code()); |
| 20889 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20890 | |
| 20891 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20892 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20893 | |
| 20894 | trans.reset(); |
| 20895 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 20896 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20897 | } |
| 20898 | |
| 20899 | TEST_F(HttpNetworkTransactionTest, ZeroRTTAsyncConfirmSyncWrite) { |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20900 | static const base::TimeDelta kDelay = base::TimeDelta::FromMilliseconds(10); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20901 | HttpRequestInfo request; |
| 20902 | request.method = "POST"; |
| 20903 | request.url = GURL("https://www.example.org/"); |
| 20904 | request.traffic_annotation = |
| 20905 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20906 | |
| 20907 | MockWrite data_writes[] = { |
| 20908 | MockWrite(SYNCHRONOUS, |
| 20909 | "POST / HTTP/1.1\r\n" |
| 20910 | "Host: www.example.org\r\n" |
| 20911 | "Connection: keep-alive\r\n" |
| 20912 | "Content-Length: 0\r\n\r\n"), |
| 20913 | }; |
| 20914 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20915 | MockRead data_reads[] = { |
| 20916 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20917 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20918 | MockRead(SYNCHRONOUS, "1"), |
| 20919 | }; |
| 20920 | |
| 20921 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20922 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20923 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20924 | ssl.connect_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20925 | ssl.confirm = MockConfirm(ASYNC, OK); |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20926 | ssl.confirm_callback = FastForwardByCallback(kDelay); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20927 | session_deps_.enable_early_data = true; |
| 20928 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20929 | |
| 20930 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20931 | |
| 20932 | TestCompletionCallback callback; |
| 20933 | auto trans = |
| 20934 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 20935 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20936 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20937 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 20938 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 20939 | |
| 20940 | rv = callback.WaitForResult(); |
| 20941 | EXPECT_THAT(rv, IsOk()); |
| 20942 | |
| 20943 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 20944 | ASSERT_TRUE(response); |
| 20945 | ASSERT_TRUE(response->headers); |
| 20946 | EXPECT_EQ(200, response->headers->response_code()); |
| 20947 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 20948 | |
| 20949 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 20950 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 20951 | |
David Benjamin | bae08ba | 2019-10-18 21:06:15 | [diff] [blame] | 20952 | // The handshake time should include the time it took to run Connect() and |
| 20953 | // ConfirmHandshake(). |
| 20954 | LoadTimingInfo load_timing_info; |
| 20955 | EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info)); |
| 20956 | EXPECT_EQ(load_timing_info.connect_timing.connect_start, start_time); |
| 20957 | EXPECT_EQ(load_timing_info.connect_timing.ssl_start, start_time); |
| 20958 | EXPECT_EQ(load_timing_info.connect_timing.ssl_end, start_time + 2 * kDelay); |
| 20959 | EXPECT_EQ(load_timing_info.connect_timing.connect_end, |
| 20960 | start_time + 2 * kDelay); |
| 20961 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20962 | trans.reset(); |
| 20963 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 20964 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20965 | } |
| 20966 | |
| 20967 | TEST_F(HttpNetworkTransactionTest, ZeroRTTAsyncConfirmAsyncWrite) { |
| 20968 | HttpRequestInfo request; |
| 20969 | request.method = "POST"; |
| 20970 | request.url = GURL("https://www.example.org/"); |
| 20971 | request.traffic_annotation = |
| 20972 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 20973 | |
| 20974 | MockWrite data_writes[] = { |
| 20975 | MockWrite(ASYNC, |
| 20976 | "POST / HTTP/1.1\r\n" |
| 20977 | "Host: www.example.org\r\n" |
| 20978 | "Connection: keep-alive\r\n" |
| 20979 | "Content-Length: 0\r\n\r\n"), |
| 20980 | }; |
| 20981 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 20982 | MockRead data_reads[] = { |
| 20983 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 20984 | MockRead("Content-Length: 1\r\n\r\n"), |
| 20985 | MockRead(SYNCHRONOUS, "1"), |
| 20986 | }; |
| 20987 | |
| 20988 | StaticSocketDataProvider data(data_reads, data_writes); |
| 20989 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 20990 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 20991 | ssl.confirm = MockConfirm(ASYNC, OK); |
| 20992 | session_deps_.enable_early_data = true; |
| 20993 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 20994 | |
| 20995 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 20996 | |
| 20997 | TestCompletionCallback callback; |
| 20998 | auto trans = |
| 20999 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21000 | |
| 21001 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 21002 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 21003 | |
| 21004 | rv = callback.WaitForResult(); |
| 21005 | EXPECT_THAT(rv, IsOk()); |
| 21006 | |
| 21007 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 21008 | ASSERT_TRUE(response); |
| 21009 | ASSERT_TRUE(response->headers); |
| 21010 | EXPECT_EQ(200, response->headers->response_code()); |
| 21011 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 21012 | |
| 21013 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 21014 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 21015 | |
| 21016 | trans.reset(); |
| 21017 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 21018 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21019 | } |
| 21020 | |
David Benjamin | fb97693 | 2019-05-15 13:39:15 | [diff] [blame] | 21021 | // 0-RTT rejects are handled at HttpNetworkTransaction. |
| 21022 | TEST_F(HttpNetworkTransactionTest, ZeroRTTReject) { |
| 21023 | enum class RejectType { |
| 21024 | kRead, |
| 21025 | kWrite, |
| 21026 | kConfirm, |
| 21027 | }; |
| 21028 | |
| 21029 | for (RejectType type : |
| 21030 | {RejectType::kRead, RejectType::kWrite, RejectType::kConfirm}) { |
| 21031 | SCOPED_TRACE(static_cast<int>(type)); |
| 21032 | for (Error reject_error : |
| 21033 | {ERR_EARLY_DATA_REJECTED, ERR_WRONG_VERSION_ON_EARLY_DATA}) { |
| 21034 | SCOPED_TRACE(reject_error); |
| 21035 | session_deps_.socket_factory = |
| 21036 | std::make_unique<MockClientSocketFactory>(); |
| 21037 | |
| 21038 | HttpRequestInfo request; |
| 21039 | request.method = type == RejectType::kConfirm ? "POST" : "GET"; |
| 21040 | request.url = GURL("https://www.example.org/"); |
| 21041 | request.traffic_annotation = |
| 21042 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21043 | |
| 21044 | // The first request fails. |
| 21045 | std::vector<MockWrite> data1_writes; |
| 21046 | std::vector<MockRead> data1_reads; |
| 21047 | SSLSocketDataProvider ssl1(SYNCHRONOUS, OK); |
| 21048 | switch (type) { |
| 21049 | case RejectType::kRead: |
| 21050 | data1_writes.emplace_back( |
| 21051 | "GET / HTTP/1.1\r\n" |
| 21052 | "Host: www.example.org\r\n" |
| 21053 | "Connection: keep-alive\r\n\r\n"); |
| 21054 | data1_reads.emplace_back(ASYNC, reject_error); |
| 21055 | // Cause ConfirmHandshake to hang (it should not be called). |
| 21056 | ssl1.confirm = MockConfirm(SYNCHRONOUS, ERR_IO_PENDING); |
| 21057 | break; |
| 21058 | case RejectType::kWrite: |
| 21059 | data1_writes.emplace_back(ASYNC, reject_error); |
| 21060 | // Cause ConfirmHandshake to hang (it should not be called). |
| 21061 | ssl1.confirm = MockConfirm(SYNCHRONOUS, ERR_IO_PENDING); |
| 21062 | break; |
| 21063 | case RejectType::kConfirm: |
| 21064 | // The request never gets far enough to read or write. |
| 21065 | ssl1.confirm = MockConfirm(ASYNC, reject_error); |
| 21066 | break; |
| 21067 | } |
| 21068 | |
| 21069 | StaticSocketDataProvider data1(data1_reads, data1_writes); |
| 21070 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 21071 | session_deps_.enable_early_data = true; |
| 21072 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 21073 | |
| 21074 | // The retry succeeds. |
| 21075 | // |
| 21076 | // TODO(https://crbug.com/950705): If |reject_error| is |
| 21077 | // ERR_EARLY_DATA_REJECTED, the retry should happen over the same socket. |
| 21078 | MockWrite data2_writes[] = { |
| 21079 | request.method == "POST" |
| 21080 | ? MockWrite("POST / HTTP/1.1\r\n" |
| 21081 | "Host: www.example.org\r\n" |
| 21082 | "Connection: keep-alive\r\n" |
| 21083 | "Content-Length: 0\r\n\r\n") |
| 21084 | : MockWrite("GET / HTTP/1.1\r\n" |
| 21085 | "Host: www.example.org\r\n" |
| 21086 | "Connection: keep-alive\r\n\r\n"), |
| 21087 | }; |
| 21088 | |
| 21089 | MockRead data2_reads[] = { |
| 21090 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 21091 | MockRead("Content-Length: 1\r\n\r\n"), |
| 21092 | MockRead(SYNCHRONOUS, "1"), |
| 21093 | }; |
| 21094 | |
| 21095 | StaticSocketDataProvider data2(data2_reads, data2_writes); |
| 21096 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21097 | SSLSocketDataProvider ssl2(SYNCHRONOUS, OK); |
| 21098 | ssl2.confirm = MockConfirm(ASYNC, OK); |
| 21099 | session_deps_.enable_early_data = true; |
| 21100 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 21101 | |
| 21102 | std::unique_ptr<HttpNetworkSession> session( |
| 21103 | CreateSession(&session_deps_)); |
| 21104 | |
| 21105 | TestCompletionCallback callback; |
| 21106 | auto trans = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21107 | session.get()); |
| 21108 | |
| 21109 | EXPECT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21110 | NetLogWithSource())), |
| 21111 | IsOk()); |
| 21112 | |
| 21113 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 21114 | ASSERT_TRUE(response); |
| 21115 | ASSERT_TRUE(response->headers); |
| 21116 | EXPECT_EQ(200, response->headers->response_code()); |
| 21117 | EXPECT_EQ(1, response->headers->GetContentLength()); |
| 21118 | } |
| 21119 | } |
| 21120 | } |
| 21121 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21122 | TEST_F(HttpNetworkTransactionTest, ZeroRTTConfirmErrorSync) { |
| 21123 | HttpRequestInfo request; |
| 21124 | request.method = "POST"; |
| 21125 | request.url = GURL("https://www.example.org/"); |
| 21126 | request.traffic_annotation = |
| 21127 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21128 | |
| 21129 | MockWrite data_writes[] = { |
| 21130 | MockWrite("POST / HTTP/1.1\r\n" |
| 21131 | "Host: www.example.org\r\n" |
| 21132 | "Connection: keep-alive\r\n" |
| 21133 | "Content-Length: 0\r\n\r\n"), |
| 21134 | }; |
| 21135 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21136 | MockRead data_reads[] = { |
| 21137 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 21138 | MockRead("Content-Length: 1\r\n\r\n"), |
| 21139 | MockRead(SYNCHRONOUS, "1"), |
| 21140 | }; |
| 21141 | |
| 21142 | StaticSocketDataProvider data(data_reads, data_writes); |
| 21143 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 21144 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 21145 | ssl.confirm = MockConfirm(SYNCHRONOUS, ERR_SSL_PROTOCOL_ERROR); |
| 21146 | session_deps_.enable_early_data = true; |
| 21147 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 21148 | |
| 21149 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21150 | |
| 21151 | TestCompletionCallback callback; |
| 21152 | auto trans = |
| 21153 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21154 | |
| 21155 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 21156 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 21157 | |
| 21158 | rv = callback.WaitForResult(); |
| 21159 | EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 21160 | |
| 21161 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 21162 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 21163 | |
| 21164 | trans.reset(); |
| 21165 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 21166 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21167 | } |
| 21168 | |
| 21169 | TEST_F(HttpNetworkTransactionTest, ZeroRTTConfirmErrorAsync) { |
| 21170 | HttpRequestInfo request; |
| 21171 | request.method = "POST"; |
| 21172 | request.url = GURL("https://www.example.org/"); |
| 21173 | request.traffic_annotation = |
| 21174 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21175 | |
| 21176 | MockWrite data_writes[] = { |
| 21177 | MockWrite("POST / HTTP/1.1\r\n" |
| 21178 | "Host: www.example.org\r\n" |
| 21179 | "Connection: keep-alive\r\n" |
| 21180 | "Content-Length: 0\r\n\r\n"), |
| 21181 | }; |
| 21182 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21183 | MockRead data_reads[] = { |
| 21184 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 21185 | MockRead("Content-Length: 1\r\n\r\n"), |
| 21186 | MockRead(SYNCHRONOUS, "1"), |
| 21187 | }; |
| 21188 | |
| 21189 | StaticSocketDataProvider data(data_reads, data_writes); |
| 21190 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 21191 | SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 21192 | ssl.confirm = MockConfirm(ASYNC, ERR_SSL_PROTOCOL_ERROR); |
| 21193 | session_deps_.enable_early_data = true; |
| 21194 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 21195 | |
| 21196 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21197 | |
| 21198 | TestCompletionCallback callback; |
| 21199 | auto trans = |
| 21200 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21201 | |
| 21202 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 21203 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 21204 | |
| 21205 | rv = callback.WaitForResult(); |
| 21206 | EXPECT_THAT(rv, IsError(ERR_SSL_PROTOCOL_ERROR)); |
| 21207 | |
| 21208 | // Check that the Write didn't get called before ConfirmHandshake completed. |
| 21209 | ASSERT_FALSE(ssl.WriteBeforeConfirm()); |
| 21210 | |
| 21211 | trans.reset(); |
| 21212 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 21213 | session->CloseAllConnections(ERR_FAILED, "Very good reason"); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 21214 | } |
| 21215 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21216 | // Test the proxy and origin server each requesting both TLS client certificates |
| 21217 | // and HTTP auth. This is a regression test for https://crbug.com/946406. |
| 21218 | TEST_F(HttpNetworkTransactionTest, AuthEverything) { |
| 21219 | // Note these hosts must match the CheckBasic*Auth() functions. |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 21220 | session_deps_.proxy_resolution_service = |
| 21221 | ConfiguredProxyResolutionService::CreateFixed( |
| 21222 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21223 | |
| 21224 | auto cert_request_info_proxy = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21225 | cert_request_info_proxy->host_and_port = HostPortPair("myproxy", 70); |
| 21226 | |
| 21227 | std::unique_ptr<FakeClientCertIdentity> identity_proxy = |
| 21228 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21229 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21230 | ASSERT_TRUE(identity_proxy); |
| 21231 | |
| 21232 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21233 | cert_request_info_origin->host_and_port = |
| 21234 | HostPortPair("www.example.org", 443); |
| 21235 | |
| 21236 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21237 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21238 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21239 | ASSERT_TRUE(identity_origin); |
| 21240 | |
| 21241 | HttpRequestInfo request; |
| 21242 | request.method = "GET"; |
| 21243 | request.url = GURL("https://www.example.org/"); |
| 21244 | request.traffic_annotation = |
| 21245 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21246 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21247 | // First, the client connects to the proxy, which requests a client |
| 21248 | // certificate. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21249 | SSLSocketDataProvider ssl_proxy1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21250 | ssl_proxy1.cert_request_info = cert_request_info_proxy.get(); |
| 21251 | ssl_proxy1.expected_send_client_cert = false; |
| 21252 | StaticSocketDataProvider data1; |
| 21253 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21254 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21255 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21256 | // The client responds with a certificate on a new connection. The handshake |
| 21257 | // succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21258 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21259 | ssl_proxy2.expected_send_client_cert = true; |
| 21260 | ssl_proxy2.expected_client_cert = identity_proxy->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21261 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21262 | std::vector<MockWrite> mock_writes2; |
| 21263 | std::vector<MockRead> mock_reads2; |
| 21264 | mock_writes2.emplace_back( |
| 21265 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21266 | "Host: www.example.org:443\r\n" |
| 21267 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21268 | mock_reads2.emplace_back( |
| 21269 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21270 | "Content-Length: 0\r\n" |
| 21271 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21272 | // The client retries with credentials. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21273 | mock_writes2.emplace_back( |
| 21274 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21275 | "Host: www.example.org:443\r\n" |
| 21276 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21277 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21278 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21279 | mock_reads2.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21280 | // The origin requests client certificates. |
| 21281 | SSLSocketDataProvider ssl_origin2(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21282 | ssl_origin2.cert_request_info = cert_request_info_origin.get(); |
| 21283 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21284 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21285 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 21286 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin2); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21287 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21288 | // The client responds to the origin client certificate request on a new |
| 21289 | // connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21290 | SSLSocketDataProvider ssl_proxy3(ASYNC, OK); |
| 21291 | ssl_proxy3.expected_send_client_cert = true; |
| 21292 | ssl_proxy3.expected_client_cert = identity_proxy->certificate(); |
| 21293 | std::vector<MockWrite> mock_writes3; |
| 21294 | std::vector<MockRead> mock_reads3; |
| 21295 | mock_writes3.emplace_back( |
| 21296 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21297 | "Host: www.example.org:443\r\n" |
| 21298 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21299 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21300 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21301 | mock_reads3.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21302 | SSLSocketDataProvider ssl_origin3(ASYNC, OK); |
| 21303 | ssl_origin3.expected_send_client_cert = true; |
| 21304 | ssl_origin3.expected_client_cert = identity_origin->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21305 | // The client sends the origin HTTP request, which results in another HTTP |
| 21306 | // auth request. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21307 | mock_writes3.emplace_back( |
| 21308 | "GET / HTTP/1.1\r\n" |
| 21309 | "Host: www.example.org\r\n" |
| 21310 | "Connection: keep-alive\r\n\r\n"); |
| 21311 | mock_reads3.emplace_back( |
| 21312 | "HTTP/1.1 401 Unauthorized\r\n" |
| 21313 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 21314 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21315 | // The client retries with credentials, and the request finally succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21316 | mock_writes3.emplace_back( |
| 21317 | "GET / HTTP/1.1\r\n" |
| 21318 | "Host: www.example.org\r\n" |
| 21319 | "Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21320 | // Authenticate as user:pass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21321 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21322 | mock_reads3.emplace_back( |
| 21323 | "HTTP/1.1 200 OK\r\n" |
| 21324 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21325 | // The client makes another request. This should reuse the socket with all |
| 21326 | // credentials cached. |
| 21327 | mock_writes3.emplace_back( |
| 21328 | "GET / HTTP/1.1\r\n" |
| 21329 | "Host: www.example.org\r\n" |
| 21330 | "Connection: keep-alive\r\n" |
| 21331 | // Authenticate as user:pass. |
| 21332 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21333 | mock_reads3.emplace_back( |
| 21334 | "HTTP/1.1 200 OK\r\n" |
| 21335 | "Content-Length: 0\r\n\r\n"); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21336 | StaticSocketDataProvider data3(mock_reads3, mock_writes3); |
| 21337 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21338 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy3); |
| 21339 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin3); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21340 | |
| 21341 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21342 | |
| 21343 | // Start the request. |
| 21344 | TestCompletionCallback callback; |
| 21345 | auto trans = |
| 21346 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21347 | int rv = callback.GetResult( |
| 21348 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21349 | |
| 21350 | // Handle the proxy client certificate challenge. |
| 21351 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21352 | SSLCertRequestInfo* cert_request_info = |
| 21353 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21354 | ASSERT_TRUE(cert_request_info); |
| 21355 | EXPECT_TRUE(cert_request_info->is_proxy); |
| 21356 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21357 | cert_request_info_proxy->host_and_port); |
| 21358 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21359 | identity_proxy->certificate(), identity_proxy->ssl_private_key(), |
| 21360 | callback.callback())); |
| 21361 | |
| 21362 | // Handle the proxy HTTP auth challenge. |
| 21363 | ASSERT_THAT(rv, IsOk()); |
| 21364 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21365 | EXPECT_TRUE( |
| 21366 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21367 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21368 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21369 | callback.callback())); |
| 21370 | |
| 21371 | // Handle the origin client certificate challenge. |
| 21372 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21373 | cert_request_info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21374 | ASSERT_TRUE(cert_request_info); |
| 21375 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21376 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21377 | cert_request_info_origin->host_and_port); |
| 21378 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21379 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21380 | callback.callback())); |
| 21381 | |
| 21382 | // Handle the origin HTTP auth challenge. |
| 21383 | ASSERT_THAT(rv, IsOk()); |
| 21384 | EXPECT_EQ(401, trans->GetResponseInfo()->headers->response_code()); |
| 21385 | EXPECT_TRUE( |
| 21386 | CheckBasicSecureServerAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21387 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21388 | AuthCredentials(ASCIIToUTF16("user"), ASCIIToUTF16("pass")), |
| 21389 | callback.callback())); |
| 21390 | |
| 21391 | // The request completes. |
| 21392 | ASSERT_THAT(rv, IsOk()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21393 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21394 | |
| 21395 | // Make a second request. This time all credentials are cached. |
| 21396 | trans = |
| 21397 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21398 | ASSERT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21399 | NetLogWithSource())), |
| 21400 | IsOk()); |
| 21401 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21402 | } |
| 21403 | |
| 21404 | // Test the proxy and origin server each requesting both TLS client certificates |
| 21405 | // and HTTP auth and each HTTP auth closing the connection. This is a regression |
| 21406 | // test for https://crbug.com/946406. |
| 21407 | TEST_F(HttpNetworkTransactionTest, AuthEverythingWithConnectClose) { |
| 21408 | // Note these hosts must match the CheckBasic*Auth() functions. |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 21409 | session_deps_.proxy_resolution_service = |
| 21410 | ConfiguredProxyResolutionService::CreateFixed( |
| 21411 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21412 | |
| 21413 | auto cert_request_info_proxy = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21414 | cert_request_info_proxy->host_and_port = HostPortPair("myproxy", 70); |
| 21415 | |
| 21416 | std::unique_ptr<FakeClientCertIdentity> identity_proxy = |
| 21417 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21418 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21419 | ASSERT_TRUE(identity_proxy); |
| 21420 | |
| 21421 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21422 | cert_request_info_origin->host_and_port = |
| 21423 | HostPortPair("www.example.org", 443); |
| 21424 | |
| 21425 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21426 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21427 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21428 | ASSERT_TRUE(identity_origin); |
| 21429 | |
| 21430 | HttpRequestInfo request; |
| 21431 | request.method = "GET"; |
| 21432 | request.url = GURL("https://www.example.org/"); |
| 21433 | request.traffic_annotation = |
| 21434 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21435 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21436 | // First, the client connects to the proxy, which requests a client |
| 21437 | // certificate. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21438 | SSLSocketDataProvider ssl_proxy1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21439 | ssl_proxy1.cert_request_info = cert_request_info_proxy.get(); |
| 21440 | ssl_proxy1.expected_send_client_cert = false; |
| 21441 | StaticSocketDataProvider data1; |
| 21442 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21443 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21444 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21445 | // The client responds with a certificate on a new connection. The handshake |
| 21446 | // succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21447 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21448 | ssl_proxy2.expected_send_client_cert = true; |
| 21449 | ssl_proxy2.expected_client_cert = identity_proxy->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21450 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21451 | std::vector<MockWrite> mock_writes2; |
| 21452 | std::vector<MockRead> mock_reads2; |
| 21453 | mock_writes2.emplace_back( |
| 21454 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21455 | "Host: www.example.org:443\r\n" |
| 21456 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21457 | mock_reads2.emplace_back( |
| 21458 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21459 | "Content-Length: 0\r\n" |
| 21460 | "Proxy-Connection: close\r\n" |
| 21461 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
| 21462 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21463 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21464 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21465 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21466 | // The client retries with credentials on a new connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21467 | SSLSocketDataProvider ssl_proxy3(ASYNC, OK); |
| 21468 | ssl_proxy3.expected_send_client_cert = true; |
| 21469 | ssl_proxy3.expected_client_cert = identity_proxy->certificate(); |
| 21470 | std::vector<MockWrite> mock_writes3; |
| 21471 | std::vector<MockRead> mock_reads3; |
| 21472 | mock_writes3.emplace_back( |
| 21473 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21474 | "Host: www.example.org:443\r\n" |
| 21475 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21476 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21477 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21478 | mock_reads3.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21479 | // The origin requests client certificates. |
| 21480 | SSLSocketDataProvider ssl_origin3(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21481 | ssl_origin3.cert_request_info = cert_request_info_origin.get(); |
| 21482 | StaticSocketDataProvider data3(mock_reads3, mock_writes3); |
| 21483 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21484 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy3); |
| 21485 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin3); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21486 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21487 | // The client responds to the origin client certificate request on a new |
| 21488 | // connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21489 | SSLSocketDataProvider ssl_proxy4(ASYNC, OK); |
| 21490 | ssl_proxy4.expected_send_client_cert = true; |
| 21491 | ssl_proxy4.expected_client_cert = identity_proxy->certificate(); |
| 21492 | std::vector<MockWrite> mock_writes4; |
| 21493 | std::vector<MockRead> mock_reads4; |
| 21494 | mock_writes4.emplace_back( |
| 21495 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21496 | "Host: www.example.org:443\r\n" |
| 21497 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21498 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21499 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21500 | mock_reads4.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21501 | SSLSocketDataProvider ssl_origin4(ASYNC, OK); |
| 21502 | ssl_origin4.expected_send_client_cert = true; |
| 21503 | ssl_origin4.expected_client_cert = identity_origin->certificate(); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21504 | // The client sends the origin HTTP request, which results in another HTTP |
| 21505 | // auth request and closed connection. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21506 | mock_writes4.emplace_back( |
| 21507 | "GET / HTTP/1.1\r\n" |
| 21508 | "Host: www.example.org\r\n" |
| 21509 | "Connection: keep-alive\r\n\r\n"); |
| 21510 | mock_reads4.emplace_back( |
| 21511 | "HTTP/1.1 401 Unauthorized\r\n" |
| 21512 | "WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n" |
| 21513 | "Connection: close\r\n" |
| 21514 | "Content-Length: 0\r\n\r\n"); |
| 21515 | StaticSocketDataProvider data4(mock_reads4, mock_writes4); |
| 21516 | session_deps_.socket_factory->AddSocketDataProvider(&data4); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21517 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy4); |
| 21518 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin4); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21519 | |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21520 | // The client retries with credentials on a new connection, and the request |
| 21521 | // finally succeeds. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21522 | SSLSocketDataProvider ssl_proxy5(ASYNC, OK); |
| 21523 | ssl_proxy5.expected_send_client_cert = true; |
| 21524 | ssl_proxy5.expected_client_cert = identity_proxy->certificate(); |
| 21525 | std::vector<MockWrite> mock_writes5; |
| 21526 | std::vector<MockRead> mock_reads5; |
| 21527 | mock_writes5.emplace_back( |
| 21528 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21529 | "Host: www.example.org:443\r\n" |
| 21530 | "Proxy-Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21531 | // Authenticate as proxyuser:proxypass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21532 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21533 | mock_reads5.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21534 | SSLSocketDataProvider ssl_origin5(ASYNC, OK); |
| 21535 | ssl_origin5.expected_send_client_cert = true; |
| 21536 | ssl_origin5.expected_client_cert = identity_origin->certificate(); |
| 21537 | mock_writes5.emplace_back( |
| 21538 | "GET / HTTP/1.1\r\n" |
| 21539 | "Host: www.example.org\r\n" |
| 21540 | "Connection: keep-alive\r\n" |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21541 | // Authenticate as user:pass. |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21542 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21543 | mock_reads5.emplace_back( |
| 21544 | "HTTP/1.1 200 OK\r\n" |
| 21545 | "Connection: close\r\n" |
| 21546 | "Content-Length: 0\r\n\r\n"); |
| 21547 | StaticSocketDataProvider data5(mock_reads5, mock_writes5); |
| 21548 | session_deps_.socket_factory->AddSocketDataProvider(&data5); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21549 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy5); |
| 21550 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin5); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21551 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21552 | // The client makes a second request. This needs yet another connection, but |
| 21553 | // all credentials are cached. |
| 21554 | SSLSocketDataProvider ssl_proxy6(ASYNC, OK); |
| 21555 | ssl_proxy6.expected_send_client_cert = true; |
| 21556 | ssl_proxy6.expected_client_cert = identity_proxy->certificate(); |
| 21557 | std::vector<MockWrite> mock_writes6; |
| 21558 | std::vector<MockRead> mock_reads6; |
| 21559 | mock_writes6.emplace_back( |
| 21560 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21561 | "Host: www.example.org:443\r\n" |
| 21562 | "Proxy-Connection: keep-alive\r\n" |
| 21563 | // Authenticate as proxyuser:proxypass. |
| 21564 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21565 | mock_reads6.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21566 | SSLSocketDataProvider ssl_origin6(ASYNC, OK); |
| 21567 | ssl_origin6.expected_send_client_cert = true; |
| 21568 | ssl_origin6.expected_client_cert = identity_origin->certificate(); |
| 21569 | mock_writes6.emplace_back( |
| 21570 | "GET / HTTP/1.1\r\n" |
| 21571 | "Host: www.example.org\r\n" |
| 21572 | "Connection: keep-alive\r\n" |
| 21573 | // Authenticate as user:pass. |
| 21574 | "Authorization: Basic dXNlcjpwYXNz\r\n\r\n"); |
| 21575 | mock_reads6.emplace_back( |
| 21576 | "HTTP/1.1 200 OK\r\n" |
| 21577 | "Connection: close\r\n" |
| 21578 | "Content-Length: 0\r\n\r\n"); |
| 21579 | StaticSocketDataProvider data6(mock_reads6, mock_writes6); |
| 21580 | session_deps_.socket_factory->AddSocketDataProvider(&data6); |
| 21581 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy6); |
| 21582 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin6); |
| 21583 | |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21584 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21585 | |
| 21586 | // Start the request. |
| 21587 | TestCompletionCallback callback; |
| 21588 | auto trans = |
| 21589 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21590 | int rv = callback.GetResult( |
| 21591 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21592 | |
| 21593 | // Handle the proxy client certificate challenge. |
| 21594 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21595 | SSLCertRequestInfo* cert_request_info = |
| 21596 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21597 | ASSERT_TRUE(cert_request_info); |
| 21598 | EXPECT_TRUE(cert_request_info->is_proxy); |
| 21599 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21600 | cert_request_info_proxy->host_and_port); |
| 21601 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21602 | identity_proxy->certificate(), identity_proxy->ssl_private_key(), |
| 21603 | callback.callback())); |
| 21604 | |
| 21605 | // Handle the proxy HTTP auth challenge. |
| 21606 | ASSERT_THAT(rv, IsOk()); |
| 21607 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21608 | EXPECT_TRUE( |
| 21609 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21610 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21611 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21612 | callback.callback())); |
| 21613 | |
| 21614 | // Handle the origin client certificate challenge. |
| 21615 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21616 | cert_request_info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21617 | ASSERT_TRUE(cert_request_info); |
| 21618 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21619 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21620 | cert_request_info_origin->host_and_port); |
| 21621 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21622 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21623 | callback.callback())); |
| 21624 | |
| 21625 | // Handle the origin HTTP auth challenge. |
| 21626 | ASSERT_THAT(rv, IsOk()); |
| 21627 | EXPECT_EQ(401, trans->GetResponseInfo()->headers->response_code()); |
| 21628 | EXPECT_TRUE( |
| 21629 | CheckBasicSecureServerAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21630 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21631 | AuthCredentials(ASCIIToUTF16("user"), ASCIIToUTF16("pass")), |
| 21632 | callback.callback())); |
| 21633 | |
| 21634 | // The request completes. |
| 21635 | ASSERT_THAT(rv, IsOk()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21636 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 21637 | |
| 21638 | // Make a second request. This time all credentials are cached. |
| 21639 | trans = |
| 21640 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21641 | ASSERT_THAT(callback.GetResult(trans->Start(&request, callback.callback(), |
| 21642 | NetLogWithSource())), |
| 21643 | IsOk()); |
| 21644 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21645 | } |
| 21646 | |
| 21647 | // Test the proxy requesting HTTP auth and the server requesting TLS client |
| 21648 | // certificates. This is a regression test for https://crbug.com/946406. |
| 21649 | TEST_F(HttpNetworkTransactionTest, ProxyHTTPAndServerTLSAuth) { |
| 21650 | // Note these hosts must match the CheckBasic*Auth() functions. |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 21651 | session_deps_.proxy_resolution_service = |
| 21652 | ConfiguredProxyResolutionService::CreateFixed( |
| 21653 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
David Benjamin | 7ebab03 | 2019-04-30 21:51:30 | [diff] [blame] | 21654 | |
| 21655 | auto cert_request_info_origin = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21656 | cert_request_info_origin->host_and_port = |
| 21657 | HostPortPair("www.example.org", 443); |
| 21658 | |
| 21659 | std::unique_ptr<FakeClientCertIdentity> identity_origin = |
| 21660 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21661 | GetTestCertsDirectory(), "client_2.pem", "client_2.pk8"); |
| 21662 | ASSERT_TRUE(identity_origin); |
| 21663 | |
| 21664 | HttpRequestInfo request; |
| 21665 | request.method = "GET"; |
| 21666 | request.url = GURL("https://www.example.org/"); |
| 21667 | request.traffic_annotation = |
| 21668 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21669 | |
| 21670 | // The client connects to the proxy. The handshake succeeds. |
| 21671 | SSLSocketDataProvider ssl_proxy1(ASYNC, OK); |
| 21672 | // The client attempts an HTTP CONNECT, but the proxy requests basic auth. |
| 21673 | std::vector<MockWrite> mock_writes1; |
| 21674 | std::vector<MockRead> mock_reads1; |
| 21675 | mock_writes1.emplace_back( |
| 21676 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21677 | "Host: www.example.org:443\r\n" |
| 21678 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 21679 | mock_reads1.emplace_back( |
| 21680 | "HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 21681 | "Content-Length: 0\r\n" |
| 21682 | "Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n\r\n"); |
| 21683 | // The client retries with credentials, and the request finally succeeds. |
| 21684 | mock_writes1.emplace_back( |
| 21685 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21686 | "Host: www.example.org:443\r\n" |
| 21687 | "Proxy-Connection: keep-alive\r\n" |
| 21688 | // Authenticate as proxyuser:proxypass. |
| 21689 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21690 | mock_reads1.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21691 | // The origin requests client certificates. |
| 21692 | SSLSocketDataProvider ssl_origin1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21693 | ssl_origin1.cert_request_info = cert_request_info_origin.get(); |
| 21694 | StaticSocketDataProvider data1(mock_reads1, mock_writes1); |
| 21695 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 21696 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
| 21697 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin1); |
| 21698 | |
| 21699 | // The client responds to the origin client certificate request on a new |
| 21700 | // connection. |
| 21701 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 21702 | std::vector<MockWrite> mock_writes2; |
| 21703 | std::vector<MockRead> mock_reads2; |
| 21704 | mock_writes2.emplace_back( |
| 21705 | "CONNECT www.example.org:443 HTTP/1.1\r\n" |
| 21706 | "Host: www.example.org:443\r\n" |
| 21707 | "Proxy-Connection: keep-alive\r\n" |
| 21708 | // Authenticate as proxyuser:proxypass. |
| 21709 | "Proxy-Authorization: Basic cHJveHl1c2VyOnByb3h5cGFzcw==\r\n\r\n"); |
| 21710 | mock_reads2.emplace_back("HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 21711 | SSLSocketDataProvider ssl_origin2(ASYNC, OK); |
| 21712 | ssl_origin2.expected_send_client_cert = true; |
| 21713 | ssl_origin2.expected_client_cert = identity_origin->certificate(); |
| 21714 | // The client sends the origin HTTP request, which succeeds. |
| 21715 | mock_writes2.emplace_back( |
| 21716 | "GET / HTTP/1.1\r\n" |
| 21717 | "Host: www.example.org\r\n" |
| 21718 | "Connection: keep-alive\r\n\r\n"); |
| 21719 | mock_reads2.emplace_back( |
| 21720 | "HTTP/1.1 200 OK\r\n" |
| 21721 | "Content-Length: 0\r\n\r\n"); |
| 21722 | StaticSocketDataProvider data2(mock_reads2, mock_writes2); |
| 21723 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21724 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 21725 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin2); |
| 21726 | |
| 21727 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21728 | |
| 21729 | // Start the request. |
| 21730 | TestCompletionCallback callback; |
| 21731 | auto trans = |
| 21732 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21733 | int rv = callback.GetResult( |
| 21734 | trans->Start(&request, callback.callback(), NetLogWithSource())); |
| 21735 | |
| 21736 | // Handle the proxy HTTP auth challenge. |
| 21737 | ASSERT_THAT(rv, IsOk()); |
| 21738 | EXPECT_EQ(407, trans->GetResponseInfo()->headers->response_code()); |
| 21739 | EXPECT_TRUE( |
| 21740 | CheckBasicSecureProxyAuth(trans->GetResponseInfo()->auth_challenge)); |
| 21741 | rv = callback.GetResult(trans->RestartWithAuth( |
| 21742 | AuthCredentials(ASCIIToUTF16("proxyuser"), ASCIIToUTF16("proxypass")), |
| 21743 | callback.callback())); |
| 21744 | |
| 21745 | // Handle the origin client certificate challenge. |
| 21746 | ASSERT_THAT(rv, IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21747 | SSLCertRequestInfo* cert_request_info = |
| 21748 | trans->GetResponseInfo()->cert_request_info.get(); |
| 21749 | ASSERT_TRUE(cert_request_info); |
| 21750 | EXPECT_FALSE(cert_request_info->is_proxy); |
| 21751 | EXPECT_EQ(cert_request_info->host_and_port, |
| 21752 | cert_request_info_origin->host_and_port); |
| 21753 | rv = callback.GetResult(trans->RestartWithCertificate( |
| 21754 | identity_origin->certificate(), identity_origin->ssl_private_key(), |
| 21755 | callback.callback())); |
| 21756 | |
| 21757 | // The request completes. |
| 21758 | ASSERT_THAT(rv, IsOk()); |
| 21759 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
David Benjamin | 2eb827f | 2019-04-29 18:31:04 | [diff] [blame] | 21760 | } |
| 21761 | |
David Benjamin | 6e673a8 | 2019-04-30 22:52:58 | [diff] [blame] | 21762 | // Test that socket reuse works with client certificates. |
| 21763 | TEST_F(HttpNetworkTransactionTest, ClientCertSocketReuse) { |
| 21764 | auto cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 21765 | cert_request_info->host_and_port = HostPortPair("www.example.org", 443); |
| 21766 | |
| 21767 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 21768 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 21769 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 21770 | ASSERT_TRUE(identity); |
| 21771 | |
| 21772 | HttpRequestInfo request1; |
| 21773 | request1.method = "GET"; |
| 21774 | request1.url = GURL("https://www.example.org/a"); |
| 21775 | request1.traffic_annotation = |
| 21776 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21777 | |
| 21778 | HttpRequestInfo request2; |
| 21779 | request2.method = "GET"; |
| 21780 | request2.url = GURL("https://www.example.org/b"); |
| 21781 | request2.traffic_annotation = |
| 21782 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21783 | |
| 21784 | // The first connection results in a client certificate request. |
| 21785 | StaticSocketDataProvider data1; |
| 21786 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 21787 | SSLSocketDataProvider ssl1(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 21788 | ssl1.cert_request_info = cert_request_info.get(); |
| 21789 | ssl1.expected_send_client_cert = false; |
| 21790 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 21791 | |
| 21792 | // The second connection succeeds and is usable for both requests. |
| 21793 | MockWrite mock_writes[] = { |
| 21794 | MockWrite("GET /a HTTP/1.1\r\n" |
| 21795 | "Host: www.example.org\r\n" |
| 21796 | "Connection: keep-alive\r\n\r\n"), |
| 21797 | MockWrite("GET /b HTTP/1.1\r\n" |
| 21798 | "Host: www.example.org\r\n" |
| 21799 | "Connection: keep-alive\r\n\r\n"), |
| 21800 | }; |
| 21801 | MockRead mock_reads[] = { |
| 21802 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21803 | "Content-Length: 0\r\n\r\n"), |
| 21804 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21805 | "Content-Length: 0\r\n\r\n"), |
| 21806 | }; |
| 21807 | StaticSocketDataProvider data2(mock_reads, mock_writes); |
| 21808 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 21809 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 21810 | ssl2.expected_send_client_cert = true; |
| 21811 | ssl2.expected_client_cert = identity->certificate(); |
| 21812 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 21813 | |
| 21814 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21815 | |
| 21816 | // Start the first request. It succeeds after providing client certificates. |
| 21817 | TestCompletionCallback callback; |
| 21818 | auto trans = |
| 21819 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21820 | ASSERT_THAT(callback.GetResult(trans->Start(&request1, callback.callback(), |
| 21821 | NetLogWithSource())), |
| 21822 | IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 21823 | |
| 21824 | SSLCertRequestInfo* info = trans->GetResponseInfo()->cert_request_info.get(); |
| 21825 | ASSERT_TRUE(info); |
| 21826 | EXPECT_FALSE(info->is_proxy); |
| 21827 | EXPECT_EQ(info->host_and_port, cert_request_info->host_and_port); |
| 21828 | |
| 21829 | ASSERT_THAT(callback.GetResult(trans->RestartWithCertificate( |
| 21830 | identity->certificate(), identity->ssl_private_key(), |
| 21831 | callback.callback())), |
| 21832 | IsOk()); |
| 21833 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
| 21834 | |
| 21835 | // Make the second request. It completes without requesting client |
| 21836 | // certificates. |
| 21837 | trans = |
| 21838 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 21839 | ASSERT_THAT(callback.GetResult(trans->Start(&request2, callback.callback(), |
| 21840 | NetLogWithSource())), |
| 21841 | IsOk()); |
| 21842 | EXPECT_EQ(200, trans->GetResponseInfo()->headers->response_code()); |
| 21843 | } |
| 21844 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 21845 | // Test for kPartitionConnectionsByNetworkIsolationKey. Runs 3 requests in |
| 21846 | // sequence with two different NetworkIsolationKeys, the first and last have the |
| 21847 | // same key, the second a different one. Checks that the requests are |
| 21848 | // partitioned across sockets as expected. |
| 21849 | TEST_F(HttpNetworkTransactionTest, NetworkIsolation) { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 21850 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 21851 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 21852 | NetworkIsolationKey network_isolation_key1(kOrigin1, kOrigin1); |
| 21853 | NetworkIsolationKey network_isolation_key2(kOrigin2, kOrigin2); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 21854 | |
| 21855 | for (bool partition_connections : {false, true}) { |
| 21856 | SCOPED_TRACE(partition_connections); |
| 21857 | |
| 21858 | base::test::ScopedFeatureList feature_list; |
| 21859 | if (partition_connections) { |
| 21860 | feature_list.InitAndEnableFeature( |
| 21861 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21862 | } else { |
| 21863 | feature_list.InitAndDisableFeature( |
| 21864 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 21865 | } |
| 21866 | |
| 21867 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 21868 | |
| 21869 | // Reads and writes for the unpartitioned case, where only one socket is |
| 21870 | // used. |
| 21871 | |
| 21872 | const MockWrite kUnpartitionedWrites[] = { |
| 21873 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 21874 | "Host: foo.test\r\n" |
| 21875 | "Connection: keep-alive\r\n\r\n"), |
| 21876 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 21877 | "Host: foo.test\r\n" |
| 21878 | "Connection: keep-alive\r\n\r\n"), |
| 21879 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 21880 | "Host: foo.test\r\n" |
| 21881 | "Connection: keep-alive\r\n\r\n"), |
| 21882 | }; |
| 21883 | |
| 21884 | const MockRead kUnpartitionedReads[] = { |
| 21885 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21886 | "Connection: keep-alive\r\n" |
| 21887 | "Content-Length: 1\r\n\r\n" |
| 21888 | "1"), |
| 21889 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21890 | "Connection: keep-alive\r\n" |
| 21891 | "Content-Length: 1\r\n\r\n" |
| 21892 | "2"), |
| 21893 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21894 | "Connection: keep-alive\r\n" |
| 21895 | "Content-Length: 1\r\n\r\n" |
| 21896 | "3"), |
| 21897 | }; |
| 21898 | |
| 21899 | StaticSocketDataProvider unpartitioned_data(kUnpartitionedReads, |
| 21900 | kUnpartitionedWrites); |
| 21901 | |
| 21902 | // Reads and writes for the partitioned case, where two sockets are used. |
| 21903 | |
| 21904 | const MockWrite kPartitionedWrites1[] = { |
| 21905 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 21906 | "Host: foo.test\r\n" |
| 21907 | "Connection: keep-alive\r\n\r\n"), |
| 21908 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 21909 | "Host: foo.test\r\n" |
| 21910 | "Connection: keep-alive\r\n\r\n"), |
| 21911 | }; |
| 21912 | |
| 21913 | const MockRead kPartitionedReads1[] = { |
| 21914 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21915 | "Connection: keep-alive\r\n" |
| 21916 | "Content-Length: 1\r\n\r\n" |
| 21917 | "1"), |
| 21918 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21919 | "Connection: keep-alive\r\n" |
| 21920 | "Content-Length: 1\r\n\r\n" |
| 21921 | "3"), |
| 21922 | }; |
| 21923 | |
| 21924 | const MockWrite kPartitionedWrites2[] = { |
| 21925 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 21926 | "Host: foo.test\r\n" |
| 21927 | "Connection: keep-alive\r\n\r\n"), |
| 21928 | }; |
| 21929 | |
| 21930 | const MockRead kPartitionedReads2[] = { |
| 21931 | MockRead("HTTP/1.1 200 OK\r\n" |
| 21932 | "Connection: keep-alive\r\n" |
| 21933 | "Content-Length: 1\r\n\r\n" |
| 21934 | "2"), |
| 21935 | }; |
| 21936 | |
| 21937 | StaticSocketDataProvider partitioned_data1(kPartitionedReads1, |
| 21938 | kPartitionedWrites1); |
| 21939 | StaticSocketDataProvider partitioned_data2(kPartitionedReads2, |
| 21940 | kPartitionedWrites2); |
| 21941 | |
| 21942 | if (partition_connections) { |
| 21943 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data1); |
| 21944 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data2); |
| 21945 | } else { |
| 21946 | session_deps_.socket_factory->AddSocketDataProvider(&unpartitioned_data); |
| 21947 | } |
| 21948 | |
| 21949 | TestCompletionCallback callback; |
| 21950 | HttpRequestInfo request1; |
| 21951 | request1.method = "GET"; |
| 21952 | request1.url = GURL("http://foo.test/1"); |
| 21953 | request1.traffic_annotation = |
| 21954 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21955 | request1.network_isolation_key = network_isolation_key1; |
| 21956 | auto trans1 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21957 | session.get()); |
| 21958 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 21959 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21960 | std::string response_data1; |
| 21961 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 21962 | EXPECT_EQ("1", response_data1); |
| 21963 | trans1.reset(); |
| 21964 | |
| 21965 | HttpRequestInfo request2; |
| 21966 | request2.method = "GET"; |
| 21967 | request2.url = GURL("http://foo.test/2"); |
| 21968 | request2.traffic_annotation = |
| 21969 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21970 | request2.network_isolation_key = network_isolation_key2; |
| 21971 | auto trans2 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21972 | session.get()); |
| 21973 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 21974 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21975 | std::string response_data2; |
| 21976 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 21977 | EXPECT_EQ("2", response_data2); |
| 21978 | trans2.reset(); |
| 21979 | |
| 21980 | HttpRequestInfo request3; |
| 21981 | request3.method = "GET"; |
| 21982 | request3.url = GURL("http://foo.test/3"); |
| 21983 | request3.traffic_annotation = |
| 21984 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 21985 | request3.network_isolation_key = network_isolation_key1; |
| 21986 | auto trans3 = std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, |
| 21987 | session.get()); |
| 21988 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 21989 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 21990 | std::string response_data3; |
| 21991 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 21992 | EXPECT_EQ("3", response_data3); |
| 21993 | trans3.reset(); |
| 21994 | } |
| 21995 | } |
| 21996 | |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 21997 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationH2) { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 21998 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 21999 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22000 | NetworkIsolationKey network_isolation_key1(kOrigin1, kOrigin1); |
| 22001 | NetworkIsolationKey network_isolation_key2(kOrigin2, kOrigin2); |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 22002 | |
| 22003 | // Whether to use an H2 proxy. When false, uses HTTPS H2 requests without a |
| 22004 | // proxy, when true, uses HTTP requests over an H2 proxy. It's unnecessary to |
| 22005 | // test tunneled HTTPS over an H2 proxy, since that path sets up H2 sessions |
| 22006 | // the same way as the HTTP over H2 proxy case. |
| 22007 | for (bool use_proxy : {false, true}) { |
| 22008 | SCOPED_TRACE(use_proxy); |
| 22009 | if (use_proxy) { |
| 22010 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 22011 | ConfiguredProxyResolutionService::CreateFixedFromPacResult( |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 22012 | "HTTPS proxy:443", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22013 | } else { |
| 22014 | session_deps_.proxy_resolution_service = |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 22015 | ConfiguredProxyResolutionService::CreateDirect(); |
Matt Menke | ae58eeb | 2019-05-24 21:09:50 | [diff] [blame] | 22016 | } |
| 22017 | const char* url1 = nullptr; |
| 22018 | const char* url2 = nullptr; |
| 22019 | const char* url3 = nullptr; |
| 22020 | if (use_proxy) { |
| 22021 | url1 = "http://foo.test/1"; |
| 22022 | url2 = "http://foo.test/2"; |
| 22023 | url3 = "http://foo.test/3"; |
| 22024 | } else { |
| 22025 | url1 = "https://foo.test/1"; |
| 22026 | url2 = "https://foo.test/2"; |
| 22027 | url3 = "https://foo.test/3"; |
| 22028 | } |
| 22029 | |
| 22030 | for (bool partition_connections : {false, true}) { |
| 22031 | SCOPED_TRACE(partition_connections); |
| 22032 | |
| 22033 | base::test::ScopedFeatureList feature_list; |
| 22034 | if (partition_connections) { |
| 22035 | feature_list.InitAndEnableFeature( |
| 22036 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 22037 | } else { |
| 22038 | feature_list.InitAndDisableFeature( |
| 22039 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 22040 | } |
| 22041 | |
| 22042 | std::unique_ptr<HttpNetworkSession> session( |
| 22043 | CreateSession(&session_deps_)); |
| 22044 | |
| 22045 | // Reads and writes for the unpartitioned case, where only one socket is |
| 22046 | // used. |
| 22047 | |
| 22048 | SpdyTestUtil spdy_util; |
| 22049 | spdy::SpdySerializedFrame unpartitioned_req1( |
| 22050 | spdy_util.ConstructSpdyGet(url1, 1, LOWEST)); |
| 22051 | spdy::SpdySerializedFrame unpartitioned_response1( |
| 22052 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 22053 | spdy::SpdySerializedFrame unpartitioned_body1( |
| 22054 | spdy_util.ConstructSpdyDataFrame(1, "1", true)); |
| 22055 | spdy_util.UpdateWithStreamDestruction(1); |
| 22056 | |
| 22057 | spdy::SpdySerializedFrame unpartitioned_req2( |
| 22058 | spdy_util.ConstructSpdyGet(url2, 3, LOWEST)); |
| 22059 | spdy::SpdySerializedFrame unpartitioned_response2( |
| 22060 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 3)); |
| 22061 | spdy::SpdySerializedFrame unpartitioned_body2( |
| 22062 | spdy_util.ConstructSpdyDataFrame(3, "2", true)); |
| 22063 | spdy_util.UpdateWithStreamDestruction(3); |
| 22064 | |
| 22065 | spdy::SpdySerializedFrame unpartitioned_req3( |
| 22066 | spdy_util.ConstructSpdyGet(url3, 5, LOWEST)); |
| 22067 | spdy::SpdySerializedFrame unpartitioned_response3( |
| 22068 | spdy_util.ConstructSpdyGetReply(nullptr, 0, 5)); |
| 22069 | spdy::SpdySerializedFrame unpartitioned_body3( |
| 22070 | spdy_util.ConstructSpdyDataFrame(5, "3", true)); |
| 22071 | |
| 22072 | const MockWrite kUnpartitionedWrites[] = { |
| 22073 | CreateMockWrite(unpartitioned_req1, 0), |
| 22074 | CreateMockWrite(unpartitioned_req2, 3), |
| 22075 | CreateMockWrite(unpartitioned_req3, 6), |
| 22076 | }; |
| 22077 | |
| 22078 | const MockRead kUnpartitionedReads[] = { |
| 22079 | CreateMockRead(unpartitioned_response1, 1), |
| 22080 | CreateMockRead(unpartitioned_body1, 2), |
| 22081 | CreateMockRead(unpartitioned_response2, 4), |
| 22082 | CreateMockRead(unpartitioned_body2, 5), |
| 22083 | CreateMockRead(unpartitioned_response3, 7), |
| 22084 | CreateMockRead(unpartitioned_body3, 8), |
| 22085 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 9), |
| 22086 | }; |
| 22087 | |
| 22088 | SequencedSocketData unpartitioned_data(kUnpartitionedReads, |
| 22089 | kUnpartitionedWrites); |
| 22090 | |
| 22091 | // Reads and writes for the partitioned case, where two sockets are used. |
| 22092 | |
| 22093 | SpdyTestUtil spdy_util2; |
| 22094 | spdy::SpdySerializedFrame partitioned_req1( |
| 22095 | spdy_util2.ConstructSpdyGet(url1, 1, LOWEST)); |
| 22096 | spdy::SpdySerializedFrame partitioned_response1( |
| 22097 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 22098 | spdy::SpdySerializedFrame partitioned_body1( |
| 22099 | spdy_util2.ConstructSpdyDataFrame(1, "1", true)); |
| 22100 | spdy_util2.UpdateWithStreamDestruction(1); |
| 22101 | |
| 22102 | spdy::SpdySerializedFrame partitioned_req3( |
| 22103 | spdy_util2.ConstructSpdyGet(url3, 3, LOWEST)); |
| 22104 | spdy::SpdySerializedFrame partitioned_response3( |
| 22105 | spdy_util2.ConstructSpdyGetReply(nullptr, 0, 3)); |
| 22106 | spdy::SpdySerializedFrame partitioned_body3( |
| 22107 | spdy_util2.ConstructSpdyDataFrame(3, "3", true)); |
| 22108 | |
| 22109 | const MockWrite kPartitionedWrites1[] = { |
| 22110 | CreateMockWrite(partitioned_req1, 0), |
| 22111 | CreateMockWrite(partitioned_req3, 3), |
| 22112 | }; |
| 22113 | |
| 22114 | const MockRead kPartitionedReads1[] = { |
| 22115 | CreateMockRead(partitioned_response1, 1), |
| 22116 | CreateMockRead(partitioned_body1, 2), |
| 22117 | CreateMockRead(partitioned_response3, 4), |
| 22118 | CreateMockRead(partitioned_body3, 5), |
| 22119 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 6), |
| 22120 | }; |
| 22121 | |
| 22122 | SpdyTestUtil spdy_util3; |
| 22123 | spdy::SpdySerializedFrame partitioned_req2( |
| 22124 | spdy_util3.ConstructSpdyGet(url2, 1, LOWEST)); |
| 22125 | spdy::SpdySerializedFrame partitioned_response2( |
| 22126 | spdy_util3.ConstructSpdyGetReply(nullptr, 0, 1)); |
| 22127 | spdy::SpdySerializedFrame partitioned_body2( |
| 22128 | spdy_util3.ConstructSpdyDataFrame(1, "2", true)); |
| 22129 | |
| 22130 | const MockWrite kPartitionedWrites2[] = { |
| 22131 | CreateMockWrite(partitioned_req2, 0), |
| 22132 | }; |
| 22133 | |
| 22134 | const MockRead kPartitionedReads2[] = { |
| 22135 | CreateMockRead(partitioned_response2, 1), |
| 22136 | CreateMockRead(partitioned_body2, 2), |
| 22137 | MockRead(SYNCHRONOUS, ERR_IO_PENDING, 3), |
| 22138 | }; |
| 22139 | |
| 22140 | SequencedSocketData partitioned_data1(kPartitionedReads1, |
| 22141 | kPartitionedWrites1); |
| 22142 | SequencedSocketData partitioned_data2(kPartitionedReads2, |
| 22143 | kPartitionedWrites2); |
| 22144 | |
| 22145 | // No need to segment SSLDataProviders by whether or not partitioning is |
| 22146 | // enabled. |
| 22147 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 22148 | ssl_data1.next_proto = kProtoHTTP2; |
| 22149 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 22150 | ssl_data2.next_proto = kProtoHTTP2; |
| 22151 | |
| 22152 | if (partition_connections) { |
| 22153 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data1); |
| 22154 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22155 | session_deps_.socket_factory->AddSocketDataProvider(&partitioned_data2); |
| 22156 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 22157 | } else { |
| 22158 | session_deps_.socket_factory->AddSocketDataProvider( |
| 22159 | &unpartitioned_data); |
| 22160 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22161 | } |
| 22162 | |
| 22163 | TestCompletionCallback callback; |
| 22164 | HttpRequestInfo request1; |
| 22165 | request1.method = "GET"; |
| 22166 | request1.url = GURL(url1); |
| 22167 | request1.traffic_annotation = |
| 22168 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22169 | request1.network_isolation_key = network_isolation_key1; |
| 22170 | auto trans1 = |
| 22171 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22172 | int rv = |
| 22173 | trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22174 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22175 | std::string response_data1; |
| 22176 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22177 | EXPECT_EQ("1", response_data1); |
| 22178 | trans1.reset(); |
| 22179 | |
| 22180 | HttpRequestInfo request2; |
| 22181 | request2.method = "GET"; |
| 22182 | request2.url = GURL(url2); |
| 22183 | request2.traffic_annotation = |
| 22184 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22185 | request2.network_isolation_key = network_isolation_key2; |
| 22186 | auto trans2 = |
| 22187 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22188 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22189 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22190 | std::string response_data2; |
| 22191 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22192 | EXPECT_EQ("2", response_data2); |
| 22193 | trans2.reset(); |
| 22194 | |
| 22195 | HttpRequestInfo request3; |
| 22196 | request3.method = "GET"; |
| 22197 | request3.url = GURL(url3); |
| 22198 | request3.traffic_annotation = |
| 22199 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22200 | request3.network_isolation_key = network_isolation_key1; |
| 22201 | auto trans3 = |
| 22202 | std::make_unique<HttpNetworkTransaction>(LOWEST, session.get()); |
| 22203 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22204 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22205 | std::string response_data3; |
| 22206 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22207 | EXPECT_EQ("3", response_data3); |
| 22208 | trans3.reset(); |
| 22209 | } |
| 22210 | } |
| 22211 | } |
| 22212 | |
Matt Menke | 7281f87 | 2019-06-25 19:29:25 | [diff] [blame] | 22213 | // Preconnect two sockets with different NetworkIsolationKeys when |
| 22214 | // features::kPartitionConnectionsByNetworkIsolationKey is enabled. Then issue a |
| 22215 | // request and make sure the correct socket is used. Loops three times, |
| 22216 | // expecting to use the first preconnect, second preconnect, and neither. |
| 22217 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationPreconnect) { |
| 22218 | base::test::ScopedFeatureList feature_list; |
| 22219 | feature_list.InitAndEnableFeature( |
| 22220 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 22221 | |
| 22222 | enum class TestCase { |
| 22223 | kUseFirstPreconnect, |
| 22224 | kUseSecondPreconnect, |
| 22225 | kDontUsePreconnect, |
| 22226 | }; |
| 22227 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22228 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22229 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22230 | const auto kOrigin3 = url::Origin::Create(GURL("http://origin3/")); |
| 22231 | NetworkIsolationKey preconnect1_isolation_key(kOrigin1, kOrigin1); |
| 22232 | NetworkIsolationKey preconnect2_isolation_key(kOrigin2, kOrigin2); |
| 22233 | NetworkIsolationKey not_preconnected_isolation_key(kOrigin3, kOrigin3); |
Matt Menke | 7281f87 | 2019-06-25 19:29:25 | [diff] [blame] | 22234 | |
| 22235 | // Test that only preconnects with |
| 22236 | for (TestCase test_case : |
| 22237 | {TestCase::kUseFirstPreconnect, TestCase::kUseSecondPreconnect, |
| 22238 | TestCase::kDontUsePreconnect}) { |
| 22239 | SpdySessionDependencies session_deps; |
| 22240 | // Make DNS lookups completely synchronously, so preconnects complete |
| 22241 | // immediately. |
| 22242 | session_deps.host_resolver->set_synchronous_mode(true); |
| 22243 | |
| 22244 | const MockWrite kMockWrites[] = { |
| 22245 | MockWrite(ASYNC, 0, |
| 22246 | "GET / HTTP/1.1\r\n" |
| 22247 | "Host: www.foo.com\r\n" |
| 22248 | "Connection: keep-alive\r\n\r\n"), |
| 22249 | }; |
| 22250 | |
| 22251 | const MockRead kMockReads[] = { |
| 22252 | MockRead(ASYNC, 1, |
| 22253 | "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n" |
| 22254 | "hello"), |
| 22255 | }; |
| 22256 | |
| 22257 | // Used for the socket that will actually be used, which may or may not be |
| 22258 | // one of the preconnects |
| 22259 | SequencedSocketData used_socket_data(MockConnect(SYNCHRONOUS, OK), |
| 22260 | kMockReads, kMockWrites); |
| 22261 | |
| 22262 | // Used for the preconnects that won't actually be used. |
| 22263 | SequencedSocketData preconnect1_data(MockConnect(SYNCHRONOUS, OK), |
| 22264 | base::span<const MockRead>(), |
| 22265 | base::span<const MockWrite>()); |
| 22266 | SequencedSocketData preconnect2_data(MockConnect(SYNCHRONOUS, OK), |
| 22267 | base::span<const MockRead>(), |
| 22268 | base::span<const MockWrite>()); |
| 22269 | |
| 22270 | NetworkIsolationKey network_isolation_key_for_request; |
| 22271 | |
| 22272 | switch (test_case) { |
| 22273 | case TestCase::kUseFirstPreconnect: |
| 22274 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22275 | session_deps.socket_factory->AddSocketDataProvider(&preconnect2_data); |
| 22276 | network_isolation_key_for_request = preconnect1_isolation_key; |
| 22277 | break; |
| 22278 | case TestCase::kUseSecondPreconnect: |
| 22279 | session_deps.socket_factory->AddSocketDataProvider(&preconnect1_data); |
| 22280 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22281 | network_isolation_key_for_request = preconnect2_isolation_key; |
| 22282 | break; |
| 22283 | case TestCase::kDontUsePreconnect: |
| 22284 | session_deps.socket_factory->AddSocketDataProvider(&preconnect1_data); |
| 22285 | session_deps.socket_factory->AddSocketDataProvider(&preconnect2_data); |
| 22286 | session_deps.socket_factory->AddSocketDataProvider(&used_socket_data); |
| 22287 | network_isolation_key_for_request = not_preconnected_isolation_key; |
| 22288 | break; |
| 22289 | } |
| 22290 | |
| 22291 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 22292 | |
| 22293 | // Preconnect sockets. |
| 22294 | HttpRequestInfo request; |
| 22295 | request.method = "GET"; |
| 22296 | request.url = GURL("http://www.foo.com/"); |
| 22297 | request.traffic_annotation = |
| 22298 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22299 | |
| 22300 | request.network_isolation_key = preconnect1_isolation_key; |
| 22301 | session->http_stream_factory()->PreconnectStreams(1, request); |
| 22302 | |
| 22303 | request.network_isolation_key = preconnect2_isolation_key; |
| 22304 | session->http_stream_factory()->PreconnectStreams(1, request); |
| 22305 | |
| 22306 | request.network_isolation_key = network_isolation_key_for_request; |
| 22307 | |
| 22308 | EXPECT_EQ(2, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22309 | |
| 22310 | // Make the request. |
| 22311 | TestCompletionCallback callback; |
| 22312 | |
| 22313 | HttpNetworkTransaction trans(DEFAULT_PRIORITY, session.get()); |
| 22314 | |
| 22315 | int rv = trans.Start(&request, callback.callback(), NetLogWithSource()); |
| 22316 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22317 | |
| 22318 | rv = callback.WaitForResult(); |
| 22319 | EXPECT_THAT(rv, IsOk()); |
| 22320 | |
| 22321 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
| 22322 | ASSERT_TRUE(response); |
| 22323 | ASSERT_TRUE(response->headers); |
| 22324 | EXPECT_EQ(200, response->headers->response_code()); |
| 22325 | |
| 22326 | std::string response_data; |
| 22327 | rv = ReadTransaction(&trans, &response_data); |
| 22328 | EXPECT_THAT(rv, IsOk()); |
| 22329 | EXPECT_EQ("hello", response_data); |
| 22330 | |
| 22331 | if (test_case != TestCase::kDontUsePreconnect) { |
| 22332 | EXPECT_EQ(2, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22333 | } else { |
| 22334 | EXPECT_EQ(3, GetIdleSocketCountInTransportSocketPool(session.get())); |
| 22335 | } |
| 22336 | } |
| 22337 | } |
| 22338 | |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22339 | // Test that the NetworkIsolationKey is passed down to SSLConfig so the session |
| 22340 | // cache is isolated. |
| 22341 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationSSL) { |
| 22342 | base::test::ScopedFeatureList feature_list; |
| 22343 | feature_list.InitWithFeatures( |
| 22344 | {features::kPartitionConnectionsByNetworkIsolationKey, |
| 22345 | features::kPartitionSSLSessionsByNetworkIsolationKey}, |
| 22346 | {}); |
| 22347 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22348 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22349 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22350 | const NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 22351 | const NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22352 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 22353 | |
| 22354 | // The server always sends Connection: close, so each request goes over a |
| 22355 | // distinct socket. |
| 22356 | |
| 22357 | const MockWrite kWrites1[] = { |
| 22358 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22359 | "Host: foo.test\r\n" |
| 22360 | "Connection: keep-alive\r\n\r\n")}; |
| 22361 | |
| 22362 | const MockRead kReads1[] = { |
| 22363 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22364 | "Connection: close\r\n" |
| 22365 | "Content-Length: 1\r\n\r\n" |
| 22366 | "1")}; |
| 22367 | |
| 22368 | const MockWrite kWrites2[] = { |
| 22369 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 22370 | "Host: foo.test\r\n" |
| 22371 | "Connection: keep-alive\r\n\r\n")}; |
| 22372 | |
| 22373 | const MockRead kReads2[] = { |
| 22374 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22375 | "Connection: close\r\n" |
| 22376 | "Content-Length: 1\r\n\r\n" |
| 22377 | "2")}; |
| 22378 | |
| 22379 | const MockWrite kWrites3[] = { |
| 22380 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 22381 | "Host: foo.test\r\n" |
| 22382 | "Connection: keep-alive\r\n\r\n")}; |
| 22383 | |
| 22384 | const MockRead kReads3[] = { |
| 22385 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22386 | "Connection: close\r\n" |
| 22387 | "Content-Length: 1\r\n\r\n" |
| 22388 | "3")}; |
| 22389 | |
| 22390 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22391 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22392 | StaticSocketDataProvider data3(kReads3, kWrites3); |
| 22393 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22394 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22395 | session_deps_.socket_factory->AddSocketDataProvider(&data3); |
| 22396 | |
| 22397 | SSLSocketDataProvider ssl_data1(ASYNC, OK); |
| 22398 | ssl_data1.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22399 | ssl_data1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22400 | SSLSocketDataProvider ssl_data2(ASYNC, OK); |
| 22401 | ssl_data2.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22402 | ssl_data2.expected_network_isolation_key = kNetworkIsolationKey2; |
| 22403 | SSLSocketDataProvider ssl_data3(ASYNC, OK); |
| 22404 | ssl_data3.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22405 | ssl_data3.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22406 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data1); |
| 22407 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data2); |
| 22408 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data3); |
| 22409 | |
| 22410 | TestCompletionCallback callback; |
| 22411 | HttpRequestInfo request1; |
| 22412 | request1.method = "GET"; |
| 22413 | request1.url = GURL("https://foo.test/1"); |
| 22414 | request1.traffic_annotation = |
| 22415 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22416 | request1.network_isolation_key = kNetworkIsolationKey1; |
| 22417 | auto trans1 = |
| 22418 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22419 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22420 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22421 | std::string response_data1; |
| 22422 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22423 | EXPECT_EQ("1", response_data1); |
| 22424 | trans1.reset(); |
| 22425 | |
| 22426 | HttpRequestInfo request2; |
| 22427 | request2.method = "GET"; |
| 22428 | request2.url = GURL("https://foo.test/2"); |
| 22429 | request2.traffic_annotation = |
| 22430 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22431 | request2.network_isolation_key = kNetworkIsolationKey2; |
| 22432 | auto trans2 = |
| 22433 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22434 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22435 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22436 | std::string response_data2; |
| 22437 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22438 | EXPECT_EQ("2", response_data2); |
| 22439 | trans2.reset(); |
| 22440 | |
| 22441 | HttpRequestInfo request3; |
| 22442 | request3.method = "GET"; |
| 22443 | request3.url = GURL("https://foo.test/3"); |
| 22444 | request3.traffic_annotation = |
| 22445 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22446 | request3.network_isolation_key = kNetworkIsolationKey1; |
| 22447 | auto trans3 = |
| 22448 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22449 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22450 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22451 | std::string response_data3; |
| 22452 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22453 | EXPECT_EQ("3", response_data3); |
| 22454 | trans3.reset(); |
| 22455 | } |
| 22456 | |
| 22457 | // Test that the NetworkIsolationKey is passed down to SSLConfig so the session |
| 22458 | // cache is isolated, for both origins and proxies. |
| 22459 | TEST_F(HttpNetworkTransactionTest, NetworkIsolationSSLProxy) { |
| 22460 | base::test::ScopedFeatureList feature_list; |
| 22461 | feature_list.InitWithFeatures( |
| 22462 | {features::kPartitionConnectionsByNetworkIsolationKey, |
| 22463 | features::kPartitionSSLSessionsByNetworkIsolationKey}, |
| 22464 | {}); |
| 22465 | |
Nicolas Arciniega | d2013f9 | 2020-02-07 23:00:56 | [diff] [blame] | 22466 | session_deps_.proxy_resolution_service = |
| 22467 | ConfiguredProxyResolutionService::CreateFixed( |
| 22468 | "https://myproxy:70", TRAFFIC_ANNOTATION_FOR_TESTS); |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22469 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 22470 | const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/")); |
| 22471 | const auto kOrigin2 = url::Origin::Create(GURL("http://origin2/")); |
| 22472 | const NetworkIsolationKey kNetworkIsolationKey1(kOrigin1, kOrigin1); |
| 22473 | const NetworkIsolationKey kNetworkIsolationKey2(kOrigin2, kOrigin2); |
David Benjamin | 6f2da65 | 2019-06-26 23:36:35 | [diff] [blame] | 22474 | std::unique_ptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 22475 | |
| 22476 | // Make both a tunneled and non-tunneled request. |
| 22477 | HttpRequestInfo request1; |
| 22478 | request1.method = "GET"; |
| 22479 | request1.url = GURL("https://foo.test/1"); |
| 22480 | request1.traffic_annotation = |
| 22481 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22482 | request1.network_isolation_key = kNetworkIsolationKey1; |
| 22483 | |
| 22484 | HttpRequestInfo request2; |
| 22485 | request2.method = "GET"; |
| 22486 | request2.url = GURL("http://foo.test/2"); |
| 22487 | request2.traffic_annotation = |
| 22488 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22489 | request2.network_isolation_key = kNetworkIsolationKey2; |
| 22490 | |
| 22491 | const MockWrite kWrites1[] = { |
| 22492 | MockWrite("CONNECT foo.test:443 HTTP/1.1\r\n" |
| 22493 | "Host: foo.test:443\r\n" |
| 22494 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 22495 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22496 | "Host: foo.test\r\n" |
| 22497 | "Connection: keep-alive\r\n\r\n")}; |
| 22498 | |
| 22499 | const MockRead kReads1[] = { |
| 22500 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 22501 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22502 | "Connection: close\r\n" |
| 22503 | "Content-Length: 1\r\n\r\n" |
| 22504 | "1")}; |
| 22505 | |
| 22506 | const MockWrite kWrites2[] = { |
| 22507 | MockWrite("GET http://foo.test/2 HTTP/1.1\r\n" |
| 22508 | "Host: foo.test\r\n" |
| 22509 | "Proxy-Connection: keep-alive\r\n\r\n")}; |
| 22510 | |
| 22511 | const MockRead kReads2[] = { |
| 22512 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22513 | "Connection: close\r\n" |
| 22514 | "Content-Length: 1\r\n\r\n" |
| 22515 | "2")}; |
| 22516 | |
| 22517 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22518 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22519 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22520 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22521 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22522 | |
| 22523 | SSLSocketDataProvider ssl_proxy1(ASYNC, OK); |
| 22524 | ssl_proxy1.expected_host_and_port = HostPortPair("myproxy", 70); |
| 22525 | ssl_proxy1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22526 | SSLSocketDataProvider ssl_origin1(ASYNC, OK); |
| 22527 | ssl_origin1.expected_host_and_port = HostPortPair("foo.test", 443); |
| 22528 | ssl_origin1.expected_network_isolation_key = kNetworkIsolationKey1; |
| 22529 | SSLSocketDataProvider ssl_proxy2(ASYNC, OK); |
| 22530 | ssl_proxy2.expected_host_and_port = HostPortPair("myproxy", 70); |
| 22531 | ssl_proxy2.expected_network_isolation_key = kNetworkIsolationKey2; |
| 22532 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy1); |
| 22533 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_origin1); |
| 22534 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_proxy2); |
| 22535 | |
| 22536 | TestCompletionCallback callback; |
| 22537 | auto trans1 = |
| 22538 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22539 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22540 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22541 | std::string response_data1; |
| 22542 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22543 | EXPECT_EQ("1", response_data1); |
| 22544 | trans1.reset(); |
| 22545 | |
| 22546 | auto trans2 = |
| 22547 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22548 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22549 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22550 | std::string response_data2; |
| 22551 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22552 | EXPECT_EQ("2", response_data2); |
| 22553 | trans2.reset(); |
| 22554 | } |
| 22555 | |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22556 | // Test that SSLConfig changes from SSLConfigService are picked up even when |
| 22557 | // there are live sockets. |
| 22558 | TEST_F(HttpNetworkTransactionTest, SSLConfigChanged) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22559 | SSLContextConfig ssl_context_config; |
| 22560 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22561 | auto ssl_config_service = |
| 22562 | std::make_unique<TestSSLConfigService>(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22563 | TestSSLConfigService* ssl_config_service_raw = ssl_config_service.get(); |
| 22564 | |
| 22565 | session_deps_.ssl_config_service = std::move(ssl_config_service); |
| 22566 | |
| 22567 | // Make three requests. Between the second and third, the SSL config will |
| 22568 | // change. |
| 22569 | HttpRequestInfo request1; |
| 22570 | request1.method = "GET"; |
| 22571 | request1.url = GURL("https://foo.test/1"); |
| 22572 | request1.traffic_annotation = |
| 22573 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22574 | |
| 22575 | HttpRequestInfo request2; |
| 22576 | request2.method = "GET"; |
| 22577 | request2.url = GURL("https://foo.test/2"); |
| 22578 | request2.traffic_annotation = |
| 22579 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22580 | |
| 22581 | HttpRequestInfo request3; |
| 22582 | request3.method = "GET"; |
| 22583 | request3.url = GURL("https://foo.test/3"); |
| 22584 | request3.traffic_annotation = |
| 22585 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22586 | |
| 22587 | const MockWrite kWrites1[] = { |
| 22588 | MockWrite("GET /1 HTTP/1.1\r\n" |
| 22589 | "Host: foo.test\r\n" |
| 22590 | "Connection: keep-alive\r\n\r\n"), |
| 22591 | MockWrite("GET /2 HTTP/1.1\r\n" |
| 22592 | "Host: foo.test\r\n" |
| 22593 | "Connection: keep-alive\r\n\r\n"), |
| 22594 | }; |
| 22595 | |
| 22596 | const MockRead kReads1[] = { |
| 22597 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22598 | "Connection: keep-alive\r\n" |
| 22599 | "Content-Length: 1\r\n\r\n" |
| 22600 | "1"), |
| 22601 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22602 | "Connection: keep-alive\r\n" |
| 22603 | "Content-Length: 1\r\n\r\n" |
| 22604 | "2"), |
| 22605 | }; |
| 22606 | |
| 22607 | // The third request goes on a different socket because the SSL config has |
| 22608 | // changed. |
| 22609 | const MockWrite kWrites2[] = { |
| 22610 | MockWrite("GET /3 HTTP/1.1\r\n" |
| 22611 | "Host: foo.test\r\n" |
| 22612 | "Connection: keep-alive\r\n\r\n")}; |
| 22613 | |
| 22614 | const MockRead kReads2[] = { |
| 22615 | MockRead("HTTP/1.1 200 OK\r\n" |
| 22616 | "Connection: keep-alive\r\n" |
| 22617 | "Content-Length: 1\r\n\r\n" |
| 22618 | "3")}; |
| 22619 | |
| 22620 | StaticSocketDataProvider data1(kReads1, kWrites1); |
| 22621 | StaticSocketDataProvider data2(kReads2, kWrites2); |
| 22622 | session_deps_.socket_factory->AddSocketDataProvider(&data1); |
| 22623 | session_deps_.socket_factory->AddSocketDataProvider(&data2); |
| 22624 | |
| 22625 | SSLSocketDataProvider ssl1(ASYNC, OK); |
| 22626 | ssl1.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22627 | SSLSocketDataProvider ssl2(ASYNC, OK); |
| 22628 | ssl2.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22629 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl1); |
| 22630 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl2); |
| 22631 | |
| 22632 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22633 | |
| 22634 | TestCompletionCallback callback; |
| 22635 | auto trans1 = |
| 22636 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22637 | int rv = trans1->Start(&request1, callback.callback(), NetLogWithSource()); |
| 22638 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22639 | std::string response_data1; |
| 22640 | EXPECT_THAT(ReadTransaction(trans1.get(), &response_data1), IsOk()); |
| 22641 | EXPECT_EQ("1", response_data1); |
| 22642 | trans1.reset(); |
| 22643 | |
| 22644 | auto trans2 = |
| 22645 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22646 | rv = trans2->Start(&request2, callback.callback(), NetLogWithSource()); |
| 22647 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22648 | std::string response_data2; |
| 22649 | EXPECT_THAT(ReadTransaction(trans2.get(), &response_data2), IsOk()); |
| 22650 | EXPECT_EQ("2", response_data2); |
| 22651 | trans2.reset(); |
| 22652 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22653 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22654 | ssl_config_service_raw->UpdateSSLConfigAndNotify(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22655 | |
| 22656 | auto trans3 = |
| 22657 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22658 | rv = trans3->Start(&request3, callback.callback(), NetLogWithSource()); |
| 22659 | EXPECT_THAT(callback.GetResult(rv), IsOk()); |
| 22660 | std::string response_data3; |
| 22661 | EXPECT_THAT(ReadTransaction(trans3.get(), &response_data3), IsOk()); |
| 22662 | EXPECT_EQ("3", response_data3); |
| 22663 | trans3.reset(); |
| 22664 | } |
| 22665 | |
| 22666 | TEST_F(HttpNetworkTransactionTest, SSLConfigChangedPendingConnect) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22667 | SSLContextConfig ssl_context_config; |
| 22668 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22669 | auto ssl_config_service = |
| 22670 | std::make_unique<TestSSLConfigService>(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22671 | TestSSLConfigService* ssl_config_service_raw = ssl_config_service.get(); |
| 22672 | |
| 22673 | session_deps_.ssl_config_service = std::move(ssl_config_service); |
| 22674 | |
| 22675 | HttpRequestInfo request; |
| 22676 | request.method = "GET"; |
| 22677 | request.url = GURL("https://foo.test/1"); |
| 22678 | request.traffic_annotation = |
| 22679 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22680 | |
| 22681 | // Make a socket which never connects. |
| 22682 | StaticSocketDataProvider data({}, {}); |
| 22683 | session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 22684 | SSLSocketDataProvider ssl_data(SYNCHRONOUS, ERR_IO_PENDING); |
| 22685 | ssl_data.expected_ssl_version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 22686 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_data); |
| 22687 | |
| 22688 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22689 | |
| 22690 | TestCompletionCallback callback; |
| 22691 | auto trans = |
| 22692 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22693 | int rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
| 22694 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22695 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 22696 | ssl_context_config.version_max = SSL_PROTOCOL_VERSION_TLS1_2; |
| 22697 | ssl_config_service_raw->UpdateSSLConfigAndNotify(ssl_context_config); |
David Benjamin | ef2f2a5a | 2019-07-16 19:21:31 | [diff] [blame] | 22698 | |
| 22699 | EXPECT_THAT(callback.GetResult(rv), IsError(ERR_NETWORK_CHANGED)); |
| 22700 | } |
| 22701 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 22702 | // Test that HttpNetworkTransaction correctly handles existing sockets when the |
| 22703 | // server requests a client certificate post-handshake (via a TLS |
| 22704 | // renegotiation). This is a regression test for https://crbug.com/829184. |
| 22705 | TEST_F(HttpNetworkTransactionTest, PostHandshakeClientCertWithSockets) { |
| 22706 | const MutableNetworkTrafficAnnotationTag kTrafficAnnotation( |
| 22707 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 22708 | |
| 22709 | auto cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>(); |
| 22710 | cert_request_info->host_and_port = HostPortPair("foo.test", 443); |
| 22711 | |
| 22712 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 22713 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 22714 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 22715 | ASSERT_TRUE(identity); |
| 22716 | |
| 22717 | // This test will make several requests so that, when the client certificate |
| 22718 | // request comes in, we have a socket in use, an idle socket, and a socket for |
| 22719 | // an unrelated host. |
| 22720 | // |
| 22721 | // First, two long-lived requests which do not complete until after the client |
| 22722 | // certificate request. This arranges for sockets to be in use during the |
| 22723 | // request. They should not be interrupted. |
| 22724 | HttpRequestInfo request_long_lived; |
| 22725 | request_long_lived.method = "GET"; |
| 22726 | request_long_lived.url = GURL("https://foo.test/long-lived"); |
| 22727 | request_long_lived.traffic_annotation = kTrafficAnnotation; |
| 22728 | |
| 22729 | HttpRequestInfo request_long_lived_bar; |
| 22730 | request_long_lived_bar.method = "GET"; |
| 22731 | request_long_lived_bar.url = GURL("https://bar.test/long-lived"); |
| 22732 | request_long_lived_bar.traffic_annotation = kTrafficAnnotation; |
| 22733 | |
| 22734 | // Next, make a request that needs client certificates. |
| 22735 | HttpRequestInfo request_auth; |
| 22736 | request_auth.method = "GET"; |
| 22737 | request_auth.url = GURL("https://foo.test/auth"); |
| 22738 | request_auth.traffic_annotation = kTrafficAnnotation; |
| 22739 | |
| 22740 | // Before responding to the challenge, make a request to an unauthenticated |
| 22741 | // endpoint. This will result in an idle socket when the client certificate |
| 22742 | // challenge is resolved. |
| 22743 | HttpRequestInfo request_unauth; |
| 22744 | request_unauth.method = "GET"; |
| 22745 | request_unauth.url = GURL("https://foo.test/unauth"); |
| 22746 | request_unauth.traffic_annotation = kTrafficAnnotation; |
| 22747 | |
| 22748 | // After all the preceding requests complete, end with two additional requests |
| 22749 | // to ensure pre-authentication foo.test sockets are not used and bar.test |
| 22750 | // sockets are unaffected. |
| 22751 | HttpRequestInfo request_post_auth; |
| 22752 | request_post_auth.method = "GET"; |
| 22753 | request_post_auth.url = GURL("https://foo.test/post-auth"); |
| 22754 | request_post_auth.traffic_annotation = kTrafficAnnotation; |
| 22755 | |
| 22756 | HttpRequestInfo request_post_auth_bar; |
| 22757 | request_post_auth_bar.method = "GET"; |
| 22758 | request_post_auth_bar.url = GURL("https://bar.test/post-auth"); |
| 22759 | request_post_auth_bar.traffic_annotation = kTrafficAnnotation; |
| 22760 | |
| 22761 | // The sockets for /long-lived and /unauth complete their request but are |
| 22762 | // not allocated for /post-auth or /retry because SSL state has since changed. |
| 22763 | const MockWrite kLongLivedWrites[] = { |
| 22764 | MockWrite(ASYNC, 0, |
| 22765 | "GET /long-lived HTTP/1.1\r\n" |
| 22766 | "Host: foo.test\r\n" |
| 22767 | "Connection: keep-alive\r\n\r\n"), |
| 22768 | }; |
| 22769 | const MockRead kLongLivedReads[] = { |
| 22770 | // Pause so /long-lived completes after the client presents client |
| 22771 | // certificates. |
| 22772 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 22773 | MockRead(ASYNC, 2, |
| 22774 | "HTTP/1.1 200 OK\r\n" |
| 22775 | "Connection: keep-alive\r\n" |
| 22776 | "Content-Length: 10\r\n\r\n" |
| 22777 | "long-lived"), |
| 22778 | }; |
| 22779 | SequencedSocketData data_long_lived(kLongLivedReads, kLongLivedWrites); |
| 22780 | SSLSocketDataProvider ssl_long_lived(ASYNC, OK); |
| 22781 | session_deps_.socket_factory->AddSocketDataProvider(&data_long_lived); |
| 22782 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_long_lived); |
| 22783 | |
| 22784 | // Requests for bar.test should be unaffected by foo.test and get allocated |
| 22785 | // a single socket. |
| 22786 | const MockWrite kBarWrites[] = { |
| 22787 | MockWrite(ASYNC, 0, |
| 22788 | "GET /long-lived HTTP/1.1\r\n" |
| 22789 | "Host: bar.test\r\n" |
| 22790 | "Connection: keep-alive\r\n\r\n"), |
| 22791 | MockWrite(ASYNC, 3, |
| 22792 | "GET /post-auth HTTP/1.1\r\n" |
| 22793 | "Host: bar.test\r\n" |
| 22794 | "Connection: keep-alive\r\n\r\n"), |
| 22795 | }; |
| 22796 | const MockRead kBarReads[] = { |
| 22797 | // Pause on /long-lived so it completes after foo.test's authentication. |
| 22798 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 22799 | MockRead(ASYNC, 2, |
| 22800 | "HTTP/1.1 200 OK\r\n" |
| 22801 | "Connection: keep-alive\r\n" |
| 22802 | "Content-Length: 10\r\n\r\n" |
| 22803 | "long-lived"), |
| 22804 | MockRead(ASYNC, 4, |
| 22805 | "HTTP/1.1 200 OK\r\n" |
| 22806 | "Connection: keep-alive\r\n" |
| 22807 | "Content-Length: 9\r\n\r\n" |
| 22808 | "post-auth"), |
| 22809 | }; |
| 22810 | SequencedSocketData data_bar(kBarReads, kBarWrites); |
| 22811 | SSLSocketDataProvider ssl_bar(ASYNC, OK); |
| 22812 | session_deps_.socket_factory->AddSocketDataProvider(&data_bar); |
| 22813 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_bar); |
| 22814 | |
| 22815 | // Requesting /auth results in a post-handshake client certificate challenge. |
| 22816 | const MockWrite kAuthWrites[] = { |
| 22817 | MockWrite(ASYNC, 0, |
| 22818 | "GET /auth HTTP/1.1\r\n" |
| 22819 | "Host: foo.test\r\n" |
| 22820 | "Connection: keep-alive\r\n\r\n"), |
| 22821 | }; |
| 22822 | const MockRead kAuthReads[] = { |
| 22823 | MockRead(ASYNC, ERR_SSL_CLIENT_AUTH_CERT_NEEDED, 1), |
| 22824 | }; |
| 22825 | SequencedSocketData data_auth(kAuthReads, kAuthWrites); |
| 22826 | SSLSocketDataProvider ssl_auth(ASYNC, OK); |
| 22827 | ssl_auth.cert_request_info = cert_request_info.get(); |
| 22828 | session_deps_.socket_factory->AddSocketDataProvider(&data_auth); |
| 22829 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_auth); |
| 22830 | |
| 22831 | // Requesting /unauth completes. |
| 22832 | const MockWrite kUnauthWrites[] = { |
| 22833 | MockWrite(ASYNC, 0, |
| 22834 | "GET /unauth HTTP/1.1\r\n" |
| 22835 | "Host: foo.test\r\n" |
| 22836 | "Connection: keep-alive\r\n\r\n"), |
| 22837 | }; |
| 22838 | const MockRead kUnauthReads[] = { |
| 22839 | MockRead(ASYNC, 1, |
| 22840 | "HTTP/1.1 200 OK\r\n" |
| 22841 | "Connection: keep-alive\r\n" |
| 22842 | "Content-Length: 6\r\n\r\n" |
| 22843 | "unauth"), |
| 22844 | }; |
| 22845 | SequencedSocketData data_unauth(kUnauthReads, kUnauthWrites); |
| 22846 | SSLSocketDataProvider ssl_unauth(ASYNC, OK); |
| 22847 | session_deps_.socket_factory->AddSocketDataProvider(&data_unauth); |
| 22848 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_unauth); |
| 22849 | |
| 22850 | // When the client certificate is selected, /auth is retried on a new |
| 22851 | // connection. In particular, it should not be retried on |data_unauth|, |
| 22852 | // which would not honor the new client certificate configuration. |
| 22853 | const MockWrite kRetryWrites[] = { |
| 22854 | MockWrite(ASYNC, 0, |
| 22855 | "GET /auth HTTP/1.1\r\n" |
| 22856 | "Host: foo.test\r\n" |
| 22857 | "Connection: keep-alive\r\n\r\n"), |
| 22858 | }; |
| 22859 | const MockRead kRetryReads[] = { |
| 22860 | MockRead(ASYNC, 1, |
| 22861 | "HTTP/1.1 200 OK\r\n" |
| 22862 | // Close the connection so we test that /post-auth is not |
| 22863 | // allocated to |data_unauth| or |data_long_lived|. |
| 22864 | "Connection: close\r\n" |
| 22865 | "Content-Length: 4\r\n\r\n" |
| 22866 | "auth"), |
| 22867 | }; |
| 22868 | SequencedSocketData data_retry(kRetryReads, kRetryWrites); |
| 22869 | SSLSocketDataProvider ssl_retry(ASYNC, OK); |
| 22870 | ssl_retry.expected_send_client_cert = true; |
| 22871 | ssl_retry.expected_client_cert = identity->certificate(); |
| 22872 | session_deps_.socket_factory->AddSocketDataProvider(&data_retry); |
| 22873 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_retry); |
| 22874 | |
| 22875 | // /post-auth gets its own socket. |
| 22876 | const MockWrite kPostAuthWrites[] = { |
| 22877 | MockWrite(ASYNC, 0, |
| 22878 | "GET /post-auth HTTP/1.1\r\n" |
| 22879 | "Host: foo.test\r\n" |
| 22880 | "Connection: keep-alive\r\n\r\n"), |
| 22881 | }; |
| 22882 | const MockRead kPostAuthReads[] = { |
| 22883 | MockRead(ASYNC, 1, |
| 22884 | "HTTP/1.1 200 OK\r\n" |
| 22885 | "Connection: keep-alive\r\n" |
| 22886 | "Content-Length: 9\r\n\r\n" |
| 22887 | "post-auth"), |
| 22888 | }; |
| 22889 | SequencedSocketData data_post_auth(kPostAuthReads, kPostAuthWrites); |
| 22890 | SSLSocketDataProvider ssl_post_auth(ASYNC, OK); |
| 22891 | ssl_post_auth.expected_send_client_cert = true; |
| 22892 | ssl_post_auth.expected_client_cert = identity->certificate(); |
| 22893 | session_deps_.socket_factory->AddSocketDataProvider(&data_post_auth); |
| 22894 | session_deps_.socket_factory->AddSSLSocketDataProvider(&ssl_post_auth); |
| 22895 | |
| 22896 | std::unique_ptr<HttpNetworkSession> session = CreateSession(&session_deps_); |
| 22897 | |
| 22898 | // Start the two long-lived requests. |
| 22899 | TestCompletionCallback callback_long_lived; |
| 22900 | auto trans_long_lived = |
| 22901 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22902 | int rv = trans_long_lived->Start( |
| 22903 | &request_long_lived, callback_long_lived.callback(), NetLogWithSource()); |
| 22904 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22905 | data_long_lived.RunUntilPaused(); |
| 22906 | |
| 22907 | TestCompletionCallback callback_long_lived_bar; |
| 22908 | auto trans_long_lived_bar = |
| 22909 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22910 | rv = trans_long_lived_bar->Start(&request_long_lived_bar, |
| 22911 | callback_long_lived_bar.callback(), |
| 22912 | NetLogWithSource()); |
| 22913 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 22914 | data_bar.RunUntilPaused(); |
| 22915 | |
| 22916 | // Request /auth. This gives a client certificate challenge. |
| 22917 | TestCompletionCallback callback_auth; |
| 22918 | auto trans_auth = |
| 22919 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22920 | rv = trans_auth->Start(&request_auth, callback_auth.callback(), |
| 22921 | NetLogWithSource()); |
| 22922 | EXPECT_THAT(callback_auth.GetResult(rv), |
| 22923 | IsError(ERR_SSL_CLIENT_AUTH_CERT_NEEDED)); |
| 22924 | |
| 22925 | // Make an unauthenticated request. This completes. |
| 22926 | TestCompletionCallback callback_unauth; |
| 22927 | auto trans_unauth = |
| 22928 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22929 | rv = trans_unauth->Start(&request_unauth, callback_unauth.callback(), |
| 22930 | NetLogWithSource()); |
| 22931 | EXPECT_THAT(callback_unauth.GetResult(rv), IsOk()); |
| 22932 | std::string response_unauth; |
| 22933 | EXPECT_THAT(ReadTransaction(trans_unauth.get(), &response_unauth), IsOk()); |
| 22934 | EXPECT_EQ("unauth", response_unauth); |
| 22935 | trans_unauth.reset(); |
| 22936 | |
| 22937 | // Complete the authenticated request. |
| 22938 | rv = trans_auth->RestartWithCertificate(identity->certificate(), |
| 22939 | identity->ssl_private_key(), |
| 22940 | callback_auth.callback()); |
| 22941 | EXPECT_THAT(callback_auth.GetResult(rv), IsOk()); |
| 22942 | std::string response_auth; |
| 22943 | EXPECT_THAT(ReadTransaction(trans_auth.get(), &response_auth), IsOk()); |
| 22944 | EXPECT_EQ("auth", response_auth); |
| 22945 | trans_auth.reset(); |
| 22946 | |
| 22947 | // Complete the long-lived requests. |
| 22948 | data_long_lived.Resume(); |
| 22949 | EXPECT_THAT(callback_long_lived.GetResult(ERR_IO_PENDING), IsOk()); |
| 22950 | std::string response_long_lived; |
| 22951 | EXPECT_THAT(ReadTransaction(trans_long_lived.get(), &response_long_lived), |
| 22952 | IsOk()); |
| 22953 | EXPECT_EQ("long-lived", response_long_lived); |
| 22954 | trans_long_lived.reset(); |
| 22955 | |
| 22956 | data_bar.Resume(); |
| 22957 | EXPECT_THAT(callback_long_lived_bar.GetResult(ERR_IO_PENDING), IsOk()); |
| 22958 | std::string response_long_lived_bar; |
| 22959 | EXPECT_THAT( |
| 22960 | ReadTransaction(trans_long_lived_bar.get(), &response_long_lived_bar), |
| 22961 | IsOk()); |
| 22962 | EXPECT_EQ("long-lived", response_long_lived_bar); |
| 22963 | trans_long_lived_bar.reset(); |
| 22964 | |
| 22965 | // Run the post-authentication requests. |
| 22966 | TestCompletionCallback callback_post_auth; |
| 22967 | auto trans_post_auth = |
| 22968 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22969 | rv = trans_post_auth->Start(&request_post_auth, callback_post_auth.callback(), |
| 22970 | NetLogWithSource()); |
| 22971 | EXPECT_THAT(callback_post_auth.GetResult(rv), IsOk()); |
| 22972 | std::string response_post_auth; |
| 22973 | EXPECT_THAT(ReadTransaction(trans_post_auth.get(), &response_post_auth), |
| 22974 | IsOk()); |
| 22975 | EXPECT_EQ("post-auth", response_post_auth); |
| 22976 | trans_post_auth.reset(); |
| 22977 | |
| 22978 | TestCompletionCallback callback_post_auth_bar; |
| 22979 | auto trans_post_auth_bar = |
| 22980 | std::make_unique<HttpNetworkTransaction>(DEFAULT_PRIORITY, session.get()); |
| 22981 | rv = trans_post_auth_bar->Start(&request_post_auth_bar, |
| 22982 | callback_post_auth_bar.callback(), |
| 22983 | NetLogWithSource()); |
| 22984 | EXPECT_THAT(callback_post_auth_bar.GetResult(rv), IsOk()); |
| 22985 | std::string response_post_auth_bar; |
| 22986 | EXPECT_THAT( |
| 22987 | ReadTransaction(trans_post_auth_bar.get(), &response_post_auth_bar), |
| 22988 | IsOk()); |
| 22989 | EXPECT_EQ("post-auth", response_post_auth_bar); |
| 22990 | trans_post_auth_bar.reset(); |
| 22991 | } |
| 22992 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 22993 | } // namespace net |